Search in sources :

Example 1 with Comparison

use of jorgediazest.util.data.Comparison in project index-checker by jorgediaz-lr.

the class IndexCheckerOutput method generateCSVOutput.

public static List<String> generateCSVOutput(PortletConfig portletConfig, String title, Locale locale, boolean groupBySite, Map<Company, Long> companyProcessTime, Map<Company, Map<Long, List<Comparison>>> companyResultDataMap, Map<Company, String> companyError) {
    List<String> out = new ArrayList<>();
    ResourceBundle resourceBundle = portletConfig.getResourceBundle(locale);
    if (companyResultDataMap != null) {
        String[] headerKeys;
        if (groupBySite) {
            headerKeys = new String[] { "output.company", "output.groupid", "output.groupname", "output.entityclass", "output.entityname", "output.errortype", "output.count", "output.primarykeys" };
        } else {
            headerKeys = new String[] { "output.company", "output.entityclass", "output.entityname", "output.errortype", "output.count", "output.primarykeys" };
        }
        List<String> headers = OutputUtils.getHeaders(portletConfig, locale, headerKeys);
        out.add(OutputUtils.getCSVRow(headers));
    }
    for (Map.Entry<Company, Long> companyEntry : companyProcessTime.entrySet()) {
        Long processTime = companyEntry.getValue();
        Company company = companyEntry.getKey();
        String companyOutput = company.getCompanyId() + " - " + company.getWebId();
        if (companyResultDataMap != null) {
            Map<Long, List<Comparison>> resultDataMap = companyResultDataMap.get(company);
            int numberOfRows = 0;
            for (Map.Entry<Long, List<Comparison>> entry : resultDataMap.entrySet()) {
                String groupIdOutput = null;
                String groupNameOutput = null;
                if (groupBySite) {
                    try {
                        Group group = GroupLocalServiceUtil.fetchGroup(entry.getKey());
                        if (group == null) {
                            groupIdOutput = LanguageUtil.get(resourceBundle, "output.not-applicable-groupid");
                            groupNameOutput = LanguageUtil.get(resourceBundle, "output.not-applicable-groupname");
                        } else {
                            groupIdOutput = "" + group.getGroupId();
                            groupNameOutput = group.getName(locale);
                        }
                    } catch (Exception e) {
                        groupIdOutput = "" + entry.getKey();
                    }
                }
                for (Comparison comp : entry.getValue()) {
                    String lineError = OutputUtils.generateCSVRow(resourceBundle, comp, companyOutput, groupIdOutput, groupNameOutput, "error", locale, comp.getError(), -1);
                    if (lineError != null) {
                        numberOfRows++;
                        out.add(lineError);
                    }
                    for (String type : comp.getOutputTypes()) {
                        String attribute = "pk";
                        if (type.contains("right")) {
                            attribute = Field.UID;
                        }
                        String line = OutputUtils.generateCSVRow(resourceBundle, comp, companyOutput, groupIdOutput, groupNameOutput, type, attribute, locale);
                        if (line != null) {
                            numberOfRows++;
                            out.add(line);
                        }
                    }
                }
            }
            if (numberOfRows == 0) {
                out.add(StringPool.BLANK);
                out.add("No results found: your system is ok or perhaps you " + "have to change some filters");
            }
        }
        String errorMessage = companyError.get(company);
        if (Validator.isNotNull(errorMessage)) {
            out.add("Company: " + company.getCompanyId() + " - " + company.getWebId());
            out.add(errorMessage);
        }
        out.add(StringPool.BLANK);
        out.add("Executed " + title + " for company " + company.getCompanyId() + " in " + processTime + " ms");
        out.add(StringPool.BLANK);
    }
    Bundle bundle = FrameworkUtil.getBundle(IndexCheckerOutput.class);
    out.add("Version: " + bundle.getVersion());
    out.add(StringPool.BLANK);
    return out;
}
Also used : Group(com.liferay.portal.kernel.model.Group) Company(com.liferay.portal.kernel.model.Company) ResourceBundle(java.util.ResourceBundle) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) Comparison(jorgediazest.util.data.Comparison) ResourceBundle(java.util.ResourceBundle) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 2 with Comparison

use of jorgediazest.util.data.Comparison in project index-checker by jorgediaz-lr.

the class IndexCheckerPortlet method executeReindex.

public void executeReindex(ActionRequest request, ActionResponse response) throws Exception {
    PortalUtil.copyRequestParameters(request, response);
    EnumSet<ExecutionMode> executionMode = getExecutionMode(request);
    String[] filterClassNameArr = ParamUtil.getParameterValues(request, "filterClassName");
    response.setRenderParameter("filterClassName", new String[0]);
    request.setAttribute("filterClassNameSelected", SetUtil.fromArray(filterClassNameArr));
    String[] filterGroupIdArr = ParamUtil.getParameterValues(request, "filterGroupId");
    response.setRenderParameter("filterGroupId", new String[0]);
    request.setAttribute("filterGroupIdSelected", SetUtil.fromArray(filterGroupIdArr));
    List<String> classNames = getClassNames(filterClassNameArr);
    Date startModifiedDate = null;
    Date endModifiedDate = null;
    long filterModifiedDate = ParamUtil.getLong(request, "filterModifiedDate");
    if (filterModifiedDate > 0) {
        long now = System.currentTimeMillis();
        startModifiedDate = getStartDate(now, filterModifiedDate);
        endModifiedDate = getTomorrowDate(now);
    }
    Map<Company, Long> companyProcessTime = new LinkedHashMap<>();
    Map<Company, String> companyError = new LinkedHashMap<>();
    for (Company company : getCompanyList()) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            List<Long> groupIds = getGroupIds(company, executionMode, filterGroupIdArr);
            if ((groupIds != null) && groupIds.isEmpty()) {
                if (_log.isInfoEnabled()) {
                    _log.info("Skipping company " + company.getCompanyId() + " " + "because groupId list is empty");
                }
                continue;
            }
            long startTime = System.currentTimeMillis();
            Map<Long, List<Comparison>> resultDataMap = executeCheck(company, groupIds, classNames, startModifiedDate, endModifiedDate, executionMode, getNumberOfThreads(request));
            for (Map.Entry<Long, List<Comparison>> entry : resultDataMap.entrySet()) {
                List<Comparison> resultList = entry.getValue();
                for (Comparison result : resultList) {
                    Map<Data, String> errors = reindex(result);
                    /* TODO Mover todo esto al JSP */
                    if (((errors != null) && !errors.isEmpty()) || (result.getError() != null)) {
                        pw.println();
                        pw.println("----");
                        pw.println(result.getModelName());
                        pw.println("----");
                        for (Map.Entry<Data, String> e : errors.entrySet()) {
                            pw.println(" * " + e.getKey() + " - Exception: " + e.getValue());
                        }
                        pw.println(" * " + result.getError());
                    }
                }
            }
            long endTime = System.currentTimeMillis();
            companyProcessTime.put(company, endTime - startTime);
        } catch (Throwable t) {
            StringWriter swt = new StringWriter();
            PrintWriter pwt = new PrintWriter(swt);
            pwt.println("Error during execution: " + t.getMessage());
            t.printStackTrace(pwt);
            companyError.put(company, swt.toString());
            _log.error(t, t);
        }
        companyError.put(company, sw.toString());
    }
    request.setAttribute("companyError", companyError);
    request.setAttribute("companyProcessTime", companyProcessTime);
    request.setAttribute("executionMode", executionMode);
    request.setAttribute("title", "Reindex");
}
Also used : Company(com.liferay.portal.kernel.model.Company) LinkedHashMap(java.util.LinkedHashMap) StringWriter(java.io.StringWriter) Comparison(jorgediazest.util.data.Comparison) List(java.util.List) ArrayList(java.util.ArrayList) PrintWriter(java.io.PrintWriter) ExecutionMode(jorgediazest.indexchecker.ExecutionMode) Data(jorgediazest.util.data.Data) Date(java.util.Date) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 3 with Comparison

use of jorgediazest.util.data.Comparison in project index-checker by jorgediaz-lr.

the class IndexCheckerPortlet method dumpToLog.

public static void dumpToLog(boolean groupBySite, Map<Long, List<Comparison>> comparisonDataMap, Locale locale) {
    if (!_log.isInfoEnabled()) {
        return;
    }
    for (Map.Entry<Long, List<Comparison>> entry : comparisonDataMap.entrySet()) {
        String groupTitle = null;
        Group group = GroupLocalServiceUtil.fetchGroup(entry.getKey());
        if ((group == null) && groupBySite) {
            groupTitle = "N/A";
        } else if (group != null) {
            groupTitle = group.getGroupId() + " - " + group.getName(locale);
        }
        if (groupTitle != null) {
            _log.info("");
            _log.info("---------------");
            _log.info("GROUP: " + groupTitle);
            _log.info("---------------");
        }
        for (Comparison comparison : entry.getValue()) {
            comparison.dumpToLog();
        }
    }
}
Also used : Group(com.liferay.portal.kernel.model.Group) Comparison(jorgediazest.util.data.Comparison) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 4 with Comparison

use of jorgediazest.util.data.Comparison in project index-checker by jorgediaz-lr.

the class IndexCheckerOutput method generateSearchContainer.

public static SearchContainer<Comparison> generateSearchContainer(PortletConfig portletConfig, RenderRequest renderRequest, boolean groupBySite, Map<Long, List<Comparison>> resultDataMap, PortletURL serverURL) {
    Locale locale = renderRequest.getLocale();
    ResourceBundle resourceBundle = portletConfig.getResourceBundle(locale);
    String[] headerKeys;
    if (groupBySite) {
        headerKeys = new String[] { "output.groupid", "output.groupname", "output.entityclass", "output.entityname", "output.errortype", "output.count", "output.primarykeys" };
    } else {
        headerKeys = new String[] { "output.entityclass", "output.entityname", "output.errortype", "output.count", "output.primarykeys" };
    }
    List<String> headerNames = OutputUtils.getHeaders(portletConfig, locale, headerKeys);
    SearchContainer<Comparison> searchContainer = new SearchContainer<>(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, SearchContainer.MAX_DELTA, serverURL, headerNames, null);
    int numberOfRows = 0;
    for (Map.Entry<Long, List<Comparison>> entry : resultDataMap.entrySet()) {
        String groupIdOutput = null;
        String groupNameOutput = null;
        if (groupBySite) {
            Group group = GroupLocalServiceUtil.fetchGroup(entry.getKey());
            if (group == null) {
                groupIdOutput = LanguageUtil.get(resourceBundle, "output.not-applicable-groupid");
                groupNameOutput = LanguageUtil.get(resourceBundle, "output.not-applicable-groupname");
            } else {
                groupIdOutput = "" + group.getGroupId();
                groupNameOutput = group.getName(locale);
            }
        }
        List<Comparison> results = searchContainer.getResults();
        if ((results == null) || results.isEmpty()) {
            results = new ArrayList<>();
        }
        results.addAll(entry.getValue());
        results = ListUtil.subList(results, searchContainer.getStart(), searchContainer.getEnd());
        searchContainer.setResults(results);
        List<ResultRow> resultRows = searchContainer.getResultRows();
        for (Comparison comp : entry.getValue()) {
            ResultRow rowError = OutputUtils.generateSearchContainerRow(resourceBundle, comp, groupIdOutput, groupNameOutput, "error", locale, numberOfRows, comp.getError());
            if (rowError != null) {
                numberOfRows++;
                resultRows.add(rowError);
            }
            for (String type : comp.getOutputTypes()) {
                String attribute = "pk";
                if (type.contains("right")) {
                    attribute = Field.UID;
                }
                int maxSize = 0;
                ResultRow row = OutputUtils.generateSearchContainerRow(resourceBundle, comp, groupIdOutput, groupNameOutput, type, attribute, locale, numberOfRows, maxSize);
                if (row != null) {
                    numberOfRows++;
                    resultRows.add(row);
                }
            }
        }
    }
    searchContainer.setTotal(numberOfRows);
    return searchContainer;
}
Also used : Locale(java.util.Locale) ResultRow(com.liferay.portal.kernel.dao.search.ResultRow) Group(com.liferay.portal.kernel.model.Group) SearchContainer(com.liferay.portal.kernel.dao.search.SearchContainer) Comparison(jorgediazest.util.data.Comparison) ResourceBundle(java.util.ResourceBundle) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 5 with Comparison

use of jorgediazest.util.data.Comparison in project index-checker by jorgediaz-lr.

the class IndexCheckerPortlet method executeRemoveOrphans.

public void executeRemoveOrphans(ActionRequest request, ActionResponse response) throws Exception {
    PortalUtil.copyRequestParameters(request, response);
    EnumSet<ExecutionMode> executionMode = getExecutionMode(request);
    String[] filterClassNameArr = ParamUtil.getParameterValues(request, "filterClassName");
    response.setRenderParameter("filterClassName", new String[0]);
    request.setAttribute("filterClassNameSelected", SetUtil.fromArray(filterClassNameArr));
    String[] filterGroupIdArr = ParamUtil.getParameterValues(request, "filterGroupId");
    response.setRenderParameter("filterGroupId", new String[0]);
    request.setAttribute("filterGroupIdSelected", SetUtil.fromArray(filterGroupIdArr));
    List<String> classNames = getClassNames(filterClassNameArr);
    Map<Company, Long> companyProcessTime = new LinkedHashMap<>();
    Map<Company, String> companyError = new LinkedHashMap<>();
    for (Company company : getCompanyList()) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            List<Long> groupIds = getGroupIds(company, executionMode, filterGroupIdArr);
            if ((groupIds != null) && groupIds.isEmpty()) {
                if (_log.isInfoEnabled()) {
                    _log.info("Skipping company " + company.getCompanyId() + " " + "because groupId list is empty");
                }
                continue;
            }
            long startTime = System.currentTimeMillis();
            Map<Long, List<Comparison>> resultDataMap = executeCheck(company, groupIds, classNames, null, null, executionMode, getNumberOfThreads(request));
            for (Map.Entry<Long, List<Comparison>> entry : resultDataMap.entrySet()) {
                List<Comparison> resultList = entry.getValue();
                for (Comparison result : resultList) {
                    Map<Data, String> errors = removeIndexOrphans(result);
                    /* TODO Mover todo esto al JSP */
                    if (((errors != null) && !errors.isEmpty()) || (result.getError() != null)) {
                        pw.println();
                        pw.println("----");
                        pw.println(result.getModelName());
                        pw.println("----");
                        for (Map.Entry<Data, String> e : errors.entrySet()) {
                            pw.println(" * " + e.getKey() + " - Exception: " + e.getValue());
                        }
                        pw.println(" * " + result.getError());
                    }
                }
            }
            long endTime = System.currentTimeMillis();
            companyProcessTime.put(company, endTime - startTime);
        } catch (Throwable t) {
            StringWriter swt = new StringWriter();
            PrintWriter pwt = new PrintWriter(swt);
            pwt.println("Error during execution: " + t.getMessage());
            t.printStackTrace(pwt);
            companyError.put(company, swt.toString());
            _log.error(t, t);
        }
        companyError.put(company, sw.toString());
    }
    request.setAttribute("companyError", companyError);
    request.setAttribute("companyProcessTime", companyProcessTime);
    request.setAttribute("executionMode", executionMode);
    request.setAttribute("title", "Remove index orphan");
}
Also used : Company(com.liferay.portal.kernel.model.Company) ExecutionMode(jorgediazest.indexchecker.ExecutionMode) Data(jorgediazest.util.data.Data) LinkedHashMap(java.util.LinkedHashMap) StringWriter(java.io.StringWriter) Comparison(jorgediazest.util.data.Comparison) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) PrintWriter(java.io.PrintWriter)

Aggregations

ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 Comparison (jorgediazest.util.data.Comparison)7 LinkedHashMap (java.util.LinkedHashMap)5 TreeMap (java.util.TreeMap)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Company (com.liferay.portal.kernel.model.Company)4 Group (com.liferay.portal.kernel.model.Group)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 ExecutionMode (jorgediazest.indexchecker.ExecutionMode)3 Data (jorgediazest.util.data.Data)3 Date (java.util.Date)2 ResourceBundle (java.util.ResourceBundle)2 ResultRow (com.liferay.portal.kernel.dao.search.ResultRow)1 SearchContainer (com.liferay.portal.kernel.dao.search.SearchContainer)1 Locale (java.util.Locale)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1