com.sinet.gage.dlap.utils.XMLUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.sinet.gage.dlap.utils.XMLUtils.java

Source

package com.sinet.gage.dlap.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import com.sinet.gage.dlap.entities.Customization;
import com.sinet.gage.dlap.entities.Customization.AllowViewInGpeerCompletion;
import com.sinet.gage.dlap.entities.Customization.Authentication;
import com.sinet.gage.dlap.entities.Customization.Authentication.Provider;
import com.sinet.gage.dlap.entities.Customization.Authentication.Provider.ApplicationId;
import com.sinet.gage.dlap.entities.Customization.Authentication.Provider.Logout;
import com.sinet.gage.dlap.entities.Customization.Authentication.Provider.Server;
import com.sinet.gage.dlap.entities.Customization.Files;
import com.sinet.gage.dlap.entities.Customization.Files.File;
import com.sinet.gage.dlap.entities.Customization.ItemMetadata;
import com.sinet.gage.dlap.entities.Customization.ItemMetadata.Value;
import com.sinet.gage.dlap.entities.Customization.ItemMetadata.Value.Choice;
import com.sinet.gage.dlap.entities.Customization.LearningObjectives;
import com.sinet.gage.dlap.entities.Customization.MenuItems;
import com.sinet.gage.dlap.entities.Customization.MenuItems.MenuItem;
import com.sinet.gage.dlap.entities.Customization.ResourceBase;
import com.sinet.gage.dlap.entities.Customization.Securehash;
import com.sinet.gage.dlap.entities.Customization.Securehash.Secretkey;
import com.sinet.gage.dlap.entities.Customization.Strings;
import com.sinet.gage.dlap.entities.Data;
import com.sinet.gage.dlap.entities.DomainResponse;
import com.sinet.gage.dlap.entities.Domains;
import com.sinet.gage.dlap.entities.Domains.Domain;
import com.sinet.gage.dlap.entities.EdivateLearn;
import com.sinet.gage.dlap.entities.EncryptedData;
import com.sinet.gage.dlap.entities.EncryptedData.CipherData;
import com.sinet.gage.dlap.entities.EncryptedData.CipherData.CipherValue;
import com.sinet.gage.dlap.entities.EncryptedData.EncryptionMethod;

/**
 * @author Team.Gage
 *
 */
public class XMLUtils {

    protected final static Logger LOGGER = LoggerFactory.getLogger(XMLUtils.class);

    /**
     * @param String
     * @param String
     * @return String
     */
    public static String parseXML(String xml, String rootElementName) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(false);
            DocumentBuilder db;
            db = dbf.newDocumentBuilder();
            Document newDoc = db.parse(new ByteArrayInputStream(xml.getBytes()));

            Element element = (Element) newDoc.getElementsByTagName(rootElementName).item(0);
            // Imports a node from another document to this document,
            // without altering
            Document newDoc2 = db.newDocument();
            // or removing the source node from the original document
            Node copiedNode = newDoc2.importNode(element, true);
            // Adds the node to the end of the list of children of this node
            newDoc2.appendChild(copiedNode);

            Transformer tf = TransformerFactory.newInstance().newTransformer();
            tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            tf.setOutputProperty(OutputKeys.INDENT, "yes");

            Writer out = new StringWriter();

            tf.transform(new DOMSource(newDoc2), new StreamResult(out));

