jorgediazest.missingrefchecker.output.MissingRefCheckerOutput.java Source code

Java tutorial

Introduction

Here is the source code for jorgediazest.missingrefchecker.output.MissingRefCheckerOutput.java

Source

/**
 * Copyright (c) 2015-present Jorge Daz All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package jorgediazest.missingrefchecker.output;

import com.liferay.portal.kernel.dao.search.ResultRow;
import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import javax.portlet.PortletConfig;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;

import jorgediazest.missingrefchecker.ref.MissingRefInfo;
import jorgediazest.missingrefchecker.ref.Reference;

import jorgediazest.util.model.Model;
import jorgediazest.util.model.TableInfo;
import jorgediazest.util.modelquery.ModelQuery;
import jorgediazest.util.output.OutputUtils;

/**
 * @author Jorge Daz
 */
public class MissingRefCheckerOutput {

    public static List<String> generateCSVOutputCheckReferences(PortletConfig portletConfig, Locale locale,
            List<MissingRefInfo> listMissingRefInfo) {

        if (listMissingRefInfo == null) {
            return null;
        }

        List<String> out = new ArrayList<String>();
        String[] headerKeys = new String[] { "output.entity-class", /*"output.entity-name",*/"output.attributes",
                "output.relation-type", "output.destination", "output.missing-references", "output.other" };

        List<String> headers = OutputUtils.getHeaders(portletConfig, locale, headerKeys);

        out.add(OutputUtils.getCSVRow(headers));

        for (MissingRefInfo missingRefInfo : listMissingRefInfo) {
            List<String> line = generateReferenceCells(locale, missingRefInfo.getReference(), false);

            Throwable throwable = missingRefInfo.getThrowable();
            Collection<String> missingReferences = missingRefInfo.getMissingReferences();

            if ((throwable != null) || (missingReferences == null)) {
                line.add("Error checking references");
            }

            if (throwable != null) {
                line.add("EXCEPTION: " + throwable.getClass() + " - " + throwable.getMessage());
            } else if (missingReferences != null) {
                line.add(StringUtil.merge(missingReferences));
            }

            out.add(OutputUtils.getCSVRow(line));
        }

        return out;
    }

    public static List<String> generateCSVOutputMappingList(PortletConfig portletConfig, Locale locale,
            Map<Reference, Reference> references) {

        if (references == null) {
            return null;
        }

        List<String> out = new ArrayList<String>();
        String[] headerKeys = new String[] { "output.entity-class", /*"output.entity-name",*/"output.attributes",
                "output.relation-type", "output.destination" };

        List<String> headers = OutputUtils.getHeaders(portletConfig, locale, headerKeys);

        out.add(OutputUtils.getCSVRow(headers));

        for (Reference reference : references.values()) {
            if (Validator.isNotNull(reference.getType())) {
                List<String> line = generateReferenceCells(locale, reference, false);

                out.add(OutputUtils.getCSVRow(line));
            }
        }

        return out;
    }

    public static SearchContainer<MissingRefInfo> generateSearchContainerCheckReferences(
            PortletConfig portletConfig, RenderRequest renderRequest, List<MissingRefInfo> listMissingRefInfo,
            PortletURL serverURL) throws SystemException {

        Locale locale = renderRequest.getLocale();

        String[] headerKeys = new String[] { "output.entity-class"/*,"output.entity-name"*/, "output.attributes",
                "output.relation-type", "output.destination", "output.missing-references", "output.other" };

        List<String> headers = OutputUtils.getHeaders(portletConfig, locale, headerKeys);

        SearchContainer<MissingRefInfo> searchContainer = new SearchContainer<MissingRefInfo>(renderRequest, null,
                null, SearchContainer.DEFAULT_CUR_PARAM, SearchContainer.MAX_DELTA, serverURL, headers, null);

        listMissingRefInfo = ListUtil.subList(listMissingRefInfo, searchContainer.getStart(),
                searchContainer.getEnd());

        searchContainer.setResults(listMissingRefInfo);

        List<ResultRow> resultRows = searchContainer.getResultRows();

        int numberOfRows = 0;

        for (MissingRefInfo missingRefInfo : listMissingRefInfo) {
            ResultRow row = generateSearchContainerRowCheckReferences(portletConfig, missingRefInfo, locale,
                    numberOfRows);

            if (row != null) {
                numberOfRows++;
                resultRows.add(row);
            }
        }

        searchContainer.setTotal(numberOfRows);

        return searchContainer;
    }

    public static SearchContainer<Reference> generateSearchContainerMappingList(PortletConfig portletConfig,
            RenderRequest renderRequest, List<Reference> referecesList, PortletURL serverURL)
            throws SystemException {

        Locale locale = renderRequest.getLocale();

        String[] headerKeys = new String[] { "output.entity-class", /*"output.entity-name",*/"output.attributes",
                "output.relation-type", "output.destination" };

        List<String> headers = OutputUtils.getHeaders(portletConfig, locale, headerKeys);

        SearchContainer<Reference> searchContainer = new SearchContainer<Reference>(renderRequest, null, null,
                SearchContainer.DEFAULT_CUR_PARAM, SearchContainer.MAX_DELTA, serverURL, headers, null);

        referecesList = ListUtil.subList(referecesList, searchContainer.getStart(), searchContainer.getEnd());

        searchContainer.setResults(referecesList);

        List<ResultRow> resultRows = searchContainer.getResultRows();

        int numberOfRows = 0;

        for (Reference reference : referecesList) {
            ResultRow row = generateSearchContainerRowMappingList(portletConfig, reference, locale, numberOfRows);

            if (row != null) {
                numberOfRows++;
                resultRows.add(row);
            }
        }

        searchContainer.setTotal(numberOfRows);

        return searchContainer;
    }

    public static Map<Model, SearchContainer<Reference>> generateSearchContainersMappingList(
            PortletConfig portletConfig, RenderRequest renderRequest, Map<Reference, Reference> references,
            PortletURL serverURL) throws SystemException {

        Map<Model, List<Reference>> referenceListMap = new TreeMap<Model, List<Reference>>();

        for (Reference reference : references.values()) {
            Model originModel = reference.getOriginModelQuery().getModel();

            if (!referenceListMap.containsKey(originModel)) {
                referenceListMap.put(originModel, new ArrayList<Reference>());
            }

            referenceListMap.get(originModel).add(reference);
        }

        Map<Model, SearchContainer<Reference>> searchContainerMap = new TreeMap<Model, SearchContainer<Reference>>();

        for (Entry<Model, List<Reference>> e : referenceListMap.entrySet()) {
            SearchContainer<Reference> sc = generateSearchContainerMappingList(portletConfig, renderRequest,
                    e.getValue(), serverURL);
            searchContainerMap.put(e.getKey(), sc);
        }

        return searchContainerMap;
    }

    protected static List<String> generateReferenceCells(Locale locale, Reference reference, boolean withTypes) {

        List<String> line = new ArrayList<String>();

        line.add(reference.getOriginModelQuery().getModel().getName());
        //line.add(reference.getOriginModel().getDisplayName(locale));

        if (withTypes) {
            line.add(reference.gerOriginAttributesWithTypes());
            line.add(reference.getType());
            line.add(reference.getDestinationWithTypes());
        } else {
            String originAttributes = reference.getOriginAttributes();

            ModelQuery originQuery = reference.getOriginModelQuery();

            if ((originAttributes.indexOf(',') == -1) && !originQuery.getModel().hasAttribute(originAttributes)) {

                TableInfo tableInfo = originQuery.getModel().getTableInfo(originAttributes);

                originAttributes = tableInfo.getName() + "." + originAttributes;
            }

            line.add(originAttributes);
            line.add(reference.getType());
            line.add(reference.getDestination());
        }

        return line;
    }

    protected static ResultRow generateSearchContainerRowCheckReferences(PortletConfig portletConfig,
            MissingRefInfo missingRefInfo, Locale locale, int numberOfRows) {

        ResultRow row = new com.liferay.taglib.search.ResultRow(missingRefInfo,
                missingRefInfo.getReference().getOrigin(), numberOfRows);

        List<String> line = generateReferenceCells(locale, missingRefInfo.getReference(), false);

        for (String cell : line) {
            row.addText(HtmlUtil.escape(cell.replace(",", ", ").replace("#", " #").replace("_", " _")));
        }

        Throwable throwable = missingRefInfo.getThrowable();
        Collection<String> missingReferences = missingRefInfo.getMissingReferences();

        if ((throwable != null) || (missingReferences == null)) {
            row.addText(HtmlUtil.escape("Error checking references"));
        }

        if (throwable != null) {
            row.addText(HtmlUtil.escape("EXCEPTION: " + throwable.getClass() + " - " + throwable.getMessage()));
        } else if (missingReferences != null) {
            row.addText(HtmlUtil.escape(StringUtil.merge(missingReferences).replace(",", ", ")));
            row.addText(StringPool.BLANK);
        }

        return row;
    }

    protected static ResultRow generateSearchContainerRowMappingList(PortletConfig portletConfig,
            Reference reference, Locale locale, int numberOfRows) {

        if (Validator.isNull(reference.getType())) {
            return null;
        }

        ResultRow row = new com.liferay.taglib.search.ResultRow(reference, reference.getOrigin(), numberOfRows);

        List<String> line = generateReferenceCells(locale, reference, true);

        for (String cell : line) {
            row.addText(HtmlUtil.escape(cell.replace(",", ", ")));
        }

        return row;
    }

}