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: 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.Artifact; 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 Raimund Renkert raimund@1285: */ raimund@1285: public class CollectionItemAttributeServiceImpl raimund@1285: extends RemoteServiceServlet raimund@1285: implements CollectionItemAttributeService raimund@1285: { raimund@1285: public static final String XPATH_RESULT = "/art:result/text()"; raimund@1285: raimund@1285: public static final String OPERATION_FAILURE = "FAILED"; raimund@1285: raimund@1285: raimund@1285: public CollectionItemAttribute getCollectionItemAttribute( raimund@1285: Collection collection, raimund@1285: String artifact, raimund@1285: String url, raimund@1285: String locale) raimund@1285: throws ServerException raimund@1285: { raimund@1285: System.out.println( raimund@1292: "CollectionItemAttributeServiceImpl.getCollectionItemAttribute"); raimund@1285: raimund@1285: Document requestDoc = XMLUtils.newDocument(); raimund@1285: raimund@1285: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( raimund@1285: requestDoc, raimund@1285: ArtifactNamespaceContext.NAMESPACE_URI, raimund@1285: 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) { raimund@1285: System.err.println(ce.getLocalizedMessage()); raimund@1285: } raimund@1285: throw new ServerException(""); raimund@1285: } raimund@1285: raimund@1292: raimund@1292: public void setCollectionItemAttribute( raimund@1292: Collection collection, raimund@1292: String artifact, raimund@1292: String url, raimund@1292: String locale, raimund@1292: CollectionItemAttribute attributes) raimund@1292: throws ServerException raimund@1292: { raimund@1292: System.out.println( raimund@1292: "CollectionItemAttributeServiceImpl.setCollectionItemAttribute"); raimund@1292: 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) { raimund@1292: System.err.println(ce.getLocalizedMessage()); raimund@1292: } raimund@1292: throw new ServerException(""); raimund@1292: } raimund@1292: raimund@1292: protected CollectionItemAttribute readXML (Document doc, String artifact) { 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: raimund@1285: if (themes.getLength() != 1) { raimund@1285: return null; 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++) { raimund@1285: cia.appendStyle(getStyle ((Element) items.item(i))); 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@1292: theme.setAttribute("index", String.valueOf(i)); 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()); 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@1292: raimund@1292: raimund@1285: protected Style getStyle (Element element) { raimund@1285: raimund@1285: if (!element.getTagName().equals("theme")) { raimund@1285: return null; raimund@1285: } raimund@1285: raimund@1285: NodeList list = element.getElementsByTagName("field"); raimund@1285: Style style = new Style(); raimund@1292: raimund@1285: style.setName (element.getAttribute("name")); raimund@1292: style.setFacet (element.getAttribute("facet")); raimund@1285: for(int i = 0; i < list.getLength(); i++) { raimund@1285: Element e = (Element) list.item(i); raimund@1285: StyleSetting set = new StyleSetting ( raimund@1285: e.getAttribute("name"), raimund@1285: e.getAttribute("default"), raimund@1285: e.getAttribute("display"), raimund@1285: e.getAttribute("hints"), raimund@1285: e.getAttribute("type")); raimund@1285: style.appendStyleSetting(set); raimund@1285: } raimund@1285: return style; raimund@1285: } raimund@1285: } raimund@1285: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :