View Javadoc

1   package uk.ac.cam.spectra.spectrasub.pages;
2   
3   import org.apache.log4j.Logger;
4   import org.apache.tapestry.IPage;
5   import org.apache.tapestry.PageRedirectException;
6   import org.apache.tapestry.annotations.InjectObject;
7   import org.apache.tapestry.annotations.InjectPage;
8   import org.apache.tapestry.event.PageBeginRenderListener;
9   import org.apache.tapestry.event.PageEvent;
10  import org.apache.tapestry.form.IPropertySelectionModel;
11  import org.apache.tapestry.form.LabeledPropertySelectionModel;
12  import org.apache.tapestry.form.StringPropertySelectionModel;
13  import org.apache.tapestry.valid.IValidationDelegate;
14  
15  import uk.ac.cam.spectra.Combine;
16  import uk.ac.cam.spectra.SpectraMetadata;
17  import uk.ac.cam.spectra.spectrasub.DataPackage;
18  import uk.ac.cam.spectra.spectrasub.DataPackageManager;
19  import uk.ac.cam.spectra.spectrasub.NotFoundException;
20  import uk.ac.cam.spectra.spectrasub.Profile;
21  import uk.ac.cam.spectra.spectrasub.components.Formlet;
22  import uk.ac.cam.spectra.spectrasub.components.UploadMessage.UploadInfo;
23  
24  import com.hp.hpl.jena.rdf.model.Model;
25  import com.hp.hpl.jena.rdf.model.ModelFactory;
26  import com.hp.hpl.jena.rdf.model.Resource;
27  
28  public abstract class AddMetadata extends BaseSubmissionPage implements
29          PageBeginRenderListener {
30  
31      private static final Logger LOG = Logger.getLogger(AddMetadata.class);
32  
33      private static final String EMBARGO_RELEASE_AUTO = "automatic";
34  
35      private static final String EMBARGO_RELEASE_MANUAL = "manual";
36  
37      public void pageBeginRender(PageEvent event) {
38  
39          if (!getRequestCycle().isRewinding()) {
40              setupPage(event);
41          }
42      }
43  
44      /**
45       * This required to make sure custom validation gets performed as well as
46       * the built in validation when there are errors. If there aren't any errors
47       * from the built in validators, then this won't be called anyway.
48       */
49      public void somethingClicked() {
50          if (getDelegate().getHasErrors()) {
51              formValidation();
52          }
53      }
54  
55      private void setupPage(PageEvent event) {
56          String id = getPackageId();
57          if (id == null) {
58              throw new RuntimeException("Package ID lost, cannot continue");
59          } else {
60              LOG.debug("Loading package: " + id);
61              DataPackage p = loadPackage();
62  
63              // Set up formlets
64              for (Object comp : getComponents().values()) {
65                  if (comp instanceof Formlet) {
66                      ((Formlet) comp).setupPage(p);
67                  }
68              }
69  
70              if (!getDelegate().getHasErrors()) {
71  
72                  Integer ePeriod = p
73                          .getIntegerValue(SpectraMetadata.embargo_period);
74                  if (ePeriod != null) {
75                      setEmbargoPeriod(ePeriod);
76                  }
77  
78                  String release = p.getValue(SpectraMetadata.embargo_release);
79                  if (release != null) {
80                      setEmbargoRelease(EMBARGO_RELEASE_MANUAL.equals(release));
81                  }
82  
83                  String chemicalFormula = p
84                          .getValue(SpectraMetadata.chemical_formula);
85                  if (chemicalFormula != null) {
86                      setChemicalFormula(chemicalFormula);
87                  }
88  
89                  String systematicName = p
90                          .getValue(SpectraMetadata.chemical_systematic_name);
91                  if (systematicName != null) {
92                      setSystematicName(systematicName);
93                  }
94  
95                  String compoundClass = p
96                          .getValue(SpectraMetadata.chemical_compound_class);
97                  if (compoundClass != null) {
98                      setCompoundClass(compoundClass);
99                  }
100             }
101         }
102 
103     }
104 
105     private void formValidation() {
106         IValidationDelegate delegate = getDelegate();
107         // Validate formlets.
108         for (Object comp : getComponents().values()) {
109             if (comp instanceof Formlet) {
110                 ((Formlet) comp).validate(delegate);
111             }
112         }
113     }
114 
115     public IPage submit() {
116         LOG.debug("Submit called");
117         formValidation();
118         Model m = ModelFactory.createDefaultModel();
119         DataPackage p;
120         try {
121             p = getPackageDao().load(getPackageId());
122             setPackage(p);
123             Resource pRes = m.createResource(p.getNormalizedURIString());
124             if (getEmbargoPeriod() != null) {
125                 SpectraMetadata.embargo_period.add(pRes, getEmbargoPeriod());
126             }
127             SpectraMetadata.embargo_release.add(pRes,
128                     getEmbargoRelease() ? EMBARGO_RELEASE_AUTO
129                             : EMBARGO_RELEASE_MANUAL);
130 
131             if (getChemicalFormula() != null) {
132                 SpectraMetadata.chemical_formula
133                         .add(pRes, getChemicalFormula());
134             }
135 
136             if (getSystematicName() != null) {
137                 SpectraMetadata.chemical_systematic_name.add(pRes,
138                         getSystematicName());
139             }
140 
141             if (getCompoundClass() != null) {
142                 SpectraMetadata.chemical_compound_class.add(pRes,
143                         getCompoundClass());
144             }
145 
146             for (Object comp : getComponents().values()) {
147                 if (comp instanceof Formlet) {
148                     ((Formlet) comp).writeMetadata(pRes);
149                 }
150             }
151 
152             p.addMetadata(m, Combine.overwrite);
153             getPackageDao().save(p);
154 
155             boolean allowSubmission = formletsAllowSubmission();
156 
157             if (!getDelegate().getHasErrors() && allowSubmission) {
158                 getPackageManager().buildPackage(p);
159                 Check check = getCheck();
160                 check.setPackageId(p.getId());
161                 throw new PageRedirectException(check);
162             } else {
163                 clearFormletsSubmissionBlocking();
164                 AddMetadata am = getAddMetadata();
165                 am.setPackageId(getPackageId());
166                 return am;
167             }
168         } catch (NotFoundException e) {
169             LOG.error("Could not find process: " + e.getMessage(), e);
170             throw new RuntimeException("Could not find process: "
171                     + e.getMessage(), e);
172         }
173     }
174 
175     private void clearFormletsSubmissionBlocking() {
176         for (Object comp : getComponents().values()) {
177             if (comp instanceof Formlet) {
178                 Formlet formlet = (Formlet) comp;
179                 formlet.clearSubmissionBlock();
180             }
181         }
182     }
183 
184     private boolean formletsAllowSubmission() {
185         boolean allowSubmission = true;
186         for (Object comp : getComponents().values()) {
187             if (comp instanceof Formlet) {
188                 if (!((Formlet) comp).allowSubmit()) {
189                     LOG
190                             .debug("component "
191                                     + comp
192                                     + " forbade submission, will return to add metadata form.");
193                     allowSubmission = false;
194                     break;
195                 }
196             }
197         }
198         return allowSubmission;
199     }
200 
201     public IPropertySelectionModel getEmbargoOptions() {
202         final int[] periods = new int[] { 0, 6, 12, 24, 36 };
203         final String[] labels = new String[] {
204                 "No embargo - publish immediately", "6 months", "1 year",
205                 "2 years", "3 years" };
206 
207         return new LabeledPropertySelectionModel(new IPropertySelectionModel() {
208 
209             public String getLabel(int i) {
210                 return labels[i];
211             }
212 
213             public Object getOption(int i) {
214                 return periods[i];
215             }
216 
217             public int getOptionCount() {
218                 return periods.length;
219             }
220 
221             public String getValue(int i) {
222                 return Integer.valueOf(periods[i]).toString();
223             }
224 
225             public Object translateValue(String val) {
226                 return Integer.valueOf(val);
227             }
228         }, "[ Select one ]");
229     }
230 
231     public IPropertySelectionModel getCompoundClassOptions() {
232         return new LabeledPropertySelectionModel(
233                 new StringPropertySelectionModel(
234                         new String[] { "Organic", "Bio-Organic",
235                                 "Organometallic", "Inorganic", "Polymer" }),
236                 "[Select one]");
237     }
238 
239     /**
240      * The metadata page to use is configured into spring via the profile.
241      * 
242      * @return
243      */
244     public AddMetadata getAddMetadata() {
245         return (AddMetadata) getRequestCycle().getPage(
246                 getProfile().getMetadataPage());
247     }
248 
249     @InjectObject("spring:profile")
250     public abstract Profile getProfile();
251 
252     public abstract String getCompoundClass();
253 
254     public abstract void setCompoundClass(String cclass);
255 
256     /**
257      * True if embargo release is automatic
258      * 
259      * @return
260      */
261     public abstract boolean getEmbargoRelease();
262 
263     public abstract void setEmbargoRelease(boolean release);
264 
265     public abstract Integer getEmbargoPeriod();
266 
267     public abstract void setEmbargoPeriod(Integer period);
268 
269     public abstract int getIdx();
270 
271     public abstract String getChemicalFormula();
272 
273     public abstract void setChemicalFormula(String chemicalFormula);
274 
275     public abstract String getSystematicName();
276 
277     public abstract void setSystematicName(String sName);
278 
279     public abstract IValidationDelegate getDelegate();
280 
281     @InjectObject("spring:packageManager")
282     public abstract DataPackageManager getPackageManager();
283 
284     @InjectPage("Check")
285     public abstract Check getCheck();
286 
287     public abstract void setUploadInfo(UploadInfo info);
288 
289     public abstract UploadInfo getUploadInfo();
290 }