9127
|
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde |
|
2 * Software engineering by |
|
3 * Björnsen Beratende Ingenieure GmbH |
|
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt |
|
5 * |
|
6 * This file is Free Software under the GNU AGPL (>=v3) |
|
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
|
8 * documentation coming with Dive4Elements River for details. |
|
9 */ |
|
10 package test; |
|
11 |
9148
|
12 import java.util.ArrayList; |
|
13 import java.util.HashMap; |
|
14 |
|
15 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; |
|
16 import org.dive4elements.river.client.client.ui.NilDatacageTwinPanelInfo; |
|
17 import org.dive4elements.river.client.client.ui.RecommandationUtils; |
|
18 import org.dive4elements.river.client.server.ArtifactHelper; |
|
19 import org.dive4elements.river.client.server.CollectionHelper; |
|
20 import org.dive4elements.river.client.server.LoadArtifactServiceImpl; |
|
21 import org.dive4elements.river.client.shared.exceptions.ServerException; |
|
22 import org.dive4elements.river.client.shared.model.Artifact; |
|
23 import org.dive4elements.river.client.shared.model.Collection; |
|
24 import org.dive4elements.river.client.shared.model.Recommendation; |
|
25 |
9127
|
26 /** |
|
27 * @author Domenico Nardi Tironi |
|
28 * |
|
29 */ |
|
30 public class SimpleRecommendation { |
|
31 |
|
32 // entspricht den Bezeichnungen im Datacage-Export |
|
33 private final String factory; |
|
34 private final String ids; |
|
35 private final String target; |
9148
|
36 private String displayName; |
9127
|
37 |
|
38 public SimpleRecommendation(final String factory, final String ids, final String target) { |
|
39 this.factory = factory; |
|
40 this.ids = ids; |
|
41 this.target = target; |
|
42 } |
|
43 |
9148
|
44 public SimpleRecommendation(final String factory, final String ids, final String target, final String displayName) { |
|
45 this(factory, ids, target); |
|
46 this.displayName = displayName; |
|
47 } |
|
48 |
9127
|
49 public String getIds() { |
|
50 return this.ids; |
|
51 } |
|
52 |
|
53 public String getTarget() { |
|
54 return this.target; |
|
55 } |
|
56 |
|
57 public String getFactory() { |
|
58 return this.factory; |
|
59 } |
|
60 |
9148
|
61 private String getDisplayName() { |
|
62 return this.displayName; |
|
63 } |
|
64 |
|
65 public final String getRecommendationPairString(final SimpleRecommendation rec2, final Collection collection, final String serverUrl, final String locale) |
|
66 throws ConnectionException, ServerException { |
|
67 final Recommendation recom1 = new Recommendation(this.getFactory(), this.getIds(), this.getTarget()); |
|
68 recom1.setDisplayName(this.displayName); |
|
69 final Recommendation recom2 = new Recommendation(rec2.getFactory(), rec2.getIds(), rec2.getTarget()); |
|
70 recom2.setDisplayName(rec2.getDisplayName()); |
|
71 final Artifact[] artifacts = loadMany(new Recommendation[] { recom1, recom2 }, null, collection, serverUrl, locale); |
9226
|
72 final String rec1String = RecommandationUtils.createDataString(artifacts[0].getUuid(), recom1, new NilDatacageTwinPanelInfo("xxxx", "yyy")); |
|
73 final String rec2String = RecommandationUtils.createDataString(artifacts[1].getUuid(), recom2, new NilDatacageTwinPanelInfo("xxxx", "yyy")); |
9148
|
74 final String combinedIdNeu = rec1String + "#" + rec2String; |
|
75 return combinedIdNeu; |
|
76 |
|
77 } |
|
78 |
|
79 private final Artifact[] loadMany(final Recommendation[] recoms, final String factory, final Collection collection, final String serverUrl, |
|
80 final String locale) throws ServerException, ConnectionException { |
|
81 final ArrayList<Artifact> artifacts = new ArrayList<Artifact>(); |
|
82 final HashMap<Recommendation, Artifact> cloneMap = new HashMap<Recommendation, Artifact>(); |
|
83 |
|
84 for (final Recommendation recom : recoms) { |
|
85 |
|
86 final Artifact prevClone = cloneMap.get(recom); |
|
87 if (prevClone != null) { |
|
88 |
|
89 artifacts.add(prevClone); |
|
90 } else { |
|
91 // Not already cloned. |
|
92 final String realFactory = factory != null ? factory : recom.getFactory(); |
|
93 |
|
94 final Artifact clone = ArtifactHelper.createArtifact(serverUrl, locale, realFactory, recom); |
|
95 |
|
96 if (clone != null) { |
|
97 final Collection c = CollectionHelper.addArtifact(collection, clone, serverUrl, locale); |
|
98 |
|
99 if (c != null) { |
|
100 artifacts.add(clone); |
|
101 // Remember we cloned a recommendation like this. |
|
102 cloneMap.put(recom, clone); |
|
103 } else { |
|
104 throw new ServerException(LoadArtifactServiceImpl.ERROR_LOAD_ARTIFACT); |
|
105 } |
|
106 } |
|
107 } |
|
108 } |
|
109 return artifacts.toArray(new Artifact[artifacts.size()]); |
|
110 |
|
111 } |
|
112 |
9127
|
113 } |