Example usage for org.w3c.dom Document getFirstChild

List of usage examples for org.w3c.dom Document getFirstChild

Introduction

In this page you can find the example usage for org.w3c.dom Document getFirstChild.

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:se.unlogic.hierarchy.foregroundmodules.systemadmin.SystemAdminModule.java

@WebPublic(alias = "updatefiltermodule")
public SimpleForegroundModuleResponse updateFilterModule(HttpServletRequest req, HttpServletResponse res,
        User user, URIParser uriParser) throws URINotFoundException, SQLException, IOException {

    SimpleFilterModuleDescriptor simpleModuleDescriptor = null;

    if (uriParser.size() == 3 && NumberUtils.isInt(uriParser.get(2))
            && (simpleModuleDescriptor = this.filterModuleDAO
                    .getModule(Integer.parseInt(uriParser.get(2)))) != null) {

        ValidationException validationException = null;

        if (req.getMethod().equalsIgnoreCase("POST")) {

            // Populate module descriptor
            List<ValidationError> validationErrors = new ArrayList<ValidationError>();

            try {
                simpleModuleDescriptor = FILTER_MODULE_DESCRIPTOR_POPULATOR.populate(simpleModuleDescriptor,
                        req);//from www  .j av  a 2  s  . com

            } catch (ValidationException e) {
                validationErrors.addAll(e.getErrors());
            }

            // Check module specific settings
            // TODO handle runtime exceptions...
            FilterModule moduleInstance = systemInterface.getFilterModuleCache()
                    .getModule(simpleModuleDescriptor);

            HashMap<String, List<String>> settingValues = null;

            if (moduleInstance != null) {

                settingValues = parseModuleSettings(moduleInstance.getSettings(), req, validationErrors);
            }

            if (validationErrors.isEmpty()) {
                this.log.info("User " + user + " updating filter module " + simpleModuleDescriptor);

                //Only update module settings if the module is started
                if (moduleInstance != null) {
                    simpleModuleDescriptor.setMutableSettingHandler(new SimpleSettingHandler(settingValues));
                }

                this.filterModuleDAO.update(simpleModuleDescriptor);

                // Check if the module is cached
                if (systemInterface.getFilterModuleCache().isCached(simpleModuleDescriptor)) {

                    // Module is cached update it
                    try {
                        systemInterface.getFilterModuleCache().update(simpleModuleDescriptor);
                    } catch (Exception e) {
                        this.log.error(
                                "Error updating filter module while updating module requested by user " + user,
                                e);
                    }
                }

                res.sendRedirect(this.getModuleURI(req));
                return null;
            } else {
                validationException = new ValidationException(validationErrors);
            }

        }

        Document doc = this.createDocument(req, uriParser);
        Element updateModuleElement = doc.createElement("updateFilterModule");
        doc.getFirstChild().appendChild(updateModuleElement);

        FilterModule moduleInstance = systemInterface.getFilterModuleCache().getModule(simpleModuleDescriptor);

        // check if the module is cached
        if (moduleInstance != null) {
            updateModuleElement.setAttribute("started", "true");

            // Get module specific settings
            List<? extends SettingDescriptor> moduleSettings = moduleInstance.getSettings();

            if (moduleSettings != null && !moduleSettings.isEmpty()) {

                Element moduleSettingDescriptorsElement = doc.createElement("moduleSettingDescriptors");
                updateModuleElement.appendChild(moduleSettingDescriptorsElement);

                for (SettingDescriptor settingDescriptor : moduleSettings) {
                    moduleSettingDescriptorsElement.appendChild(settingDescriptor.toXML(doc));

                    rewriteURLs(simpleModuleDescriptor, settingDescriptor, req);
                }
            }
        }

        updateModuleElement.appendChild(simpleModuleDescriptor.toXML(doc, true, false));

        updateModuleElement.appendChild(this.getDataSources(doc));
        updateModuleElement.appendChild(getPathTypes(doc));

        if (validationException != null) {
            updateModuleElement.appendChild(validationException.toXML(doc));
            updateModuleElement.appendChild(RequestUtils.getRequestParameters(req, doc));
        }

        AccessUtils.appendAllowedGroupsAndUsers(doc, updateModuleElement, simpleModuleDescriptor,
                systemInterface.getUserHandler(), systemInterface.getGroupHandler());

        return new SimpleForegroundModuleResponse(doc, moduleDescriptor.getName(), this.getDefaultBreadcrumb(),
                getModuleBreadcrumb(req, simpleModuleDescriptor, "updatefiltermodule",
                        updateFilterModuleBreadCrumbText));
    } else {
        throw new URINotFoundException(uriParser);
    }
}

From source file:se.unlogic.hierarchy.foregroundmodules.systemadmin.SystemAdminModule.java

@WebPublic(alias = "addfmodule")
public SimpleForegroundModuleResponse addForegroundModule(HttpServletRequest req, HttpServletResponse res,
        User user, URIParser uriParser) throws URINotFoundException, SQLException, IOException {

    SimpleSectionDescriptor simpleSectionDescriptor = null;

    if (uriParser.size() == 3 && NumberUtils.isInt(uriParser.get(2))
            && (simpleSectionDescriptor = this.sectionDAO.getSection(Integer.parseInt(uriParser.get(2)),
                    false)) != null) {//from   w ww.  j a va 2s. c  o  m

        ValidationException validationException = null;

        if (req.getMethod().equalsIgnoreCase("POST")) {

            try {
                SimpleForegroundModuleDescriptor simpleModuleDescriptor = FOREGROUND_MODULE_DESCRIPTOR_POPULATOR
                        .populate(req);
                simpleModuleDescriptor.setSectionID(simpleSectionDescriptor.getSectionID());

                SimpleForegroundModuleDescriptor aliasMatch = this.foregroundModuleDAO
                        .getModule(simpleSectionDescriptor.getSectionID(), simpleModuleDescriptor.getAlias());

                if (aliasMatch != null) {
                    throw new ValidationException(
                            new ValidationError("alias", ValidationErrorType.Other, "duplicateModuleAlias"));
                }

                this.log.info("User " + user + " adding foreground module " + simpleModuleDescriptor
                        + " to section " + simpleSectionDescriptor);

                this.foregroundModuleDAO.add(simpleModuleDescriptor);

                res.sendRedirect(this.getModuleURI(req));
                return null;

            } catch (ValidationException e) {
                validationException = e;
            }
        }

        Document doc = this.createDocument(req, uriParser);
        Element addModuleElement = doc.createElement("addForegroundModule");
        doc.getFirstChild().appendChild(addModuleElement);

        addModuleElement.appendChild(simpleSectionDescriptor.toXML(doc));

        addModuleElement.appendChild(this.getDataSources(doc));
        addModuleElement.appendChild(getPathTypes(doc));
        addModuleElement.appendChild(HTTPProtocol.getProtocols(doc));

        if (validationException != null) {
            addModuleElement.appendChild(validationException.toXML(doc));
            addModuleElement.appendChild(RequestUtils.getRequestParameters(req, doc));
        }

        return new SimpleForegroundModuleResponse(doc, moduleDescriptor.getName(), this.getDefaultBreadcrumb(),
                new Breadcrumb(this.addForegroundModuleBreadCrumbText, this.addForegroundModuleBreadCrumbText,
                        getFullAlias() + "/addModule/" + simpleSectionDescriptor.getSectionID()));
    } else {
        throw new URINotFoundException(uriParser);
    }
}

From source file:se.unlogic.hierarchy.foregroundmodules.systemadmin.SystemAdminModule.java

@WebPublic(alias = "updatefmodule")
public SimpleForegroundModuleResponse updateForegroundModule(HttpServletRequest req, HttpServletResponse res,
        User user, URIParser uriParser) throws URINotFoundException, SQLException, IOException {

    SimpleForegroundModuleDescriptor simpleModuleDescriptor = null;

    if (uriParser.size() == 3 && NumberUtils.isInt(uriParser.get(2))
            && (simpleModuleDescriptor = this.foregroundModuleDAO
                    .getModule(Integer.parseInt(uriParser.get(2)))) != null) {

        ValidationException validationException = null;

        if (req.getMethod().equalsIgnoreCase("POST")) {

            // Populate module descriptor
            List<ValidationError> validationErrors = new ArrayList<ValidationError>();

            try {
                simpleModuleDescriptor = FOREGROUND_MODULE_DESCRIPTOR_POPULATOR.populate(simpleModuleDescriptor,
                        req);/*  ww  w  . j  a va 2  s  .  com*/
            } catch (ValidationException e) {
                validationErrors.addAll(e.getErrors());
            }

            // Check for alias dupes
            SimpleForegroundModuleDescriptor aliasMatch = this.foregroundModuleDAO
                    .getModule(simpleModuleDescriptor.getSectionID(), simpleModuleDescriptor.getAlias());

            if (aliasMatch != null && !aliasMatch.getModuleID().equals(simpleModuleDescriptor.getModuleID())) {
                validationErrors
                        .add(new ValidationError("alias", ValidationErrorType.Other, "duplicateModuleAlias"));
            }

            // Check module specific settings
            // TODO handle runtime exceptions...
            ForegroundModule moduleInstance = this.getForegroundModuleInstance(simpleModuleDescriptor);

            HashMap<String, List<String>> settingValues = null;

            if (moduleInstance != null) {

                settingValues = parseModuleSettings(moduleInstance.getSettings(), req, validationErrors);
            }

            if (validationErrors.isEmpty()) {
                this.log.info("User " + user + " updating foreground module " + simpleModuleDescriptor);

                //Only update module settings if the module is started
                if (moduleInstance != null) {
                    simpleModuleDescriptor.setMutableSettingHandler(new SimpleSettingHandler(settingValues));
                }

                this.foregroundModuleDAO.update(simpleModuleDescriptor);

                this.systemInterface.getEventHandler().sendEvent(SimpleForegroundModuleDescriptor.class,
                        new CRUDEvent<SimpleForegroundModuleDescriptor>(CRUDAction.UPDATE,
                                simpleModuleDescriptor),
                        EventTarget.ALL);

                // Check if the module is cached
                SectionInterface sectionInterface = systemInterface
                        .getSectionInterface(simpleModuleDescriptor.getSectionID());

                if (sectionInterface != null
                        && sectionInterface.getForegroundModuleCache().isCached(simpleModuleDescriptor)) {

                    // Module is cached update it
                    try {
                        sectionInterface.getForegroundModuleCache().update(simpleModuleDescriptor);
                    } catch (Exception e) {
                        this.log.error("Error updating foreground module " + simpleModuleDescriptor
                                + " in section " + sectionInterface.getSectionDescriptor()
                                + " while updating module requested by user " + user, e);
                    }
                }

                res.sendRedirect(this.getModuleURI(req));
                return null;
            } else {
                validationException = new ValidationException(validationErrors);
            }

        }

        Document doc = this.createDocument(req, uriParser);
        Element updateModuleElement = doc.createElement("updateForegroundModule");
        doc.getFirstChild().appendChild(updateModuleElement);

        ForegroundModule moduleInstance = this.getForegroundModuleInstance(simpleModuleDescriptor);

        // check if the module is cached
        if (moduleInstance != null) {
            updateModuleElement.setAttribute("started", "true");

            // Get module specific settings
            List<? extends SettingDescriptor> moduleSettings = moduleInstance.getSettings();

            if (moduleSettings != null && !moduleSettings.isEmpty()) {

                Element moduleSettingDescriptorsElement = doc.createElement("moduleSettingDescriptors");
                updateModuleElement.appendChild(moduleSettingDescriptorsElement);

                for (SettingDescriptor settingDescriptor : moduleSettings) {
                    moduleSettingDescriptorsElement.appendChild(settingDescriptor.toXML(doc));

                    rewriteURLs(simpleModuleDescriptor, settingDescriptor, req);
                }
            }
        }

        updateModuleElement.appendChild(simpleModuleDescriptor.toXML(doc, true, false));

        updateModuleElement.appendChild(this.getDataSources(doc));
        updateModuleElement.appendChild(getPathTypes(doc));
        updateModuleElement.appendChild(HTTPProtocol.getProtocols(doc));

        if (validationException != null) {
            updateModuleElement.appendChild(validationException.toXML(doc));
            updateModuleElement.appendChild(RequestUtils.getRequestParameters(req, doc));
        }

        AccessUtils.appendAllowedGroupsAndUsers(doc, updateModuleElement, simpleModuleDescriptor,
                systemInterface.getUserHandler(), systemInterface.getGroupHandler());

        return new SimpleForegroundModuleResponse(doc, moduleDescriptor.getName(), this.getDefaultBreadcrumb(),
                getModuleBreadcrumb(req, simpleModuleDescriptor, "updateModule",
                        updateForegroundModuleBreadCrumbText));
    } else {
        throw new URINotFoundException(uriParser);
    }
}

