Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/server/LoadArtifactServiceImpl.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/server/LoadArtifactServiceImpl.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.server; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.HashMap; | |
5 | |
6 import org.apache.log4j.Logger; | |
7 | |
8 import org.dive4elements.river.client.shared.exceptions.ServerException; | |
9 import org.dive4elements.river.client.shared.model.Artifact; | |
10 import org.dive4elements.river.client.shared.model.Collection; | |
11 import org.dive4elements.river.client.shared.model.Recommendation; | |
12 | |
13 import org.dive4elements.river.client.client.services.LoadArtifactService; | |
14 | |
15 /** | |
16 * This service creates a new Artifact based on a given Recommendation and puts | |
17 * this new artifact into a specified Collection. | |
18 * | |
19 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
20 */ | |
21 public class LoadArtifactServiceImpl | |
22 extends ArtifactServiceImpl | |
23 implements LoadArtifactService | |
24 { | |
25 private static final Logger logger = | |
26 Logger.getLogger(LoadArtifactServiceImpl.class); | |
27 | |
28 /** Error. */ | |
29 public static final String ERROR_LOAD_ARTIFACT = "error_load_artifact"; | |
30 | |
31 | |
32 /** | |
33 * Clones or creates a single artifact and adds it to a collection. | |
34 * | |
35 * Note that in contrast to loadMany, always the given factory is used | |
36 * to clone the artifact. | |
37 * | |
38 * @param parent collection to add recommendation to. | |
39 * @param recom recommendation to create clone for. | |
40 * @param factory factory to use. | |
41 * @param locale the locale to translate messages. | |
42 */ | |
43 public Artifact load( | |
44 Collection parent, | |
45 Recommendation recom, | |
46 String factory, | |
47 String locale | |
48 ) | |
49 throws ServerException { | |
50 logger.info( | |
51 "LoadArtifactServiceImpl.load: " + recom.getMasterArtifact()); | |
52 | |
53 String url = getServletContext().getInitParameter("server-url"); | |
54 | |
55 // 1) Clone the Artifact specified in >>recom<< | |
56 Artifact clone = ArtifactHelper.createArtifact( | |
57 url, locale, factory, recom); | |
58 | |
59 if (clone != null) { | |
60 logger.debug("Successfully create Artifact Clone. Add now!"); | |
61 Collection c = CollectionHelper.addArtifact( | |
62 parent, clone, url, locale); | |
63 | |
64 if (c != null) { | |
65 logger.debug("Successfully added Clone to Collection."); | |
66 | |
67 return clone; | |
68 } | |
69 } | |
70 | |
71 throw new ServerException(ERROR_LOAD_ARTIFACT); | |
72 } | |
73 | |
74 | |
75 /** | |
76 * Clone/create one or more artifacts and add it to a collection, avoiding | |
77 * duplicates. | |
78 * | |
79 * @param parent Collection where clones will be added to. | |
80 * @param recoms definitions of source of clone. | |
81 * @param factory name of factory to use when cloning artifacts (can be | |
82 * null in which case the recommendations getFactory() will | |
83 * be used. | |
84 * @param locale the locale to translate messages. | |
85 * | |
86 * @return cloned artifacts (same artifact might be contained multiple | |
87 * times). | |
88 */ | |
89 public Artifact[] loadMany( | |
90 Collection parent, | |
91 Recommendation[] recoms, | |
92 String factory, | |
93 String locale | |
94 ) | |
95 throws ServerException { | |
96 logger.debug("LoadArtifactServiceImpl.loadMany"); | |
97 | |
98 String url = getServletContext().getInitParameter("server-url"); | |
99 | |
100 ArrayList<Artifact> artifacts = new ArrayList<Artifact>(); | |
101 HashMap<Recommendation, Artifact> cloneMap = | |
102 new HashMap<Recommendation, Artifact>(); | |
103 | |
104 // TODO Respect the index of what to clone. | |
105 | |
106 // 1) Clone the Artifacts specified in >>recoms<< | |
107 for (Recommendation recom : recoms) { | |
108 // Do not do two clones of two identical recommendations. | |
109 Artifact prevClone = cloneMap.get(recom); | |
110 if (prevClone != null) { | |
111 // Already cloned a recommendation like this. | |
112 logger.debug("LoadArtifactServiceImpl: Avoid reclones, " | |
113 + "clone already exists."); | |
114 artifacts.add(prevClone); | |
115 } | |
116 else { | |
117 // Not already cloned. | |
118 String realFactory = factory != null | |
119 ? factory | |
120 : recom.getFactory(); | |
121 | |
122 logger.debug("One will be cloned with : " + realFactory); | |
123 | |
124 Artifact clone = ArtifactHelper.createArtifact( | |
125 url, locale, realFactory, recom); | |
126 | |
127 if (clone != null) { | |
128 logger.debug("LoadArtifactServiceImple: Successfully " | |
129 + "loaded Artifact Clone."); | |
130 Collection c = CollectionHelper.addArtifact( | |
131 parent, clone, url, locale); | |
132 | |
133 if (c != null) { | |
134 artifacts.add(clone); | |
135 // Remember we cloned a recommendation like this. | |
136 cloneMap.put(recom, clone); | |
137 } | |
138 else { | |
139 throw new ServerException(ERROR_LOAD_ARTIFACT); | |
140 } | |
141 } | |
142 } | |
143 } | |
144 return artifacts.toArray(new Artifact[artifacts.size()]); | |
145 } | |
146 } | |
147 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |