raimund@1285: package de.intevation.flys.client.server;
raimund@1285: 
raimund@1285: import org.w3c.dom.Document;
raimund@1285: import org.w3c.dom.Element;
raimund@1285: import org.w3c.dom.NodeList;
raimund@1285: 
ingo@1367: import org.apache.log4j.Logger;
ingo@1367: 
raimund@1285: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
raimund@1285: 
raimund@1285: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
raimund@1285: import de.intevation.artifacts.httpclient.http.HttpClient;
raimund@1285: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
raimund@1285: import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
raimund@1285: 
raimund@1285: import de.intevation.artifacts.common.ArtifactNamespaceContext;
raimund@1285: import de.intevation.artifacts.common.utils.XMLUtils;
raimund@1285: 
raimund@1285: import de.intevation.flys.client.shared.exceptions.ServerException;
raimund@1285: import de.intevation.flys.client.client.services.CollectionItemAttributeService;
raimund@1285: 
raimund@1285: import de.intevation.flys.client.shared.model.Collection;
raimund@1285: import de.intevation.flys.client.shared.model.CollectionItemAttribute;
raimund@1285: import de.intevation.flys.client.shared.model.Style;
raimund@1285: import de.intevation.flys.client.shared.model.StyleSetting;
raimund@1285: 
raimund@1285: 
raimund@1285: /**
raimund@1285:  *
raimund@1285:  * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
raimund@1285:  */
raimund@1285: public class CollectionItemAttributeServiceImpl
raimund@1285: extends      RemoteServiceServlet
raimund@1285: implements   CollectionItemAttributeService
raimund@1285: {
ingo@1367:     private static final Logger logger =
ingo@1367:         Logger.getLogger(CollectionItemAttributeServiceImpl.class);
ingo@1367: 
ingo@1367: 
raimund@1285:     public static final String XPATH_RESULT = "/art:result/text()";
raimund@1285: 
raimund@1285:     public static final String OPERATION_FAILURE = "FAILED";
raimund@1285: 
ingo@1310:     public static final String ERROR_NO_STYLES_FOUND =
ingo@1310:         "error_no_theme_styles_found";
ingo@1310: 
raimund@1285: 
raimund@1285:     public CollectionItemAttribute getCollectionItemAttribute(
raimund@1285:         Collection collection,
raimund@1285:         String artifact,
raimund@1285:         String locale)
raimund@1285:     throws ServerException
raimund@1285:     {
ingo@1367:         logger.info(
raimund@1292:             "CollectionItemAttributeServiceImpl.getCollectionItemAttribute");
raimund@1285: 
raimund@1425:         String url  = getServletContext().getInitParameter("server-url");
raimund@1425: 
raimund@1285:         Document requestDoc = XMLUtils.newDocument();
raimund@1285: 
raimund@1285:         XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
felix@1442:             requestDoc,
felix@1442:             ArtifactNamespaceContext.NAMESPACE_URI,
felix@1442:             ArtifactNamespaceContext.NAMESPACE_PREFIX);
raimund@1285: 
raimund@1285:         Element action = ec.create("action");
raimund@1285: 
raimund@1285:         Element type = ec.create("type");
raimund@1285:         ec.addAttr(type, "name", "getitemattribute", false);
raimund@1285: 
raimund@1285:         Element art = ec.create("artifact");
raimund@1285:         ec.addAttr(art, "uuid", artifact, false);
raimund@1285: 
raimund@1285:         type.appendChild(art);
raimund@1285:         action.appendChild(type);
raimund@1285:         requestDoc.appendChild (action);
raimund@1285: 
raimund@1285:         try {
raimund@1285:             HttpClient client = new HttpClientImpl(url, locale);
raimund@1285:             Document res = (Document) client.doCollectionAction(
raimund@1285:                 requestDoc,
raimund@1285:                 collection.identifier(),
raimund@1285:                 new DocumentResponseHandler());
raimund@1292:             return readXML (res, artifact);
raimund@1285:         }
raimund@1285:         catch (ConnectionException ce) {
ingo@1367:             logger.error(ce, ce);
raimund@1285:         }
ingo@1310: 
ingo@1310:         throw new ServerException(ERROR_NO_STYLES_FOUND);
raimund@1285:     }
raimund@1285: 
raimund@1292: 
raimund@1292:     public void setCollectionItemAttribute(
raimund@1292:         Collection collection,
raimund@1292:         String artifact,
raimund@1292:         String locale,
raimund@1292:         CollectionItemAttribute attributes)
raimund@1292:     throws ServerException
raimund@1292:     {
ingo@1367:         logger.info(
raimund@1292:             "CollectionItemAttributeServiceImpl.setCollectionItemAttribute");
raimund@1292: 
raimund@1425:         String url  = getServletContext().getInitParameter("server-url");
raimund@1425: 
raimund@1292:         Document doc = writeXML(attributes, artifact);
raimund@1292: 
raimund@1292:         try {
raimund@1292:             HttpClient client = new HttpClientImpl(url, locale);
raimund@1292:             Document res = (Document) client.doCollectionAction(
raimund@1292:                 doc,
raimund@1292:                 collection.identifier(),
raimund@1292:                 new DocumentResponseHandler());
raimund@1292: 
raimund@1292:             return;
raimund@1292:         }
raimund@1292:         catch (ConnectionException ce) {
ingo@1367:             logger.error(ce, ce);
ingo@1367:             throw new ServerException(ce.getLocalizedMessage());
raimund@1292:         }
raimund@1292:     }
raimund@1292: 
ingo@1310:     protected CollectionItemAttribute readXML(Document doc, String artifact)
ingo@1310:     throws    ServerException
ingo@1310:     {
raimund@1285:         CollectionItemAttribute cia = new CollectionItemAttribute();
raimund@1292:         cia.setArtifact(artifact);
raimund@1285: 
raimund@1285:         Element root = doc.getDocumentElement();
raimund@1285:         NodeList themes = root.getElementsByTagName("art:themes");
raimund@1285: 
ingo@1310:         if (themes == null || themes.getLength() == 0) {
ingo@1310:             throw new ServerException(ERROR_NO_STYLES_FOUND);
raimund@1285:         }
raimund@1285: 
raimund@1285:         Element e = (Element) themes.item(0);
raimund@1285:         NodeList items = e.getElementsByTagName("theme");
raimund@1285: 
raimund@1285:         for (int i = 0; i < items.getLength(); i++) {
ingo@2908:             Style s = StyleHelper.getStyle ((Element) items.item(i));
raimund@1326:             if(s == null) {
raimund@1326:                 throw new ServerException(ERROR_NO_STYLES_FOUND);
raimund@1326:             }
raimund@1326:             else {
raimund@1326:                 cia.appendStyle(s);
raimund@1326:             }
raimund@1285:         }
raimund@1285: 
raimund@1285:         return cia;
raimund@1285:     }
raimund@1285: 
raimund@1285: 
raimund@1292:     protected Document writeXML (
raimund@1292:         CollectionItemAttribute attributes,
raimund@1292:         String artifact)
raimund@1292:     {
raimund@1292:         Document styles = XMLUtils.newDocument();
raimund@1292: 
raimund@1292:         XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
raimund@1292:                 styles,
raimund@1292:                 ArtifactNamespaceContext.NAMESPACE_URI,
raimund@1292:                 ArtifactNamespaceContext.NAMESPACE_PREFIX);
raimund@1292:         Element action = ec.create("action");
raimund@1292:         Element type = ec.create("type");
raimund@1292:         type.setAttribute("name", "setitemattribute");
raimund@1292:         Element art = ec.create("artifact");
raimund@1292:         art.setAttribute("uuid", artifact);
raimund@1292:         Element attr = ec.create("attribute");
raimund@1292:         Element themes = ec.create("themes");
raimund@1292:         action.appendChild(type);
raimund@1292:         type.appendChild(art);
raimund@1292:         art.appendChild(attr);
raimund@1292:         attr.appendChild(themes);
raimund@1292: 
raimund@1292:         XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
raimund@1292:                 styles,
raimund@1292:                 "",
raimund@1292:                 "");
raimund@1292: 
raimund@1292:         for (int i = 0; i < attributes.getNumStyles(); i++) {
raimund@1292:             Style s = attributes.getStyle(i);
raimund@1292:             Element theme = creator.create("theme");
raimund@1292:             theme.setAttribute("name", s.getName());
raimund@1292:             theme.setAttribute("facet", s.getFacet());
raimund@1329:             theme.setAttribute("index", String.valueOf(s.getIndex()));
raimund@1292:             for (int j = 0; j < s.getNumSettings(); j++) {
raimund@1292:                 StyleSetting set = s.getSetting(j);
raimund@1292:                 Element field = creator.create("field");
raimund@1292:                 field.setAttribute("name", set.getName());
raimund@1292:                 field.setAttribute("display", set.getDisplayName());
raimund@1292:                 field.setAttribute("default", set.getDefaultValue());
raimund@1292:                 field.setAttribute("hints", set.getHints());
raimund@1292:                 field.setAttribute("type", set.getType());
ingo@4284:                 field.setAttribute("hidden", String.valueOf(set.isHidden()));
raimund@1292:                 theme.appendChild(field);
raimund@1292:             }
raimund@1292:             themes.appendChild(theme);
raimund@1292:         }
raimund@1292:         styles.appendChild(action);
raimund@1292:         return styles;
raimund@1292:     }
raimund@1285: }
raimund@1285: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :