view gwt-client/src/main/java/org/dive4elements/river/client/server/ModuleServiceImpl.java @ 8870:c26fb37899ca

Introduced groups for modules. Modules marked with the same group-id, will be put together in the ui. Also using now the localization info from the server instead of localizing the modules again on the client side.
author gernotbelger
date Wed, 07 Feb 2018 11:59:13 +0100
parents 5e38e2924c07
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.client.server;

import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException;
import org.dive4elements.artifacts.httpclient.http.HttpClient;
import org.dive4elements.artifacts.httpclient.http.HttpClientImpl;
import org.dive4elements.river.client.client.services.ModuleService;
import org.dive4elements.river.client.server.auth.User;
import org.dive4elements.river.client.shared.exceptions.ServerException;
import org.dive4elements.river.client.shared.model.DefaultModule;
import org.dive4elements.river.client.shared.model.Module;

import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPathConstants;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ModuleServiceImpl
extends      RemoteServiceServlet
implements   ModuleService
{
    private static final Logger log =
        Logger.getLogger(ModuleServiceImpl.class);

    public static final String XPATH_MODULES = "/art:modules/art:module";

    public static final String ERROR_NO_MODULES_FOUND =
        "error_no_module_found";

    @Override
    public Module[] list(final String locale) throws ServerException {
        final User user = this.getUser();

        log.info("ModuleService.list");

        final String url = getServletContext().getInitParameter("server-url");

        // create dummy xml
        final Document doc = XMLUtils.newDocument();

        final XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
            doc,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        final Element dummy = ec.create("modules");
        doc.appendChild(dummy);

        final HttpClient client = new HttpClientImpl(url, locale);
        try {
            final Document result = client.callService(url, "modules", doc);

            final NodeList list = (NodeList) XMLUtils.xpath(
                result,
                XPATH_MODULES,
                XPathConstants.NODESET,
                ArtifactNamespaceContext.INSTANCE);

            if (list == null) {
                log.warn("No modules found.");

                throw new ServerException(ERROR_NO_MODULES_FOUND);
            }

            final int num = list.getLength();

            final List<Module> modules = new ArrayList<Module>(list.getLength());
            for(int i =0; i < num; i++) {
                final Element em = (Element)list.item(i);
                final String name = em.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "name");
                final String localname = em.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "localname");
                final String strselected = em.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "selected");
                boolean selected = Boolean.parseBoolean(strselected); 
                final String group = em.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "groupId");
                final String groupLabel = em.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "groupLabel");

                final NodeList rivers = em.getChildNodes();
                final List<String> riverUuids = new ArrayList<String>(rivers.getLength());
                for (int j = 0; j < rivers.getLength(); j++) {
                    final Element re = (Element)rivers.item(j);
                    riverUuids.add(re.getAttribute("uuid"));
                }
                log.debug("Found module " + name + " " + localname);
                if (user == null || user.canUseFeature("module:" + name)) {
                    modules.add(new DefaultModule(name, localname, selected, group, groupLabel, riverUuids));
                }
            }
            return modules.toArray(new Module[modules.size()]);
        }
        catch (ConnectionException ce) {
            log.error(ce, ce);
        }

        throw new ServerException(ERROR_NO_MODULES_FOUND);
    }
}

// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 tw=80 :

http://dive4elements.wald.intevation.org