jorgediazest.indexchecker.model.JournalArticleQuery.java Source code

Java tutorial

Introduction

Here is the source code for jorgediazest.indexchecker.model.JournalArticleQuery.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.indexchecker.model;

import com.liferay.portal.kernel.dao.orm.Criterion;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
import com.liferay.portal.kernel.dao.orm.ProjectionList;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.SearchException;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.PrefsPropsUtil;
import com.liferay.portal.kernel.workflow.WorkflowConstants;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import jorgediazest.util.data.Data;
import jorgediazest.util.data.DataUtil;
import jorgediazest.util.model.Model;
import jorgediazest.util.model.ModelUtil;
import jorgediazest.util.modelquery.ModelQueryFactory.DataComparatorFactory;
import jorgediazest.util.reflection.ReflectionUtil;
import jorgediazest.util.service.Service;

/**
 * @author Jorge Daz
 */
public class JournalArticleQuery extends IndexCheckerModelQuery {

    @Override
    public void fillDataObject(Data data, String[] attributes, Document doc) {
        super.fillDataObject(data, attributes, doc);

        if (indexAllVersions) {
            long id = DataUtil.getIdFromUID(doc.get(Field.UID));
            data.setPrimaryKey(id);
        }
    }

    public Map<Long, Data> getData(String[] attributes, String mapKeyAttribute, Criterion filter) throws Exception {

        if (indexAllVersions) {
            return super.getData(attributes, mapKeyAttribute, filter);
        }

        String[] attributes2 = new String[attributes.length];

        for (int i = 0; i < attributes.length; i++) {
            if (attributes[i].equals("version")) {
                attributes2[i] = "max(version)";
            } else {
                attributes2[i] = attributes[i];
            }
        }

        Criterion filterStatusApproved = getModel().generateCriterionFilter(
                "status=" + WorkflowConstants.STATUS_APPROVED + "+status=" + WorkflowConstants.STATUS_IN_TRASH);

        Criterion conjuctionFilterStatusApproved = ModelUtil.generateConjunctionQueryFilter(filter,
                filterStatusApproved);

        Map<Long, Data> dataMap = super.getData(attributes2, "resourcePrimKey", conjuctionFilterStatusApproved);

        Criterion filterStatusNotApproved = getModel().generateCriterionFilter(
                "status<>" + WorkflowConstants.STATUS_APPROVED + ",status<>" + WorkflowConstants.STATUS_IN_TRASH);

        Criterion conjuctionFilterStatusNotApproved = ModelUtil.generateConjunctionQueryFilter(filter,
                filterStatusNotApproved);

        Map<Long, Data> dataMapNotApproved = super.getData(attributes2, "resourcePrimKey",
                conjuctionFilterStatusNotApproved);

        for (Entry<Long, Data> entry : dataMapNotApproved.entrySet()) {
            if (!dataMap.containsKey(entry.getKey())) {
                dataMap.put(entry.getKey(), entry.getValue());
            }
        }

        Map<Long, Data> dataMapResult = new HashMap<Long, Data>();

        for (Data data : dataMap.values()) {
            dataMapResult.put((Long) data.get(mapKeyAttribute), data);
        }

        return dataMapResult;
    }

    @Override
    public void init(Model model, DataComparatorFactory dataComparatorFactory) throws Exception {

        super.init(model, dataComparatorFactory);

        try {
            Class<?> journalServiceConfigurationValuesClass = model.getService().getClassLoader()
                    .loadClass("com.liferay.journal.configuration." + "JournalServiceConfigurationValues");

            String indexAllVersionsString = ReflectionUtil.getWrappedStaticString(
                    journalServiceConfigurationValuesClass, "JOURNAL_ARTICLE_INDEX_ALL_VERSIONS");

            indexAllVersions = GetterUtil.getBoolean(indexAllVersionsString);
        } catch (SystemException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Map<Data, String> reindex(Collection<Data> dataCollection) {

        Map<Long, Data> articles = new HashMap<Long, Data>();

        for (Data data : dataCollection) {
            articles.put(data.getResourcePrimKey(), data);
        }

        return super.reindex(articles.values());
    }

    @Override
    public void reindex(Data value) throws SearchException {
        getModel().getIndexerNullSafe().reindex(getModel().getClassName(), value.getResourcePrimKey());
    }

    protected boolean indexAllVersions;

}