            return out.toString();
        } catch (TransformerException | ParserConfigurationException | SAXException | IOException e) {
            LOGGER.error("Exception in parsing xml from response: ", e);
        }
        return "";
    }

    public static List<DomainResponse> transferObject(Domains domains) {

        List<DomainResponse> domainsResponseList = new ArrayList<DomainResponse>();
        try {
            if (domains != null && !CollectionUtils.isEmpty(domains.getDomain())) {
                for (Domain domain : domains.getDomain()) {
                    EdivateLearn edivateLearn = null;
                    if (domain.getData() != null && domain.getData().getCustomization() != null
                            && domain.getData().getCustomization().getEdivatelearn() != null) {
                        edivateLearn = new EdivateLearn(
                                domain.getData().getCustomization().getEdivatelearn().getDomaintype() != null
                                        ? domain.getData().getCustomization().getEdivatelearn().getDomaintype()
                                        : "",
                                domain.getData().getCustomization().getEdivatelearn().getLicensetype() != null
                                        ? domain.getData().getCustomization().getEdivatelearn().getLicensetype()
                                        : "",
                                domain.getData().getCustomization().getEdivatelearn().getNooflicense() != null
                                        ? domain.getData().getCustomization().getEdivatelearn().getNooflicense()
                                        : "",
                                domain.getData().getCustomization().getEdivatelearn().getPooltype() != null
                                        ? domain.getData().getCustomization().getEdivatelearn().getPooltype()
                                        : "",
                                domain.getData().getCustomization().getEdivatelearn().isPilot(),
                                domain.getData().getCustomization().getEdivatelearn().getPilotstartdate() != null
                                        ? domain.getData().getCustomization().getEdivatelearn().getPilotstartdate()
                                        : "",
                                domain.getData().getCustomization().getEdivatelearn().getPilotenddate() != null
                                        ? domain.getData().getCustomization().getEdivatelearn().getPilotenddate()
                                        : "",
                                domain.getData().getCustomization().getEdivatelearn().isFullsubscription(),
                                domain.getData().getCustomization().getEdivatelearn()
                                        .getSubscriptionstartdate() != null
                                                ? domain.getData().getCustomization().getEdivatelearn()
                                                        .getSubscriptionstartdate()
                                                : "",
                                domain.getData().getCustomization().getEdivatelearn()
                                        .getSubscriptionenddate() != null
                                                ? domain.getData().getCustomization().getEdivatelearn()
                                                        .getSubscriptionenddate()
                                                : "");
                    } else {
                        edivateLearn = new EdivateLearn("", "", "", "", Boolean.FALSE, "", "", Boolean.FALSE, "",
                                "");
                    }
                    List<Securehash> securehashList = new ArrayList<Securehash>();
                    if (domain.getData() != null && domain.getData().getCustomization() != null
                            && domain.getData().getCustomization().getSecurehash() != null) {
                        for (com.sinet.gage.dlap.entities.Domains.Domain.Data.Customization.Securehash securehash : domain
                                .getData().getCustomization().getSecurehash()) {
                            List<Secretkey> secretkeyList = new ArrayList<>();
                            if (!CollectionUtils.isEmpty(securehash.getSecretkey())) {
                                for (com.sinet.gage.dlap.entities.Domains.Domain.Data.Customization.Securehash.Secretkey secretkey : securehash
                                        .getSecretkey()) {
                                    Secretkey secretkeyTemp = new Secretkey(secretkey.getName(),
                                            secretkey.get$value());

                                    secretkeyList.add(secretkeyTemp);
                                }
                            }

                            List<EncryptedData> encryptedDataList = new ArrayList<>();
                            if (!CollectionUtils.isEmpty(securehash.getEncryptedData())) {
                                for (com.sinet.gage.dlap.entities.Domains.Domain.Data.Customization.Securehash.EncryptedData encryptedData : securehash
                                        .getEncryptedData()) {
                                    CipherValue cipherValue = new CipherValue(
                                            encryptedData.getCipherData().getCipherValue().get$value());
                                    CipherData cipherData = new CipherData(cipherValue);
                                    EncryptionMethod encryptionMethod = new EncryptionMethod(
                                            encryptedData.getEncryptionMethod().getAlgorithm());
                                    EncryptedData encryptedDataTemp = new EncryptedData(encryptedData.getXmlns(),
                                            cipherData, encryptionMethod);

                                    encryptedDataList.add(encryptedDataTemp);
                                }
                            }
                            Securehash securehashTemp = new Securehash(secretkeyList, encryptedDataList);

                            securehashList.add(securehashTemp);
                        }
                    }

                    Files files = null;
                    if (domain.getData() != null && domain.getData().getCustomization() != null
                            && domain.getData().getCustomization().getFiles() != null
                            && !CollectionUtils.isEmpty(domain.getData().getCustomization().getFiles().getFile())) {
                        List<File> fileList = new ArrayList<>();
                        for (com.sinet.gage.dlap.entities.Domains.Domain.Data.Customization.Files.File file : domain
                                .getData().getCustomization().getFiles().getFile()) {
                            File fileTemp = new File(file.getType(), file.getPath());
                            fileList.add(fileTemp);
                        }
                        files = new Files(fileList);
                    }

                    ResourceBase resourceBase = null;
                    if (domain.getData() != null && domain.getData().getCustomization() != null
                            && domain.getData().getCustomization().getResourcebase() != null) {
                        resourceBase = new ResourceBase(
                                domain.getData().getCustomization().getResourcebase().get$value());
                    }
                    AllowViewInGpeerCompletion allowViewInGpeerCompletion = null;
                    if (domain.getData() != null && domain.getData().getCustomization() != null
                            && domain.getData().getCustomization().getAllowviewingpeercompletion() != null) {
                        allowViewInGpeerCompletion = new AllowViewInGpeerCompletion(
                                domain.getData().getCustomization().getAllowviewingpeercompletion().get$value());
                    }

                    LearningObjectives learningObjectives = null;
                    if (domain.getData() != null && domain.getData().getCustomization() != null
                            && domain.getData().getCustomization().getLearningobjectives() != null) {
                        learningObjectives = new LearningObjectives(
                                domain.getData().getCustomization().getLearningobjectives().getSetowner(),
                                domain.getData().getCustomization().getLearningobjectives().getLowgrade(),
                                domain.getData().getCustomization().getLearningobjectives().getHighgrade());
                    }

                    MenuItems menuItems = null;
                    if (domain.getData() != null && domain.getData().getCustomization() != null
                            && domain.getData().getCustomization().getMenuitems() != null && !CollectionUtils
                                    .isEmpty(domain.getData().getCustomization().getMenuitems().getMenuitem())) {
                        List<MenuItem> menuItemList = new ArrayList<>();
                        for (com.sinet.gage.dlap.entities.Domains.Domain.Data.Customization.MenuItems.MenuItem menuItem : domain
                                .getData().getCustomization().getMenuitems().getMenuitem()) {
                            MenuItem menuItemTemp = new MenuItem(menuItem.getType(), menuItem.getText(),
                                    menuItem.getUrl());
                            menuItemList.add(menuItemTemp);
                        }
                        menuItems = new MenuItems(menuItemList);
                    }

                    Strings strings = null;
                    if (domain.getData() != null && domain.getData().getCustomization() != null
                            && domain.getData().getCustomization().getStrings() != null
                            && domain.getData().getCustomization().getStrings().getString() != null) {
                        com.sinet.gage.dlap.entities.Customization.Strings.String string = new com.sinet.gage.dlap.entities.Customization.Strings.String(
                                domain.getData().getCustomization().getStrings().getString().getName(),
                                domain.getData().getCustomization().getStrings().getString().get$value());
                        strings = new Strings(string);
                    }

                    ItemMetadata itemMetadata = null;
                    if (domain.getData() != null && domain.getData().getCustomization() != null
                            && domain.getData().getCustomization().getItemMetadata() != null) {
                        Choice choice = new Choice(
                                domain.getData().getCustomization().getItemMetadata().getValue().getChoice()
                                        .getValue(),
                                domain.getData().getCustomization().getItemMetadata().getValue().getChoice()
                                        .getDisplay());
                        Value value = new Value(
                                domain.getData().getCustomization().getItemMetadata().getValue().getId(),
                                domain.getData().getCustomization().getItemMetadata().getValue().getDisplay(),
                                domain.getData().getCustomization().getItemMetadata().getValue().getInputtype(),
                                domain.getData().getCustomization().getItemMetadata().getValue().getSearchtype(),
                                domain.getData().getCustomization().getItemMetadata().getValue().getItemtype(),
                                domain.getData().getCustomization().getItemMetadata().getValue()
                                        .isAllowgroupoverride(),
                                domain.getData().getCustomization().getItemMetadata().getValue().getDefaultvalue(),
                                domain.getData().getCustomization().getItemMetadata().getValue().getMinvalue(),
                                domain.getData().getCustomization().getItemMetadata().getValue().getMaxvalue(),
                                choice);
                        itemMetadata = new ItemMetadata(value);
                    }

                    Authentication authentication = null;
                    if (domain.getData() != null && domain.getData().getCustomization() != null
                            && domain.getData().getCustomization().getAuthentication() != null) {
                        if (domain.getData().getCustomization().getAuthentication().getProvider() != null
                                && !CollectionUtils.isEmpty(
                                        domain.getData().getCustomization().getAuthentication().getProvider())) {
                            List<Provider> providerList = new ArrayList<>();
                            for (com.sinet.gage.dlap.entities.Domains.Domain.Data.Customization.Authentication.Provider provider : domain
                                    .getData().getCustomization().getAuthentication().getProvider()) {
                                ApplicationId applicationid = null;
                                if (provider.getApplicationid() != null) {
                                    applicationid = new ApplicationId(provider.getApplicationid().get$value());
                                }
                                EncryptedData encryptedDataTemp = null;
                                if (provider.getEncryptedData() != null) {
                                    CipherValue cipherValue = new CipherValue(provider.getEncryptedData()
                                            .getCipherData().getCipherValue().get$value());
                                    CipherData cipherData = new CipherData(cipherValue);
                                    EncryptionMethod encryptionMethod = new EncryptionMethod(
                                            provider.getEncryptedData().getEncryptionMethod().getAlgorithm());
                                    encryptedDataTemp = new EncryptedData(provider.getEncryptedData().getXmlns(),
                                            cipherData, encryptionMethod);
                                }

                                Server server = null;
                                if (provider.getServer() != null) {
                                    server = new Server(provider.getServer().get$value());
                                }
                                Logout logout = null;
                                if (provider.getLogout() != null) {
                                    logout = new Logout(provider.getLogout().get$value());
                                }
                                Provider providerTemp = new Provider(provider.getType(), applicationid,
                                        encryptedDataTemp, server, logout);
                                providerList.add(providerTemp);
                            }
                            authentication = new Authentication(
                                    domain.getData().getCustomization().getAuthentication().getLoginprefix(),
                                    providerList);
                        }
                    }

                    Customization customization = new Customization(edivateLearn, securehashList, files,
                            resourceBase, allowViewInGpeerCompletion, learningObjectives, menuItems, strings,
                            itemMetadata, authentication);
                    Data data = new Data(customization);
                    DomainResponse domainResponse = new DomainResponse(domain.getId(), domain.getName(),
                            domain.getUserspace(), domain.getParentid(), domain.getReference(), domain.getGuid(),
                            domain.getFlags(), domain.getCreationdate(), domain.getCreationby(),
                            domain.getModifieddate(), domain.getModifiedby(), domain.getVersion(), data);
                    domainsResponseList.add(domainResponse);
                }
            }
        } catch (Exception e) {
            LOGGER.error("Exception in tranferring from object: ", e);
        }

        return domainsResponseList;
    }

}