comparison artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java @ 219:cabe4c02ab64

Refactored the CallContextImpl - there are two concrete classes for Artifacts and ArtifactCollections now. artifacts/trunk@1559 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 24 Mar 2011 16:16:51 +0000
parents b2115f484edb
children 98695dc6d94d
comparison
equal deleted inserted replaced
218:70cbbe144931 219:cabe4c02ab64
86 */ 86 */
87 public static final String NO_SUCH_ARTIFACT = 87 public static final String NO_SUCH_ARTIFACT =
88 "No such artifact"; 88 "No such artifact";
89 89
90 /** 90 /**
91 * Error message issued if one tries to remove a requested artifact
92 * from the list of artifacts running in background which is
93 * not in this list.
94 */
95 public static final String NOT_IN_BACKGROUND =
96 "Not in background";
97
98 /**
99 * Error message issued if an artifact wants to translate itself
100 * into a none valid persistent state.
101 */
102 public static final String INVALID_CALL_STATE =
103 "Invalid after call state";
104
105 /**
106 * Error message issued if the creation of an artifact failed. 91 * Error message issued if the creation of an artifact failed.
107 */ 92 */
108 public static final String CREATION_FAILED = 93 public static final String CREATION_FAILED =
109 "Creation of artifact failed"; 94 "Creation of artifact failed";
110 95
227 * collection. 212 * collection.
228 */ 213 */
229 public static final String XPATH_COLLECTION_ITEM_ATTRIBUTE = 214 public static final String XPATH_COLLECTION_ITEM_ATTRIBUTE =
230 "/art:action/art:type/art:artifact/art:attribute"; 215 "/art:action/art:type/art:artifact/art:attribute";
231 216
232
233 /**
234 * Inner class that implements the call context handed
235 * to the methods calls describe(), feed(), etc. of the artifact.
236 */
237 public class CallContextImpl
238 implements CallContext
239 {
240 /**
241 * The persistence wrapper around the living artifact
242 */
243 protected PersistentArtifact artifact;
244 /**
245 * The action to be performed after the artifact calls
246 * desribe(), feed(), etc. return.
247 */
248 protected int action;
249 /**
250 * The meta information of the concrete call
251 * (preferred languages et. al.)
252 */
253 protected CallMeta callMeta;
254 /**
255 * Map to act like a clipboard when nesting calls
256 * like a proxy artifact.
257 */
258 protected HashMap customValues;
259
260 /**
261 * Constructor to create a call context with a given
262 * persistent artifact, a default action and meta informations.
263 * @param artifact The persistent wrapper around a living artifact.
264 * @param action The action to be performed after the concrete
265 * artifact call has returned.
266 * @param callMeta The meta information for this call context.
267 */
268 public CallContextImpl(
269 PersistentArtifact artifact,
270 int action,
271 CallMeta callMeta
272 ) {
273 this.artifact = artifact;
274 this.action = action;
275 this.callMeta = callMeta;
276 }
277
278 public void afterCall(int action) {
279 this.action = action;
280 if (action == BACKGROUND) {
281 addIdToBackground(artifact.getId());
282 }
283 }
284
285 public void afterBackground(int action) {
286 if (this.action != BACKGROUND) {
287 throw new IllegalStateException(NOT_IN_BACKGROUND);
288 }
289 fromBackground(artifact, action);
290 }
291
292 public Object globalContext() {
293 return context;
294 }
295
296 public ArtifactDatabase getDatabase() {
297 return ArtifactDatabaseImpl.this;
298 }
299
300 public CallMeta getMeta() {
301 return callMeta;
302 }
303
304 public Long getTimeToLive() {
305 return artifact.getTTL();
306 }
307
308 /**
309 * Dispatches and executes the persistence action after
310 * the return of the concrete artifact call.
311 */
312 public void postCall() {
313 switch (action) {
314 case NOTHING:
315 break;
316 case TOUCH:
317 artifact.touch();
318 break;
319 case STORE:
320 artifact.store();
321 break;
322 case BACKGROUND:
323 logger.warn(
324 "BACKGROUND processing is not fully implemented, yet!");
325 artifact.store();
326 break;
327 default:
328 logger.error(INVALID_CALL_STATE + ": " + action);
329 throw new IllegalStateException(INVALID_CALL_STATE);
330 }
331 }
332
333 public Object getContextValue(Object key) {
334 return customValues != null
335 ? customValues.get(key)
336 : null;
337 }
338
339 public Object putContextValue(Object key, Object value) {
340 if (customValues == null) {
341 customValues = new HashMap();
342 }
343 return customValues.put(key, value);
344 }
345 } // class CallContextImpl
346 217
347 /** 218 /**
348 * This inner class allows the deferral of writing the output 219 * This inner class allows the deferral of writing the output
349 * of the artifact's out() call. 220 * of the artifact's out() call.
350 */ 221 */
388 this.callMeta = callMeta; 259 this.callMeta = callMeta;
389 } 260 }
390 261
391 public void write(OutputStream output) throws IOException { 262 public void write(OutputStream output) throws IOException {
392 263
393 CallContextImpl cc = new CallContextImpl( 264 ArtifactCallContext cc = new ArtifactCallContext(
394 artifact, CallContext.TOUCH, callMeta); 265 ArtifactDatabaseImpl.this,
266 CallContext.TOUCH,
267 callMeta,
268 context,
269 artifact);
395 270
396 try { 271 try {
397 artifact.getArtifact().out(format, output, cc); 272 artifact.getArtifact().out(format, output, cc);
398 } 273 }
399 finally { 274 finally {
695 catch (Exception e) { 570 catch (Exception e) {
696 logger.error(e.getLocalizedMessage(), e); 571 logger.error(e.getLocalizedMessage(), e);
697 throw new ArtifactDatabaseException(CREATION_FAILED); 572 throw new ArtifactDatabaseException(CREATION_FAILED);
698 } 573 }
699 574
700 CallContextImpl cc = new CallContextImpl( 575 ArtifactCallContext cc = new ArtifactCallContext(
701 persistentArtifact, CallContext.NOTHING, callMeta); 576 ArtifactDatabaseImpl.this,
577 CallContext.NOTHING,
578 callMeta,
579 context,
580 persistentArtifact);
702 581
703 try { 582 try {
704 return artifact.describe(null, cc); 583 return artifact.describe(null, cc);
705 } 584 }
706 finally { 585 finally {
720 599
721 if (artifact == null) { 600 if (artifact == null) {
722 throw new ArtifactDatabaseException(NO_SUCH_ARTIFACT); 601 throw new ArtifactDatabaseException(NO_SUCH_ARTIFACT);
723 } 602 }
724 603
725 CallContextImpl cc = new CallContextImpl( 604 ArtifactCallContext cc = new ArtifactCallContext(
726 artifact, CallContext.TOUCH, callMeta); 605 ArtifactDatabaseImpl.this,
606 CallContext.TOUCH,
607 callMeta,
608 context,
609 artifact);
727 610
728 try { 611 try {
729 return artifact.getArtifact().describe(data, cc); 612 return artifact.getArtifact().describe(data, cc);
730 } 613 }
731 finally { 614 finally {
745 628
746 if (artifact == null) { 629 if (artifact == null) {
747 throw new ArtifactDatabaseException(NO_SUCH_ARTIFACT); 630 throw new ArtifactDatabaseException(NO_SUCH_ARTIFACT);
748 } 631 }
749 632
750 CallContextImpl cc = new CallContextImpl( 633 ArtifactCallContext cc = new ArtifactCallContext(
751 artifact, CallContext.STORE, callMeta); 634 ArtifactDatabaseImpl.this,
635 CallContext.STORE,
636 callMeta,
637 context,
638 artifact);
752 639
753 try { 640 try {
754 return artifact.getArtifact().advance(target, cc); 641 return artifact.getArtifact().advance(target, cc);
755 } 642 }
756 finally { 643 finally {
766 653
767 if (artifact == null) { 654 if (artifact == null) {
768 throw new ArtifactDatabaseException(NO_SUCH_ARTIFACT); 655 throw new ArtifactDatabaseException(NO_SUCH_ARTIFACT);
769 } 656 }
770 657
771 CallContextImpl cc = new CallContextImpl( 658 ArtifactCallContext cc = new ArtifactCallContext(
772 artifact, CallContext.STORE, callMeta); 659 ArtifactDatabaseImpl.this,
660 CallContext.STORE,
661 callMeta,
662 context,
663 artifact);
773 664
774 try { 665 try {
775 return artifact.getArtifact().feed(data, cc); 666 return artifact.getArtifact().feed(data, cc);
776 } 667 }
777 finally { 668 finally {
967 catch (Exception e) { 858 catch (Exception e) {
968 logger.error(e.getLocalizedMessage(), e); 859 logger.error(e.getLocalizedMessage(), e);
969 throw new ArtifactDatabaseException(CREATION_FAILED); 860 throw new ArtifactDatabaseException(CREATION_FAILED);
970 } 861 }
971 862
972 CallContextImpl cc = new CallContextImpl( 863 ArtifactCallContext cc = new ArtifactCallContext(
973 persistentArtifact, CallContext.NOTHING, callMeta); 864 ArtifactDatabaseImpl.this,
865 CallContext.NOTHING,
866 callMeta,
867 context,
868 persistentArtifact);
974 869
975 try { 870 try {
976 return artifact.describe(input, cc); 871 return artifact.describe(input, cc);
977 } 872 }
978 finally { 873 finally {
1261 1156
1262 public Document describeCollection(String collectionId, CallMeta callMeta) 1157 public Document describeCollection(String collectionId, CallMeta callMeta)
1263 throws ArtifactDatabaseException 1158 throws ArtifactDatabaseException
1264 { 1159 {
1265 logger.debug("Describe collection: " + collectionId); 1160 logger.debug("Describe collection: " + collectionId);
1266 1161 ArtifactCollectionFactory acf = getArtifactCollectionFactory();
1267 throw new ArtifactDatabaseException("Not implemented yet."); 1162
1163 if (acf == null) {
1164 throw new ArtifactDatabaseException(NO_SUCH_FACTORY);
1165 }
1166
1167 UserFactory uf = getUserFactory();
1168 if (uf == null) {
1169 throw new ArtifactDatabaseException(NO_SUCH_FACTORY);
1170 }
1171
1172 ArtifactCollection c = backend.getCollection(
1173 collectionId, acf, uf, context);
1174
1175 if (c == null) {
1176 logger.warn("No collection found with identifier: " + collectionId);
1177 }
1178
1179 CallContext cc = new CollectionCallContext(
1180 ArtifactDatabaseImpl.this,
1181 CallContext.NOTHING,
1182 callMeta,
1183 context,
1184 c);
1185
1186 return c.describe(cc);
1268 } 1187 }
1269 1188
1270 public Document getCollectionAttribute(String collectionId, String artifactId, 1189 public Document getCollectionAttribute(String collectionId, String artifactId,
1271 CallMeta callMeta) throws ArtifactDatabaseException 1190 CallMeta callMeta) throws ArtifactDatabaseException
1272 { 1191 {

http://dive4elements.wald.intevation.org