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@1367: import org.apache.log4j.Logger; ingo@1367: 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@1367: private static final Logger logger = ingo@1367: Logger.getLogger(LoadArtifactServiceImpl.class); ingo@1367: ingo@1367: ingo@905: public static final String ERROR_LOAD_ARTIFACT = "error_load_artifact"; ingo@905: felix@1346: felix@1346: /** felix@1346: * Clones or creates a single artifact and adds it to a collection. felix@1346: * felix@1346: * Note that in contrast to loadMany, always the given factory is used felix@1346: * to clone the artifact. felix@1346: * felix@1346: * @param parent collection to add recommendation to. felix@1346: * @param recom recommendation to create clone for. felix@1346: * @param factory factory to use. felix@1346: * @param url servers url. felix@1346: * @param locale the locale to translate messages. felix@1346: */ 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@1367: logger.info( 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@1367: logger.debug("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@1367: logger.debug("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@1346: * Clone/create one or more artifacts and add it to a collection, avoiding felix@1346: * duplicates. felix@1272: * felix@1346: * @param parent Collection where clones will be added to. felix@1346: * @param recoms definitions of source of clone. felix@1346: * @param factory name of factory to use when cloning artifacts (can be felix@1346: * null in which case the recommendations getFactory() will felix@1346: * be used. felix@1346: * @param url servers url. felix@1346: * @param locale the locale to translate messages. felix@1346: * 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 locale felix@913: ) felix@913: throws ServerException { ingo@1367: logger.debug("LoadArtifactServiceImpl.loadMany"); felix@1346: ingo@1413: String url = getServletContext().getInitParameter("server-url"); ingo@1413: 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. ingo@1367: logger.debug("LoadArtifactServiceImpl: Avoid reclones, " felix@1307: + "clone already exists."); felix@1307: artifacts.add(prevClone); felix@1307: } felix@1307: else { felix@1307: // Not already cloned. felix@1346: String realFactory = factory != null felix@1346: ? factory felix@1346: : recom.getFactory(); felix@1346: ingo@1367: logger.debug("One will be cloned with : " + realFactory); felix@1346: felix@1307: Artifact clone = ArtifactHelper.createArtifact( felix@1346: url, locale, realFactory, recom); felix@1307: felix@1307: if (clone != null) { ingo@1367: logger.debug("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 :