From source file:se.unlogic.hierarchy.foregroundmodules.systemadmin.SystemAdminModule.java

@WebPublic(alias = "movefmodule")
public SimpleForegroundModuleResponse moveForegroundModule(HttpServletRequest req, HttpServletResponse res,
        User user, URIParser uriParser) throws SQLException, URINotFoundException, IOException {

    SimpleForegroundModuleDescriptor module = null;

    if ((uriParser.size() == 3 || uriParser.size() == 4) && NumberUtils.isInt(uriParser.get(2))
            && (module = this.foregroundModuleDAO.getModule(Integer.parseInt(uriParser.get(2)))) != null) {

        if (uriParser.size() == 3) {

            // Show section tree
            Document doc = this.createDocument(req, uriParser);
            Element moveModuleElement = doc.createElement("moveForegroundModule");
            doc.getFirstChild().appendChild(moveModuleElement);

            moveModuleElement.appendChild(module.toXML(doc));

            Element sectionsElement = doc.createElement("sections");
            moveModuleElement.appendChild(sectionsElement);

            this.appendSection(sectionsElement, doc, this.sectionDAO.getRootSection(true), false);

            return new SimpleForegroundModuleResponse(doc, moduleDescriptor.getName(),
                    this.getDefaultBreadcrumb(),
                    getModuleBreadcrumb(req, module, "moveModule", moveForegroundModuleBreadCrumbText));
        } else {/*from   w ww .j a va 2s.  com*/

            SimpleSectionDescriptor simpleSectionDescriptor = null;

            if (NumberUtils.isInt(uriParser.get(3)) && (simpleSectionDescriptor = this.sectionDAO
                    .getSection(Integer.parseInt(uriParser.get(3)), true)) != null) {

                if (module.getSectionID() == simpleSectionDescriptor.getSectionID()) {

                    this.log.info("User " + user + " trying to move foreground module " + module
                            + " to the section it already belongs to, ignoring move");

                    res.sendRedirect(this.getModuleURI(req));

                    return null;

                } else {

                    // TODO check alias

                    this.log.info("User " + user + " moving foreground module " + module + " to section "
                            + simpleSectionDescriptor);

                    // Update module in database
                    Integer oldSectionID = module.getSectionID();
                    module.setSectionID(simpleSectionDescriptor.getSectionID());
                    this.foregroundModuleDAO.update(module);

                    // Check if the section the that the module belonged to is cached
                    SectionInterface oldSectionInterface = systemInterface.getSectionInterface(oldSectionID);

                    boolean enabled = false;

                    if (oldSectionInterface != null
                            && oldSectionInterface.getForegroundModuleCache().isCached(module)) {

                        enabled = true;

                        try {
                            oldSectionInterface.getForegroundModuleCache().unload(module);
                        } catch (Exception e) {
                            this.log.error("Error unloading foreground module " + module + " from section "
                                    + oldSectionInterface.getSectionDescriptor()
                                    + " while moving module to section " + simpleSectionDescriptor + " by user "
                                    + user, e);
                        }
                    }

                    // If the module was enabled and the reciving section is cached then cache the module there
                    if (enabled) {
                        SectionInterface newSectionInterface = systemInterface
                                .getSectionInterface(module.getSectionID());

                        if (newSectionInterface != null) {
                            try {
                                newSectionInterface.getForegroundModuleCache().cache(module);
                            } catch (Exception e) {
                                this.log.error("Error caching foreground module " + module + " in section "
                                        + simpleSectionDescriptor + " while moving module from section "
                                        + oldSectionInterface.getSectionDescriptor() + " by user " + user, e);
                            }
                        }
                    }

                    res.sendRedirect(this.getModuleURI(req));

                    return null;
                }
            }
        }
    }

    throw new URINotFoundException(uriParser);
}

