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.imagegallery.GalleryModule.java

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

    Gallery gallery = null;//from   w  w  w .j ava 2 s. c om

    if (uriParser.size() != 4 || (gallery = this.galleryDao.get(uriParser.get(2).toString())) == null) {

        throw new URINotFoundException(uriParser);

    } else {

        this.checkAccess(user, gallery);

        // check if path is valid
        File dir = null;
        try {
            dir = new File(gallery.getUrl());
        } catch (Exception ex) {
            throw new URINotFoundException(uriParser);
        }

        // get all filenames
        String filename = uriParser.get(3);

        // get all files
        File[] allFiles = dir.listFiles(fileFilter);

        Arrays.sort(allFiles);

        // check if filename exist
        boolean found = false;
        int currentIdx = -1;

        for (int i = 0; i < allFiles.length; i++) {
            if (allFiles[i].getName().equals(filename)) {
                found = true;
                currentIdx = i;
                break;
            }
        }
        int picsInGallery = allFiles.length;

        if (found) {

            log.info("User " + user + " requested image " + filename + " in gallery " + gallery);

            HttpSession session = req.getSession(true);

            // find next and previous picture
            String nextPicture = currentIdx < picsInGallery - 1 ? allFiles[currentIdx + 1].getName() : null;
            String prevPicture = currentIdx > 0 ? allFiles[currentIdx - 1].getName() : null;

            // create XML-document containing information about the requested gallery and images
            Document doc = this.createDocument(req, uriParser, user);

            Element pictureElement = doc.createElement("showImage");
            doc.getFirstChild().appendChild(pictureElement);

            Node gNode = gallery.toXML(doc);
            gNode.appendChild(XMLUtils.createElement("numPics", String.valueOf(picsInGallery), doc));
            gNode.appendChild(XMLUtils.createElement("currentPic", String.valueOf(currentIdx + 1), doc));

            if (currentIdx == 0) {
                currentIdx++;
            }

            pictureElement.appendChild(
                    XMLUtils.createElement("currentPage", (((currentIdx) / numOfThumbsPerPage) + 1) + "", doc));

            if (nextPicture != null) {
                gNode.appendChild(XMLUtils.createElement("nextImage", nextPicture.toString(), doc));
            }
            if (prevPicture != null) {
                gNode.appendChild(XMLUtils.createElement("prevImage", prevPicture.toString(), doc));
            }

            Element fileElement = doc.createElement("file");
            fileElement.appendChild(XMLUtils.createElement("filename", filename, doc));

            Element commentsElement = doc.createElement("comments");
            ArrayList<Comment> comments = commentDao.getByFilenameAndGallery(filename, gallery);

            //TODO move this to a separate method and add captcha support
            if (req.getMethod().equalsIgnoreCase("POST")) {

                String commentStatus = req.getParameter("viewComments");

                if (commentStatus != null) {
                    if (commentStatus.equals("true")) {
                        session.setAttribute(this.moduleDescriptor.getModuleID() + ".showAll", true);
                    } else {
                        session.setAttribute(this.moduleDescriptor.getModuleID() + ".showAll", false);
                    }
                }

                String commentText = req.getParameter("commentText");
                if (commentText != null && (this.allowAnonymousComments || user != null)) {
                    try {
                        if (StringUtils.isEmpty(commentText)) {
                            throw new ValidationException(
                                    new ValidationError("commentText", ValidationErrorType.RequiredField));
                        }

                        Comment comment = new Comment();
                        comment.setComment(commentText);
                        comment.setDate(new Timestamp(System.currentTimeMillis()));
                        comment.setPictureID(pictureDao.getPictureIDByFilenameAndGallery(filename, gallery));

                        if (user != null) {
                            comment.setUser(user);
                        }

                        log.info("User " + user + " adding comment " + comment + " to image " + filename
                                + " in gallery " + gallery);

                        commentDao.add(comment);

                        res.sendRedirect(req.getRequestURI());
                    } catch (ValidationException e) {
                        fileElement.appendChild(e.toXML(doc));
                    }
                }
            }

            // check if user wants to show all comments or not
            Boolean showAll = (Boolean) session.getAttribute(this.moduleDescriptor.getModuleID() + ".showAll");
            if (showAll == null || showAll) {
                commentsElement.appendChild(XMLUtils.createElement("showAll", "true", doc));

                if (comments != null) {

                    for (Comment comment : comments) {
                        commentsElement.appendChild(comment.toXML(doc));
                    }
                }
            }

            if (comments != null) {
                commentsElement.appendChild(
                        XMLUtils.createElement("commentsNum", String.valueOf(comments.size()), doc));
            }

            fileElement.appendChild(commentsElement);

            if (this.allowAnonymousComments || user != null) {
                fileElement.appendChild(doc.createElement("commentsAllowed"));
            }

            gNode.appendChild(fileElement);
            pictureElement.appendChild(gNode);

            return new SimpleForegroundModuleResponse(doc, filename, getDefaultBreadcrumb(),
                    getGalleryBreadcrumb(gallery, req), getImageBreadcrumb(gallery, filename, req));
        } else {
            throw new URINotFoundException(uriParser);
        }
    }
}

