1 package org.cateproject.controller.flow.action;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Set;
6 import java.util.UUID;
7
8 import org.cateproject.service.event.CdmObjectEvent;
9 import org.hibernate.LockMode;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.binding.message.MessageContext;
12 import org.springframework.dao.DataIntegrityViolationException;
13 import org.springframework.dao.InvalidDataAccessApiUsageException;
14 import org.springframework.webflow.engine.FlowExecutionExceptionHandler;
15 import org.springframework.webflow.engine.RequestControlContext;
16 import org.springframework.webflow.execution.FlowExecutionException;
17 import org.springframework.webflow.execution.RequestContext;
18
19 import eu.etaxonomy.cdm.api.service.IAgentService;
20 import eu.etaxonomy.cdm.api.service.IIdentifiableEntityService;
21 import eu.etaxonomy.cdm.api.service.IReferenceService;
22 import eu.etaxonomy.cdm.model.agent.AgentBase;
23 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
24 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
25 import eu.etaxonomy.cdm.model.media.Rights;
26 import eu.etaxonomy.cdm.model.media.RightsTerm;
27 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
28
29 public abstract class AbstractEditAction<T extends IdentifiableEntity,SERVICE extends IIdentifiableEntityService<T>, FORM extends AbstractReplaceForm<? extends T, ? extends T>> extends AbstractFlowAction<T,SERVICE> implements FlowExecutionExceptionHandler {
30
31 protected IAgentService agentService;
32
33 @Autowired
34 public void setAgentService(IAgentService agentService) {
35 this.agentService = agentService;
36 }
37
38 protected IReferenceService referenceService;
39
40 @Autowired
41 public void setReferenceService(IReferenceService referenceService) {
42 this.referenceService = referenceService;
43 }
44
45 public abstract String editOrCreate(UUID uuid, RequestContext requestContext);
46
47 public abstract FORM setUpReplaceForm(T t);
48
49 public abstract Boolean replace(FORM replaceForm, MessageContext messageContext);
50
51 public abstract Boolean setReplacement(UUID uuid, FORM replaceForm);
52
53 public abstract boolean delete(T t, MessageContext messageContext);
54 public abstract T createNewInstance(Class clazz, RequestContext requestContext);
55
56 public abstract List<Class> getAvailableClasses(Class clazz);
57
58 public abstract boolean updateTitleCache(T t);
59
60 public abstract Boolean saveOrUpdate(T object, MessageContext messageContext);
61
62 protected Boolean doSave(T object, MessageContext messageContext) {
63 object.setProtectedTitleCache(true);
64 service.saveOrUpdate(object);
65 applicationContext.publishEvent(new CdmObjectEvent<T>(this,object));
66 return Boolean.TRUE;
67 }
68
69 protected Boolean doDelete(T object, MessageContext messageContext) {
70 service.delete(object);
71 applicationContext.publishEvent(new CdmObjectEvent<T>(this,object));
72 return Boolean.TRUE;
73 }
74
75 protected Boolean doReplace(AbstractReplaceForm<T,T> replaceForm, MessageContext messageContext) {
76 service.replace(replaceForm.getOriginal(), replaceForm.getReplacement());
77 applicationContext.publishEvent(new CdmObjectEvent<T>(this,replaceForm.getOriginal()));
78
79
80
81
82 if(replaceForm.getReplacement() != null) {
83 applicationContext.publishEvent(new CdmObjectEvent<T>(this,replaceForm.getReplacement()));
84 }
85 return Boolean.TRUE;
86 }
87
88 public T load(UUID uuid, RequestContext requestContext) {
89 List<String> propertyPaths = new ArrayList<String>();
90 T t = service.load(uuid,propertyPaths);
91
92 return t;
93 }
94
95 public boolean canHandle(FlowExecutionException exception) {
96 if(exception.getCause().getClass().equals(DataIntegrityViolationException.class)) {
97 return true;
98 }
99 if(exception.getCause().getClass().equals(InvalidDataAccessApiUsageException.class)) {
100 return true;
101 }
102 return false;
103 }
104
105 public void handle(FlowExecutionException exception, RequestControlContext context) {
106 leaveMessage("has.references",context);
107 }
108
109 public Rights getRights(T t, UUID uuid) {
110 Rights rights = null;
111 for(Rights r : (Set<Rights>)t.getRights()) {
112 if(r.getUuid().equals(uuid)) {
113 rights = r;
114 break;
115 }
116 }
117 return rights;
118 }
119
120 public Rights addRights(T t, String type) {
121 Rights rights = Rights.NewInstance();
122
123 if(type.equals("copyright")) {
124 rights.setType(RightsTerm.COPYRIGHT());
125 rights.setAbbreviatedText("©");
126 rights.setText("Copyright");
127 } else if(type.equals("by")) {
128 rights.setType(RightsTerm.LICENSE());
129 rights.setAbbreviatedText("by");
130 rights.setText("This work is licensed under a Creative Commons Attribution 3.0 Unported License");
131 rights.setUri("http://creativecommons.org/licenses/by/3.0/");
132 } else if(type.equals("by-sa")) {
133 rights.setType(RightsTerm.LICENSE());
134 rights.setAbbreviatedText("by-sa");
135 rights.setText("This work is licensed under a Creative Commons Attribution-Share-Alike 3.0 Unported License");
136 rights.setUri("http://creativecommons.org/licenses/by-sa/3.0/");
137 } else if(type.equals("by-nd")) {
138 rights.setType(RightsTerm.LICENSE());
139 rights.setAbbreviatedText("by-nd");
140 rights.setText("This work is licensed under a Creative Commons Attribution-No-Derivatives 3.0 Unported License");
141 rights.setUri("http://creativecommons.org/licenses/by-nd/3.0/");
142 } else if(type.equals("by-nc")) {
143 rights.setType(RightsTerm.LICENSE());
144 rights.setAbbreviatedText("by-nc");
145 rights.setText("This work is licensed under a Creative Commons Attribution-Non-Commercial 3.0 Unported License");
146 rights.setUri("http://creativecommons.org/licenses/by-nc/3.0/");
147 } else if(type.equals("by-nc-sa")) {
148 rights.setType(RightsTerm.LICENSE());
149 rights.setAbbreviatedText("by-nc-sa");
150 rights.setText("This work is licensed under a Creative Commons Attribution-Non-Commercial-Share-Alike 3.0 Unported License");
151 rights.setUri("http://creativecommons.org/licenses/by-nc-sa/3.0/");
152 } else if(type.equals("by-nc-nd")) {
153 rights.setType(RightsTerm.LICENSE());
154 rights.setAbbreviatedText("by-nc-nd");
155 rights.setText("This work is licensed under a Creative Commons Attribution-Non-Commercial-No-Derivatives 3.0 Unported License");
156 rights.setUri("http://creativecommons.org/licenses/by-nc-nd/3.0/");
157 }
158
159 t.addRights(rights);
160
161 return rights;
162 }
163
164 public Boolean removeRights(T t, UUID uuid) {
165 Rights rightsToRemove = null;
166
167 for(Rights r : (Set<Rights>)t.getRights()) {
168 if(r.getUuid().equals(uuid)) {
169 rightsToRemove = r;
170 break;
171 }
172 }
173
174 if(rightsToRemove == null) {
175 return Boolean.FALSE;
176 }
177
178 t.removeRights(rightsToRemove);
179
180 return Boolean.TRUE;
181 }
182
183 public Boolean removeRightsHolder(Rights rights) {
184 rights.setAgent(null);
185 return Boolean.TRUE;
186 }
187
188 public Boolean setRightsHolder(UUID agent, Rights rights) {
189 AgentBase rightsHolder = agentService.find(agent);
190 agentService.refresh(rightsHolder,LockMode.READ,null);
191 rights.setAgent(rightsHolder);
192 return Boolean.TRUE;
193 }
194
195 public IdentifiableSource getSource(T t, UUID uuid) {
196 IdentifiableSource source = null;
197 for(IdentifiableSource s : (Set<IdentifiableSource>)t.getSources()) {
198 if(s.getUuid().equals(uuid)) {
199 source = s;
200 break;
201 }
202 }
203 return source;
204 }
205
206 public IdentifiableSource addSource(T t) {
207 IdentifiableSource source = IdentifiableSource.NewInstance();
208
209 t.addSource(source);
210
211 return source;
212 }
213
214 public Boolean removeSource(T t, UUID uuid) {
215 IdentifiableSource sourceToRemove = null;
216
217 for(IdentifiableSource s : (Set<IdentifiableSource>)t.getSources()) {
218 if(s.getUuid().equals(uuid)) {
219 sourceToRemove = s;
220 break;
221 }
222 }
223
224 if(sourceToRemove == null) {
225 return Boolean.FALSE;
226 }
227
228 t.removeSource(sourceToRemove);
229
230 return Boolean.TRUE;
231 }
232
233 public Boolean removeCitation(IdentifiableSource source) {
234 source.setCitation(null);
235 return Boolean.TRUE;
236 }
237
238 public Boolean setCitation(UUID ref, IdentifiableSource source) {
239 ReferenceBase reference = referenceService.find(ref);
240 referenceService.refresh(reference,LockMode.READ,null);
241 source.setCitation(reference);
242 return Boolean.TRUE;
243 }
244 }