From source file:se.unlogic.hierarchy.foregroundmodules.systemadmin.SystemAdminModule.java

@WebPublic(alias = "copyfmodule")
public SimpleForegroundModuleResponse copyForegroundModule(HttpServletRequest req, HttpServletResponse res,
        User user, URIParser uriParser) throws SQLException, URINotFoundException, IOException {

    SimpleForegroundModuleDescriptor module = null;

    if ((uriParser.size() == 3 || uriParser.size() == 4) && NumberUtils.isInt(uriParser.get(2))
            && (module = this.foregroundModuleDAO.getModule(Integer.parseInt(uriParser.get(2)))) != null) {

        if (uriParser.size() == 3) {

            // Show section tree
            Document doc = this.createDocument(req, uriParser);
            Element copyModuleElement = doc.createElement("copyForegroundModule");
            doc.getFirstChild().appendChild(copyModuleElement);

            copyModuleElement.appendChild(module.toXML(doc));

            Element sectionsElement = doc.createElement("sections");
            copyModuleElement.appendChild(sectionsElement);

            this.appendSection(sectionsElement, doc, this.sectionDAO.getRootSection(true), false);

            return new SimpleForegroundModuleResponse(doc, moduleDescriptor.getName(),
                    this.getDefaultBreadcrumb(),
                    getModuleBreadcrumb(req, module, "copyModule", copyForegroundModuleBreadCrumbText));
        } else {//  w  w  w.ja v a 2s  .c  o  m

            SimpleSectionDescriptor simpleSectionDescriptor = null;

            if (NumberUtils.isInt(uriParser.get(3)) && (simpleSectionDescriptor = this.sectionDAO
                    .getSection(Integer.parseInt(uriParser.get(3)), true)) != null) {

                this.log.info("User " + user + " copying foreground module " + module + " to section "
                        + simpleSectionDescriptor);

                // Clear moduleID
                module.setModuleID(null);

                // Set new sectionID
                module.setSectionID(simpleSectionDescriptor.getSectionID());

                // Check if alias already exists
                if (this.foregroundModuleDAO.getModule(module.getSectionID(), module.getAlias()) != null) {

                    // Alias already exists add number after alias
                    for (int copyNumber = 1; copyNumber < Integer.MAX_VALUE; copyNumber++) {

                        if (this.foregroundModuleDAO.getModule(module.getSectionID(),
                                module.getAlias() + copyNumber) == null) {
                            module.setAlias(module.getAlias() + copyNumber);
                            break;
                        }
                    }
                }

                // Add module into database
                this.foregroundModuleDAO.add(module);

                res.sendRedirect(this.getModuleURI(req));

                return null;
            }
        }
    }

    throw new URINotFoundException(uriParser);
}

From source file:se.unlogic.hierarchy.foregroundmodules.systemadmin.SystemAdminModule.java

@WebPublic
public SimpleForegroundModuleResponse addSection(HttpServletRequest req, HttpServletResponse res, User user,
        URIParser uriParser) throws URINotFoundException, SQLException, IOException {

    SimpleSectionDescriptor parentSectionBean = null;

    if (uriParser.size() == 3 && NumberUtils.isInt(uriParser.get(2)) && (parentSectionBean = this.sectionDAO
            .getSection(Integer.parseInt(uriParser.get(2)), false)) != null) {

        ValidationException validationException = null;

        if (req.getMethod().equalsIgnoreCase("POST")) {

            try {
                SimpleSectionDescriptor newSectionBean = SECTION_DESCRIPTOR_POPULATOR.populate(req);
                newSectionBean.setParentSectionID(parentSectionBean.getSectionID());

                SimpleSectionDescriptor sectionMatch = this.sectionDAO
                        .getSection(parentSectionBean.getSectionID(), newSectionBean.getAlias());

                if (sectionMatch != null) {
                    throw new ValidationException(
                            new ValidationError("alias", ValidationErrorType.Other, "duplicateSectionAlias"));
                }/*from   ww  w  .j ava 2s  .c o  m*/

                this.log.info("User " + user + " adding section " + newSectionBean + " to section "
                        + parentSectionBean);

                this.sectionDAO.add(newSectionBean);

                res.sendRedirect(this.getModuleURI(req));
                return null;

            } catch (ValidationException e) {
                validationException = e;
            }
        }

        Document doc = this.createDocument(req, uriParser);
        Element addSectionElement = doc.createElement("addSection");
        doc.getFirstChild().appendChild(addSectionElement);

        addSectionElement.appendChild(parentSectionBean.toXML(doc));

        if (validationException != null) {
            addSectionElement.appendChild(validationException.toXML(doc));
            addSectionElement.appendChild(RequestUtils.getRequestParameters(req, doc));
        }

        addSectionElement.appendChild(HTTPProtocol.getProtocols(doc));

        return new SimpleForegroundModuleResponse(doc, moduleDescriptor.getName(), this.getDefaultBreadcrumb(),
                new Breadcrumb(this.addSectionBreadCrumbText, this.addSectionBreadCrumbText,
                        getFullAlias() + "/addSection/" + parentSectionBean.getSectionID()));
    } else {
        throw new URINotFoundException(uriParser);
    }
}