From source file:se.unlogic.hierarchy.foregroundmodules.imagegallery.GalleryModule.java

@SuppressWarnings("deprecation")
@WebPublic/*from w  ww  .j a v  a 2  s  . c  om*/
public SimpleForegroundModuleResponse addGallery(HttpServletRequest req, HttpServletResponse res, User user,
        URIParser uriParser) throws Exception {

    this.checkAdminAccess(user);

    ValidationException validationException = null;

    MultipartRequest requestWrapper = null;

    if (req.getMethod().equalsIgnoreCase("POST")) {
        try {
            requestWrapper = new MultipartRequest(this.ramThreshold * BinarySizes.KiloByte,
                    this.diskThreshold * BinarySizes.MegaByte, req);

            Gallery gallery = GalleryPopulator.populate(requestWrapper);

            log.info("User " + user + " adding gallery " + gallery);

            gallery.setGalleryID(this.galleryDao.add(gallery));

            Boolean zipUpload = requestWrapper.getParameter("uploadCheck") != null;

            if (zipUpload) {

                try {

                    FileItem fileItem = requestWrapper.getFile(0);

                    this.uploadGalleryZip(fileItem, gallery);

                } catch (FileUploadException e) {
                    this.galleryDao.delete(gallery);
                    throw new ValidationException(new ValidationError("UnableToParseRequest"));
                } catch (IOException e) {
                    this.galleryDao.delete(gallery);
                    throw new ValidationException(new ValidationError("UnableToParseRequest"));
                } finally {
                    if (requestWrapper != null) {
                        requestWrapper.deleteFiles();
                    }
                }

            }

            // create thumbs
            this.createGalleryThumbs(gallery, false);

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

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

    Document doc = this.createDocument(req, uriParser, user);

    Element addGalleryElement = doc.createElement("addGallery");
    doc.getFirstChild().appendChild(addGalleryElement);
    if (!StringUtils.isEmpty(this.path)) {
        addGalleryElement.appendChild(XMLUtils.createElement("path", this.path, doc));
    }

    if (validationException != null) {
        addGalleryElement.appendChild(validationException.toXML(doc));
        addGalleryElement.appendChild(RequestUtils.getRequestParameters(requestWrapper, doc));

    }

    AccessUtils.appendGroupsAndUsers(doc, addGalleryElement,
            sectionInterface.getSystemInterface().getUserHandler(),
            sectionInterface.getSystemInterface().getGroupHandler());

    return new SimpleForegroundModuleResponse(doc, this.moduleDescriptor.getName(),
            this.getDefaultBreadcrumb());
}

From source file:se.unlogic.hierarchy.foregroundmodules.imagegallery.GalleryModule.java

@SuppressWarnings("deprecation")
@WebPublic//from  w  ww.ja  v a2 s.c o m
public SimpleForegroundModuleResponse addImages(HttpServletRequest req, HttpServletResponse res, User user,
        URIParser uriParser) throws Exception {

    Gallery gallery = null;

    if (uriParser.size() < 3 || (gallery = this.galleryDao.get(uriParser.get(2))) == null) {

        throw new URINotFoundException(uriParser);

    } else {

        this.checkUploadAccess(user, gallery);

        ValidationException validationException = null;

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

            MultipartRequest requestWrapper = null;

            try {
                requestWrapper = new MultipartRequest(this.ramThreshold * BinarySizes.KiloByte,
                        this.diskThreshold * BinarySizes.MegaByte, req);

                FileItem fileItem = requestWrapper.getFile(0);

                if (fileItem.getName() != null && fileItem.getName().toLowerCase().endsWith(".zip")) {

                    log.info("User " + user + " adding images from zip file to gallery " + gallery);

                    int imageCount = this.uploadGalleryZip(fileItem, gallery);

                    log.info("User " + user + " added " + imageCount + " images to gallery " + gallery);

                } else if (fileFilter.accept(fileItem)) {

                    log.info("User " + user + " adding image to gallery " + gallery);

                    File file = new File(gallery.getUrl() + "/" + fileItem.getName());
                    fileItem.write(file);

                    log.info("User " + user + " added 1 image to gallery " + gallery);

                } else {
                    throw new ValidationException(new ValidationError("UnableToParseRequest"));
                }

                // create thumbs
                this.createGalleryThumbs(gallery, false);

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

            } catch (ValidationException e) {
                validationException = e;
            } catch (FileSizeLimitExceededException e) {
                validationException = new ValidationException(new ValidationError("FileSizeLimitExceeded"));
            } catch (FileUploadException e) {
                validationException = new ValidationException(new ValidationError("UnableToParseRequest"));
            } finally {
                if (requestWrapper != null) {
                    requestWrapper.deleteFiles();
                }
            }
        }

        Document doc = this.createDocument(req, uriParser, user);

        Element addImageElement = doc.createElement("addImages");
        doc.getFirstChild().appendChild(addImageElement);
        addImageElement.appendChild(gallery.toXML(doc));

        XMLUtils.appendNewElement(doc, addImageElement, "diskThreshold", diskThreshold);

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

        AccessUtils.appendGroupsAndUsers(doc, addImageElement,
                sectionInterface.getSystemInterface().getUserHandler(),
                sectionInterface.getSystemInterface().getGroupHandler());

        return new SimpleForegroundModuleResponse(doc, this.moduleDescriptor.getName(),
                this.getDefaultBreadcrumb());
    }

}

From source file:se.unlogic.hierarchy.foregroundmodules.imagegallery.GalleryModule.java

@SuppressWarnings("deprecation")
@WebPublic//w w w.ja v  a 2  s.c o  m
public SimpleForegroundModuleResponse updateGallery(HttpServletRequest req, HttpServletResponse res, User user,
        URIParser uriParser) throws IOException, SQLException, URINotFoundException, AccessDeniedException {

    this.checkAdminAccess(user);

    Gallery gallery = null;

    if (uriParser.size() != 3 || (gallery = this.galleryDao.get(uriParser.get(2))) == null) {
        throw new URINotFoundException(uriParser);

    } else {

        ValidationException validationException = null;

        if (req.getMethod().equalsIgnoreCase("POST")) {
            try {
                gallery = GalleryPopulator.populate(gallery, req);

                log.info("User " + user + " updating gallery " + gallery);

                this.galleryDao.update(gallery);

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

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

        Document doc = this.createDocument(req, uriParser, user);

        Element updateGalleryElement = doc.createElement("updateGallery");
        doc.getFirstChild().appendChild(updateGalleryElement);
        if (!StringUtils.isEmpty(this.path)) {
            updateGalleryElement.appendChild(XMLUtils.createElement("path", this.path, doc));
        }

        updateGalleryElement.appendChild(gallery.toXML(doc));

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

        AccessUtils.appendGroupsAndUsers(doc, updateGalleryElement,
                sectionInterface.getSystemInterface().getUserHandler(),
                sectionInterface.getSystemInterface().getGroupHandler());

        return new SimpleForegroundModuleResponse(doc, this.moduleDescriptor.getName(),
                this.getDefaultBreadcrumb());
    }
}

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

@Override
public SimpleForegroundModuleResponse defaultMethod(HttpServletRequest req, HttpServletResponse res, User user,
        URIParser uriParser) throws Exception {

    log.info("User " + user + " listing system tree");

    Document doc = this.createDocument(req, uriParser);

    SimpleSectionDescriptor rootSection = this.sectionDAO.getRootSection(true);

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

    this.appendSection(sectionsElement, doc, rootSection, true);

    return new SimpleForegroundModuleResponse(doc, moduleDescriptor.getName(), this.getDefaultBreadcrumb());
}

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

@WebPublic(alias = "addbmodule")
public SimpleForegroundModuleResponse addBackgroundModule(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  ww  w. j  a v a2s . c  o  m

        ValidationException validationException = null;

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

            try {
                SimpleBackgroundModuleDescriptor simpleModuleDescriptor = BACKGROUND_MODULE_DESCRIPTOR_POPULATOR
                        .populate(req);
                simpleModuleDescriptor.setSectionID(simpleSectionDescriptor.getSectionID());

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

                this.backgroundModuleDAO.add(simpleModuleDescriptor);

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

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

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

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

        addModuleElement.appendChild(this.getDataSources(doc));
        addModuleElement.appendChild(getPathTypes(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.addBackgroundModuleBreadCrumbText, this.addBackgroundModuleBreadCrumbText,
                        getFullAlias() + "/addModule/" + simpleSectionDescriptor.getSectionID()));
    } else {
        throw new URINotFoundException(uriParser);
    }
}

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

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

    SimpleBackgroundModuleDescriptor simpleModuleDescriptor = null;

    if (uriParser.size() == 3 && NumberUtils.isInt(uriParser.get(2))
            && (simpleModuleDescriptor = this.backgroundModuleDAO
                    .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 = BACKGROUND_MODULE_DESCRIPTOR_POPULATOR.populate(simpleModuleDescriptor,
                        req);/*from w w  w.  java 2  s .co  m*/
            } catch (ValidationException e) {
                validationErrors.addAll(e.getErrors());
            }

            // Check module specific settings
            // TODO handle runtime exceptions...
            BackgroundModule moduleInstance = this.getBackgroundModuleInstance(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 background module " + simpleModuleDescriptor);

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

                this.backgroundModuleDAO.update(simpleModuleDescriptor);

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

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

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

                    // Module is cached update it
                    try {
                        sectionInterface.getBackgroundModuleCache().update(simpleModuleDescriptor);
                    } catch (Exception e) {
                        this.log.error("Error updating background 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("updateBackgroundModule");
        doc.getFirstChild().appendChild(updateModuleElement);

        BackgroundModule moduleInstance = this.getBackgroundModuleInstance(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, "updateModule",
                        updateBackgroundModuleBreadCrumbText));
    } else {
        throw new URINotFoundException(uriParser);
    }
}

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

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

    SimpleBackgroundModuleDescriptor module = null;

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

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

            // Show section tree
            Document doc = this.createDocument(req, uriParser);
            Element moveModuleElement = doc.createElement("moveBackgroundModule");
            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", moveBackgroundModuleBreadCrumbText));
        } else {/*from ww w. j  a v a  2  s.  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 background 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 background module " + module + " to section "
                            + simpleSectionDescriptor);

                    // Update module in database
                    Integer oldSectionID = module.getSectionID();
                    module.setSectionID(simpleSectionDescriptor.getSectionID());
                    this.backgroundModuleDAO.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.getBackgroundModuleCache().isCached(module)) {

                        enabled = true;

                        try {
                            oldSectionInterface.getBackgroundModuleCache().unload(module);
                        } catch (Exception e) {
                            this.log.error("Error unloading background 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.getBackgroundModuleCache().cache(module);
                            } catch (Exception e) {
                                this.log.error("Error caching background 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 = "copybmodule")
public SimpleForegroundModuleResponse copyBackgroundModule(HttpServletRequest req, HttpServletResponse res,
        User user, URIParser uriParser) throws SQLException, URINotFoundException, IOException {

    SimpleBackgroundModuleDescriptor module = null;

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

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

            // Show section tree
            Document doc = this.createDocument(req, uriParser);
            Element copyModuleElement = doc.createElement("copyBackgroundModule");
            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", copyBackgroundModuleBreadCrumbText));
        } else {//from w w w  . j a  v a  2 s.co 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 background module " + module + " to section "
                        + simpleSectionDescriptor);

                // Clear moduleID
                module.setModuleID(null);

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

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

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

                return null;
            }
        }
    }

    throw new URINotFoundException(uriParser);
}

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

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

    ValidationException validationException = null;

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

        try {// ww w  .  j av  a 2 s .c  o m
            SimpleFilterModuleDescriptor simpleModuleDescriptor = FILTER_MODULE_DESCRIPTOR_POPULATOR
                    .populate(req);

            this.log.info("User " + user + " adding filter module " + simpleModuleDescriptor);

            this.filterModuleDAO.add(simpleModuleDescriptor);

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

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

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

    addModuleElement.appendChild(this.getDataSources(doc));
    addModuleElement.appendChild(getPathTypes(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.addFilterModuleBreadCrumbText, this.addFilterModuleBreadCrumbText,
                    getFullAlias() + "/addfiltermodule/"));
}