View Javadoc

1   package uk.ac.cam.spectra.spectrasub.components;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.apache.log4j.Logger;
7   import org.apache.tapestry.BaseComponent;
8   import org.apache.tapestry.annotations.Persist;
9   import org.apache.tapestry.event.PageBeginRenderListener;
10  import org.apache.tapestry.event.PageEvent;
11  import org.apache.tapestry.form.IFormComponent;
12  import org.apache.tapestry.valid.IValidationDelegate;
13  import org.apache.tapestry.valid.ValidationConstraint;
14  
15  import uk.ac.cam.spectra.SpectraMetadata;
16  import uk.ac.cam.spectra.spectrasub.DataPackage;
17  import uk.ac.cam.spectra.spectrasub.Name;
18  
19  import com.hp.hpl.jena.rdf.model.Resource;
20  
21  public abstract class DefaultAuthorFormlet extends BaseComponent implements
22  		Formlet, PageBeginRenderListener {
23  
24  	private String noSubmit;
25  
26  	protected static final Logger LOG = Logger
27  			.getLogger(DefaultAuthorFormlet.class);
28  
29  	public void pageBeginRender(PageEvent event) {
30  		LOG.debug("Page begin render");
31  		if (getNewChemist() == null) {
32  			setNewChemist(new Name());
33  		}
34  
35  		if (event.getRequestCycle().isRewinding()) {
36  			LOG.debug("Form is rewinding");
37  			if (getChemists() == null) {
38  				List<Name> cs = new ArrayList<Name>(3);
39  				setChemists(cs);
40  			}
41  			setSupervisor(new Name());
42  		}
43  	}
44  
45  	public void setupPage(DataPackage p) {
46  		LOG.debug("setupPage");
47  		List<String> cNames = p.getValues(SpectraMetadata.chemist);
48  		List<Name> cs = new ArrayList<Name>(cNames.size());
49  		for (String c : cNames) {
50  			Name name = Name.decode(c);
51  			if (!name.isBlank()) {
52  				cs.add(name);
53  			}
54  		}
55  		if (getNewChemist() != null) {
56  			if (getNewChemist().isBlank()) {
57  				LOG.debug("Adding new chemist to existing");
58  				cs.add(getNewChemist());
59  				setNewChemist(new Name());
60  			}
61  		}
62  		LOG.debug("Setting " + cs.size() + " chemists: " + cs);
63  		setChemists(cs);
64  
65  		String supervisor = p.getValue(SpectraMetadata.supervisor);
66  		if (supervisor != null) {
67  			LOG.debug("Setting supervisor to " + supervisor);
68  			setSupervisor(Name.decode(supervisor));
69  		} else {
70  			LOG.debug("Setting blank name for supervisor");
71  			setSupervisor(new Name());
72  		}
73  
74  		String institution = p.getValue(SpectraMetadata.institution);
75  		if (institution != null) {
76  			setInstitution(institution);
77  		}
78  
79  	}
80  
81  	public void validate(IValidationDelegate delegate) {
82  		LOG.debug("validate");
83  		if (getChemists().isEmpty() && getNewChemist().isBlank()) {
84  			delegate
85  					.setFormComponent((IFormComponent) getComponent("newChemistSurname"));
86  			delegate.record("At least one Chemist required",
87  					ValidationConstraint.REQUIRED);
88  		}
89  	}
90  
91  	public void writeMetadata(Resource resource) {
92  		LOG.debug("Submission. Chemists: " + getChemists());
93  		if (!Name.isEmpty(getChemists())) {
94  			LOG.debug("Not empty");
95  			for (Name n : Name.trim(getChemists())) {
96  				LOG.debug("adding " + n);
97  				SpectraMetadata.chemist.add(resource, n.toString());
98  			}
99  		}
100 
101 		LOG.debug("Extra chemist." + getNewChemist());
102 		if (getNewChemist() != null && !getNewChemist().isBlank()) {
103 			SpectraMetadata.chemist.add(resource, getNewChemist().toString());
104 			setNewChemist(new Name());
105 		}
106 
107 		LOG.debug("Supervisor " + getSupervisor());
108 		if (!getSupervisor().isBlank()) {
109 			SpectraMetadata.supervisor
110 					.add(resource, getSupervisor().toString());
111 		}
112 		if (getInstitution() != null) {
113 			SpectraMetadata.institution.add(resource, getInstitution());
114 		}
115 
116 	}
117 
118 	/**
119 	 * If the add more chemist link has been clicked, stop the submission.
120 	 */
121 	public boolean allowSubmit() {
122 		return !"true".equals(noSubmit);
123 	}
124 
125 	@Persist
126 	public abstract List<Name> getChemists();
127 
128 	public abstract void setChemists(List<Name> chemists);
129 
130 	public abstract Name getChemistName();
131 
132 	public abstract Name getNewChemist();
133 
134 	public abstract void setNewChemist(Name n);
135 
136 	public abstract Name getSupervisor();
137 
138 	public abstract void setSupervisor(Name n);
139 
140 	public void clearSubmissionBlock() {
141 		noSubmit = Boolean.FALSE.toString();
142 	}
143 
144 	public abstract void setInstitution(String institution);
145 
146 	public abstract String getInstitution();
147 
148 	public String getNoSubmit() {
149 		return noSubmit;
150 	}
151 
152 	public void setNoSubmit(String noSubmit) {
153 		this.noSubmit = noSubmit;
154 	}
155 
156 }