From source file:se.unlogic.hierarchy.foregroundmodules.systemadmin.SystemAdminModule.java

@WebPublic
public SimpleForegroundModuleResponse updateSection(HttpServletRequest req, HttpServletResponse res, User user,
        URIParser uriParser) throws URINotFoundException, SQLException, IOException {

    SimpleSectionDescriptor simpleSectionDescriptor = null;

    if (uriParser.size() == 3 && NumberUtils.isInt(uriParser.get(2))
            && (simpleSectionDescriptor = this.sectionDAO.getSection(Integer.parseInt(uriParser.get(2)),
                    true)) != null) {/*from  w  w w  .  ja  v  a 2  s.  c o m*/

        ValidationException validationException = null;

        if (req.getMethod().equalsIgnoreCase("POST")) {

            try {
                simpleSectionDescriptor = SECTION_DESCRIPTOR_POPULATOR.populate(simpleSectionDescriptor, req);

                SimpleSectionDescriptor sectionMatch = this.sectionDAO.getSection(
                        simpleSectionDescriptor.getParentSectionID(), simpleSectionDescriptor.getAlias());

                if (sectionMatch != null
                        && !sectionMatch.getSectionID().equals(simpleSectionDescriptor.getSectionID())) {
                    throw new ValidationException(
                            new ValidationError("alias", ValidationErrorType.Other, "duplicateSectionAlias"));
                }

                this.log.info("User " + user + " updating section " + simpleSectionDescriptor);

                this.sectionDAO.update(simpleSectionDescriptor);

                this.sectionDAO.getReverseFullAlias(simpleSectionDescriptor);

                // Check if the section is the root section
                if (simpleSectionDescriptor.getParentSectionID() == null) {

                    // Section is root section, update root section in background using background thread to prevent thread locks
                    RootSectionUpdater rootSectionUpdater = new RootSectionUpdater(
                            this.sectionInterface.getSystemInterface().getRootSection(),
                            simpleSectionDescriptor);
                    rootSectionUpdater.setDaemon(true);
                    rootSectionUpdater.start();

                } else {

                    // Section is not root section, check if the section is cached
                    SectionInterface sectionInterface = systemInterface
                            .getSectionInterface(simpleSectionDescriptor.getParentSectionID());

                    if (sectionInterface != null
                            && sectionInterface.getSectionCache().isCached(simpleSectionDescriptor)) {

                        // Section is cached, check if it is a parentsection to this module
                        if (this.isParentSection(simpleSectionDescriptor.getSectionID())) {

                            // The section is a parent section to this module, use background thread to avoid threadlock
                            SectionUpdater sectionUpdater = new SectionUpdater(
                                    sectionInterface.getSectionCache(), simpleSectionDescriptor, user);
                            sectionUpdater.isDaemon();
                            sectionUpdater.start();

                        } else {

                            // The section is not a parent section to this module, update it using this thread
                            try {
                                sectionInterface.getSectionCache().update(simpleSectionDescriptor);
                            } catch (KeyNotCachedException e) {
                                this.log.debug("Unable to update section " + simpleSectionDescriptor
                                        + " requested by user " + user + ", section unloaded.");
                            }
                        }
                    }
                }

                res.sendRedirect(this.getModuleURI(req));
                return null;

            } catch (ValidationException e) {
                validationException = e;
            }
        }

        Document doc = this.createDocument(req, uriParser);
        Element updateSectionElement = doc.createElement("updateSection");
        doc.getFirstChild().appendChild(updateSectionElement);

        updateSectionElement.appendChild(simpleSectionDescriptor.toXML(doc));

        if (validationException != null) {
            updateSectionElement.appendChild(validationException.toXML(doc));
            updateSectionElement.appendChild(RequestUtils.getRequestParameters(req, doc));
        }

        AccessUtils.appendAllowedGroupsAndUsers(doc, updateSectionElement, simpleSectionDescriptor,
                systemInterface.getUserHandler(), systemInterface.getGroupHandler());
        updateSectionElement.appendChild(HTTPProtocol.getProtocols(doc));

        return new SimpleForegroundModuleResponse(doc, moduleDescriptor.getName(), this.getDefaultBreadcrumb(),
                getSectionBreadcrumb(req, simpleSectionDescriptor, "updateSection",
                        updateSectionBreadCrumbText));
    } else {
        throw new URINotFoundException(uriParser);
    }
}

From source file:se.unlogic.hierarchy.foregroundmodules.systemadmin.SystemAdminModule.java

@WebPublic
public SimpleForegroundModuleResponse moveSection(HttpServletRequest req, HttpServletResponse res, User user,
        URIParser uriParser) throws SQLException, URINotFoundException, IOException {

    SimpleSectionDescriptor simpleSectionDescriptor = null;

    if ((uriParser.size() == 3 || uriParser.size() == 4) && NumberUtils.isInt(uriParser.get(2))
            && (simpleSectionDescriptor = this.sectionDAO.getSection(Integer.parseInt(uriParser.get(2)),
                    false)) != null) {/*w w w  . ja  v  a 2  s .c o m*/

        if (uriParser.size() == 3) {

            // Show section tree
            Document doc = this.createDocument(req, uriParser);
            Element moveModuleElement = doc.createElement("moveSection");
            doc.getFirstChild().appendChild(moveModuleElement);

            moveModuleElement.appendChild(simpleSectionDescriptor.toXML(doc));

            Element sectionsElement = doc.createElement("sections");
            moveModuleElement.appendChild(sectionsElement);

            this.appendSection(sectionsElement, doc, this.sectionDAO.getRootSection(true), false);

            return new SimpleForegroundModuleResponse(doc, moduleDescriptor.getName(),
                    this.getDefaultBreadcrumb(), getSectionBreadcrumb(req, simpleSectionDescriptor,
                            "moveSection", moveSectionBreadCrumbText));
        } else {

            SimpleSectionDescriptor newParentSectionBean = null;

            if (NumberUtils.isInt(uriParser.get(3)) && (newParentSectionBean = this.sectionDAO
                    .getSection(Integer.parseInt(uriParser.get(3)), true)) != null) {

                if (simpleSectionDescriptor.getParentSectionID() == newParentSectionBean.getSectionID()) {

                    this.log.info("User " + user + " trying to move section " + simpleSectionDescriptor
                            + " to the section it already belongs to, ignoring move");

                    res.sendRedirect(this.getModuleURI(req));

                    return null;

                } else {

                    // TODO check alias for duplicates

                    this.log.info("User " + user + " moving section " + simpleSectionDescriptor + " to section "
                            + newParentSectionBean);

                    // Update section in database
                    Integer oldParentSectionID = simpleSectionDescriptor.getParentSectionID();
                    simpleSectionDescriptor.setParentSectionID(newParentSectionBean.getSectionID());
                    this.sectionDAO.update(simpleSectionDescriptor);

                    // Set new fullalias
                    simpleSectionDescriptor.setFullAlias(
                            newParentSectionBean.getFullAlias() + "/" + simpleSectionDescriptor.getAlias());

                    // Check if the section is a parent to this module
                    if (this.isParentSection(simpleSectionDescriptor.getSectionID())) {

                        // The selected section is a parent to this section, use background thread instead

                        // Check if the target section is cached
                        SectionInterface targetSectionInterface = systemInterface
                                .getSectionInterface(simpleSectionDescriptor.getParentSectionID());

                        if (targetSectionInterface != null) {

                            // Target section is cached move section

                            SectionMover sectionMover = new SectionMover(
                                    systemInterface.getSectionInterface(oldParentSectionID).getSectionCache(),
                                    targetSectionInterface.getSectionCache(), simpleSectionDescriptor, user);
                            sectionMover.start();
                        } else {

                            // Target section is not cached unload section
                            SectionUnloader sectionUnloader = new SectionUnloader(
                                    systemInterface.getSectionInterface(oldParentSectionID).getSectionCache(),
                                    simpleSectionDescriptor, user);
                            sectionUnloader.start();
                        }

                    } else {
                        // Check if the parent section of the section is cached
                        SectionInterface oldSectionInterface = systemInterface
                                .getSectionInterface(oldParentSectionID);

                        boolean enabled = false;

                        if (oldSectionInterface != null
                                && oldSectionInterface.getSectionCache().isCached(simpleSectionDescriptor)) {

                            enabled = true;

                            try {
                                oldSectionInterface.getSectionCache().unload(simpleSectionDescriptor);
                            } catch (KeyNotCachedException e) {
                                this.log.debug("Error unloading section " + simpleSectionDescriptor
                                        + " from section " + oldSectionInterface.getSectionDescriptor()
                                        + " during module move to section " + newParentSectionBean
                                        + " requested by user " + user + ", sectiond already unloaded");
                            }
                        }

                        // If the module is enabled and the receiving section is cached then cache the module there
                        if (enabled) {
                            SectionInterface newParentSectionInterface = systemInterface
                                    .getSectionInterface(simpleSectionDescriptor.getParentSectionID());

                            if (newParentSectionInterface != null) {
                                try {
                                    newParentSectionInterface.getSectionCache().cache(simpleSectionDescriptor);
                                } catch (Exception e) {
                                    this.log.error("Error caching section " + simpleSectionDescriptor
                                            + " in section " + newParentSectionInterface.getSectionDescriptor()
                                            + " during section move from section "
                                            + oldSectionInterface.getSectionDescriptor() + " by user " + user,
                                            e);
                                }
                            }
                        }
                    }

                    res.sendRedirect(this.getModuleURI(req));

                    return null;
                }
            }
        }
    }

    throw new URINotFoundException(uriParser);
}

From source file:se.unlogic.hierarchy.foregroundmodules.systemadmin.SystemAdminModule.java

@WebPublic(alias = "import")
public SimpleForegroundModuleResponse importModules(HttpServletRequest req, HttpServletResponse res, User user,
        URIParser uriParser)/*ww w  .j a v  a  2s.  co  m*/
        throws URINotFoundException, SQLException, IOException, InstantiationException, IllegalAccessException {

    SimpleSectionDescriptor sectionDescriptor = null;

    if (uriParser.size() == 3 && NumberUtils.isInt(uriParser.get(2)) && (sectionDescriptor = this.sectionDAO
            .getSection(Integer.parseInt(uriParser.get(2)), true)) != null) {

        ValidationException validationException = null;

        if (req.getMethod().equalsIgnoreCase("POST") && MultipartRequest.isMultipartRequest(req)) {

            MultipartRequest multipartRequest = null;

            try {
                multipartRequest = new MultipartRequest(ramThreshold * BinarySizes.KiloByte,
                        maxRequestSize * BinarySizes.MegaByte, maxFileSize * BinarySizes.MegaByte, req);

                List<SimpleForegroundModuleDescriptor> foregroundModuleDescriptors = null;
                List<SimpleBackgroundModuleDescriptor> backgroundModuleDescriptors = null;
                List<SimpleFilterModuleDescriptor> filterModuleDescriptors = null;

                List<ValidationError> errors = new ArrayList<ValidationError>();

                for (FileItem fileItem : multipartRequest.getFiles()) {

                    if (StringUtils.isEmpty(fileItem.getName()) || fileItem.getSize() == 0) {

                        continue;
                    }

                    if (fileItem.getName().endsWith(".fgmodule")) {

                        try {

                            foregroundModuleDescriptors = CollectionUtils.addAndInstantiateIfNeeded(
                                    foregroundModuleDescriptors,
                                    parseModuleDescriptor(fileItem, SimpleForegroundModuleDescriptor.class));

                        } catch (ValidationException e) {

                            errors.addAll(e.getErrors());
                        }

                    } else if (fileItem.getName().endsWith(".bgmodule")) {

                        try {

                            backgroundModuleDescriptors = CollectionUtils.addAndInstantiateIfNeeded(
                                    backgroundModuleDescriptors,
                                    parseModuleDescriptor(fileItem, SimpleBackgroundModuleDescriptor.class));

                        } catch (ValidationException e) {

                            errors.addAll(e.getErrors());
                        }

                    } else if (fileItem.getName().endsWith(".flmodule")) {

                        if (sectionDescriptor.getParentSectionID() != null) {

                            errors = CollectionUtils.addAndInstantiateIfNeeded(errors,
                                    new ValidationError("FilterModuleImportInSubsection"));
                            continue;
                        }

                        try {
                            filterModuleDescriptors = CollectionUtils.addAndInstantiateIfNeeded(
                                    filterModuleDescriptors,
                                    parseModuleDescriptor(fileItem, SimpleFilterModuleDescriptor.class));

                        } catch (ValidationException e) {

                            errors.addAll(e.getErrors());
                        }

                    } else {

                        errors.add(new InvalidFileExtensionValidationError(fileItem.getName()));
                    }
                }

                if (!errors.isEmpty()) {

                    throw new ValidationException(errors);

                } else if (foregroundModuleDescriptors == null && backgroundModuleDescriptors == null
                        && filterModuleDescriptors == null) {

                    throw new ValidationException(new ValidationError("NoDescriptorsfound"));

                } else {

                    boolean preserveModuleIDs = multipartRequest.getParameter("preserveModuleIDs") != null;

                    boolean preserveDataSourceIDs = multipartRequest
                            .getParameter("preserveDataSourceIDs") != null;

                    StartMode startMode = EnumUtils.toEnum(StartMode.class,
                            multipartRequest.getParameter("startMode"));

                    if (!preserveModuleIDs) {

                        clearModuleIDs(foregroundModuleDescriptors);
                        clearModuleIDs(backgroundModuleDescriptors);
                        clearModuleIDs(filterModuleDescriptors);

                    } else {

                        checkModuleIDs(foregroundModuleDescriptors, foregroundModuleDAO, errors);
                        checkModuleIDs(backgroundModuleDescriptors, backgroundModuleDAO, errors);
                        checkModuleIDs(filterModuleDescriptors, filterModuleDAO, errors);
                    }

                    if (!preserveDataSourceIDs) {

                        clearDataSourceIDs(foregroundModuleDescriptors);
                        clearDataSourceIDs(backgroundModuleDescriptors);
                        clearDataSourceIDs(filterModuleDescriptors);
                    }

                    if (foregroundModuleDescriptors != null) {

                        for (ForegroundModuleDescriptor moduleDescriptor : foregroundModuleDescriptors) {

                            ForegroundModuleDescriptor conflictingDescriptor = foregroundModuleDAO
                                    .getModule(sectionDescriptor.getSectionID(), moduleDescriptor.getAlias());

                            if (conflictingDescriptor != null) {

                                errors.add(new DuplicateModuleAliasValidationError(moduleDescriptor,
                                        conflictingDescriptor, ModuleType.FOREGROUND));
                            }
                        }
                    }

                    if (!errors.isEmpty()) {

                        throw new ValidationException(errors);
                    }

                    setSectionID(foregroundModuleDescriptors, sectionDescriptor.getSectionID());
                    setSectionID(backgroundModuleDescriptors, sectionDescriptor.getSectionID());

                    log.info("User "
                            + user + " importing " + CollectionUtils.getSize(foregroundModuleDescriptors,
                                    backgroundModuleDescriptors, filterModuleDescriptors)
                            + " modules into section " + sectionDescriptor);

                    addModules(foregroundModuleDescriptors, foregroundModuleDAO);
                    addModules(backgroundModuleDescriptors, backgroundModuleDAO);
                    addModules(filterModuleDescriptors, filterModuleDAO);

                    if (startMode != null) {

                        SectionInterface sectionInterface = systemInterface
                                .getSectionInterface(sectionDescriptor.getSectionID());

                        if (sectionInterface != null) {

                            cacheModules(foregroundModuleDescriptors,
                                    sectionInterface.getForegroundModuleCache(), startMode, sectionDescriptor,
                                    user);
                            cacheModules(backgroundModuleDescriptors,
                                    sectionInterface.getBackgroundModuleCache(), startMode, sectionDescriptor,
                                    user);
                            cacheModules(filterModuleDescriptors, systemInterface.getFilterModuleCache(),
                                    startMode, sectionDescriptor, user);
                        }
                    }

                    res.sendRedirect(this.getModuleURI(req));
                    return null;
                }

            } catch (ValidationException e) {

                validationException = e;

            } catch (SizeLimitExceededException e) {

                validationException = new ValidationException(
                        new RequestSizeLimitExceededValidationError(e.getActualSize(), e.getPermittedSize()));

            } catch (FileSizeLimitExceededException e) {

                validationException = new ValidationException(new FileSizeLimitExceededValidationError(
                        e.getFileName(), e.getActualSize(), e.getPermittedSize()));

            } catch (FileUploadException e) {

                validationException = new ValidationException(new ValidationError("UnableToParseRequest"));

            } finally {

                if (multipartRequest != null) {

                    multipartRequest.deleteFiles();
                }
            }
        }

        log.info("User " + user + " requesting import modules form in section " + sectionDescriptor);

        Document doc = this.createDocument(req, uriParser);
        Element importModulesElement = doc.createElement("ImportModules");
        doc.getFirstChild().appendChild(importModulesElement);

        importModulesElement.appendChild(sectionDescriptor.toXML(doc));

        XMLUtils.appendNewElement(doc, importModulesElement, "Started",
                systemInterface.getSectionInterface(sectionDescriptor.getSectionID()) != null);

        if (validationException != null) {
            importModulesElement.appendChild(validationException.toXML(doc));
        }

        return new SimpleForegroundModuleResponse(doc, moduleDescriptor.getName(), this.getDefaultBreadcrumb(),
                getSectionBreadcrumb(req, sectionDescriptor, "importModules", importModulesBreadCrumbText));
    } else {
        throw new URINotFoundException(uriParser);
    }
}

From source file:SeedGenerator.MainForm.java

private void btnParseResponseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnParseResponseActionPerformed
    try {// w w w. java2  s. com
        String SQLurl = "SELECT id,resultSet FROM queryqueue where commonQueryId between 7 and 13;";
        Statement stmturl = con1.createStatement();
        ResultSet rsurl = stmturl.executeQuery(SQLurl);

        while (rsurl.next()) {
            try {
                String s = rsurl.getString("resultSet");
                int id = rsurl.getInt("id");
                //get the factory
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(s));

                Document doc = db.parse(is);
                boolean bool = true;

                Node n = doc.getFirstChild();

                Node firstnode = doc.getFirstChild();
                recursiveXmlParse(firstnode, id, false);
                //                    NodeList headnodes = doc.getElementsByTagName("head");
                //                    NodeList resultnodes = doc.getElementsByTagName("result");
                //                    for (int i = 0; i < headnodes.getLength(); i++) {
                //                        Element element = (Element) headnodes.item(i);
                //                        String b = element.getElementsByTagName("binding").item(0).getTextContent();
                //                        System.out.println(b);
                //                        //            System.out.println(element.getNodeName());
                //                    }
                //                    for (int i = 0; i < resultnodes.getLength(); i++) {
                //                        Element element = (Element) resultnodes.item(i);
                //                        System.out.println(element.getTextContent());
                //                    }
            } catch (Exception ex) {
                //             System.out.println(ex.getMessage());
            }
        }
        stmturl.close();
        rsurl.close();

        // return id + 1;
    } catch (Exception ex) {
        //    System.out.println(ex.getMessage());
    } // TODO add your handling code here:
}