ingo@905: package de.intevation.flys.client.server; ingo@905: felix@913: import java.util.ArrayList; felix@1307: import java.util.HashMap; felix@913: ingo@905: import de.intevation.flys.client.shared.exceptions.ServerException; ingo@905: import de.intevation.flys.client.shared.model.Artifact; ingo@905: import de.intevation.flys.client.shared.model.Collection; ingo@905: import de.intevation.flys.client.shared.model.Recommendation; ingo@905: ingo@905: import de.intevation.flys.client.client.services.LoadArtifactService; ingo@905: ingo@905: /** ingo@905: * This service creates a new Artifact based on a given Recommendation and puts ingo@905: * this new artifact into a specified Collection. ingo@905: * ingo@905: * @author Ingo Weinzierl ingo@905: */ ingo@905: public class LoadArtifactServiceImpl ingo@905: extends ArtifactServiceImpl ingo@905: implements LoadArtifactService ingo@905: { ingo@905: public static final String ERROR_LOAD_ARTIFACT = "error_load_artifact"; ingo@905: ingo@905: public Artifact load( ingo@905: Collection parent, ingo@905: Recommendation recom, ingo@905: String factory, ingo@905: String url, ingo@905: String locale ingo@905: ) ingo@905: throws ServerException { ingo@905: System.out.println( ingo@905: "LoadArtifactServiceImpl.load: " + recom.getMasterArtifact()); ingo@905: ingo@905: // 1) Clone the Artifact specified in >>recom<< ingo@905: Artifact clone = ArtifactHelper.createArtifact( ingo@905: url, locale, factory, recom); ingo@905: ingo@905: if (clone != null) { ingo@905: System.out.println("Successfully create Artifact Clone. Add now!"); ingo@905: Collection c = CollectionHelper.addArtifact( ingo@905: parent, clone, url, locale); ingo@905: ingo@905: if (c != null) { ingo@905: System.out.println("Successfully added Clone to Collection."); ingo@905: ingo@905: return clone; ingo@905: } ingo@905: } ingo@905: ingo@905: throw new ServerException(ERROR_LOAD_ARTIFACT); ingo@905: } felix@913: felix@1272: felix@1272: /** felix@1307: * Clone one or more artifacts and add it to a collection, avoid duplicates. felix@1272: * felix@1272: * @param parent Collection where clones will be added to. felix@1272: * @param recommendations definitions of source of clone. felix@1272: * @param factory name of factory to use when cloning artifacts. felix@1307: * @return cloned artifacts (same artifact might be contained multiple felix@1307: * times). felix@1272: */ felix@913: public Artifact[] loadMany( felix@1272: Collection parent, felix@913: Recommendation[] recoms, felix@1272: String factory, felix@1272: String url, felix@1272: String locale felix@913: ) felix@913: throws ServerException { felix@1307: System.out.println("LoadArtifactServiceImpl.loadMany"); felix@913: ArrayList artifacts = new ArrayList(); felix@1307: HashMap cloneMap = felix@1307: new HashMap(); felix@913: felix@1272: // TODO Respect the index of what to clone. felix@1272: felix@1307: // 1) Clone the Artifacts specified in >>recoms<< felix@913: for (Recommendation recom : recoms) { felix@1307: // Do not do two clones of two identical recommendations. felix@1307: Artifact prevClone = cloneMap.get(recom); felix@1307: if (prevClone != null) { felix@1307: // Already cloned a recommendation like this. felix@1307: System.out.println("LoadArtifactServiceImpl: Avoid reclones, " felix@1307: + "clone already exists."); felix@1307: artifacts.add(prevClone); felix@1307: } felix@1307: else { felix@1307: // Not already cloned. felix@1307: Artifact clone = ArtifactHelper.createArtifact( felix@1307: url, locale, factory, recom); felix@1307: felix@1307: if (clone != null) { felix@1307: System.out.println("LoadArtifactServiceImple: Successfully " felix@1307: + "loaded Artifact Clone."); felix@1307: Collection c = CollectionHelper.addArtifact( felix@1307: parent, clone, url, locale); felix@1307: felix@1307: if (c != null) { felix@1307: artifacts.add(clone); felix@1307: // Remember we cloned a recommendation like this. felix@1307: cloneMap.put(recom, clone); felix@1307: } felix@1307: else { felix@1307: throw new ServerException(ERROR_LOAD_ARTIFACT); felix@1307: } felix@913: } felix@913: } felix@913: } felix@913: return artifacts.toArray(new Artifact[artifacts.size()]); felix@913: } ingo@905: } ingo@905: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :