view src/main/java/de/mpiwg/itgroup/ismi/browse/FullEntityRepositoryBean.java @ 173:aa564b1b5e1f public_by_author

publicByAuthor feature ui for selecting texts ready. actual changing of public state not yet implemented.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Wed, 06 Jun 2018 21:01:05 +0200
parents 0b5d02012299
children 0d31c8be7c31
line wrap: on
line source

package de.mpiwg.itgroup.ismi.browse;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.mpi.openmind.cache.WrapperService;
import org.mpi.openmind.repository.bo.Attribute;
import org.mpi.openmind.repository.bo.Entity;

/**
 * EntityRepositoryBean for full Entities with Attributes and Relations loaded.
 * 
 * @author casties
 *
 */
public class FullEntityRepositoryBean extends EntityRepositoryBean {

    private static final long serialVersionUID = 8022526185079972610L;

    protected String sortAttributeName;

    protected boolean sortAttributeNumerically = false;

    /*
     * (non-Javadoc)
     * 
     * @see de.mpiwg.itgroup.ismi.browse.AbstractEntityRepositoryBean#
     * updateAdvancedEntities()
     */
    @Override
    protected void updateAdvancedEntities() {
        if (StringUtils.isNotEmpty(getObjectClass())) {
            this.advancedPaginator.initCount();
            int startRecord = this.advancedPaginator.getCurrentPage() * this.advancedPaginator.getItemsPerPage();
            if ((this.advancedPaginator.getCurrentPage() + 1) == this.advancedPaginator.getNumberOfPages()) {
                int mod = this.entities.size() % advancedPaginator.getItemsPerPage();
                if (mod == 0) {
                    this.currentEntities = entities.subList(startRecord,
                            startRecord + this.advancedPaginator.getItemsPerPage());
                } else {
                    this.currentEntities = entities.subList(startRecord, startRecord + mod);
                }

            } else {
                this.currentEntities = entities.subList(startRecord,
                        startRecord + this.advancedPaginator.getItemsPerPage());
            }
        } else {
            // empty object_class
            this.currentEntities = new ArrayList<Entity>();
        }
        // make sure all entities are loaded
        WrapperService store = getWrapper();
        for (Entity ent : this.currentEntities) {
            if (ent.isLightweight()) {
                store.getEntityContent(ent);
            }
        }
    }

    public String actionSortByAttributes() {
        try {
            this.sortByAttributes();
        } catch (Exception e) {
            printInternalError(e);
            logger.error(e.getMessage(), e);
        }
        return GOTO_ENTITY_REPOSITORY;
    }

    public void sortByAttributes() throws Exception {
        this.resultMode = MODE_ADVANCED;
        this.setPage("");
        this.entities = new ArrayList<Entity>();
        this.currentEntities = new ArrayList<Entity>();

        this.resultSummaryMsg = "";

        /*
         * run search and sort result (by attribute)
         */
        List<Entity> resultList = getWrapper().getEntitiesByDef(this.objectClass);
        // sort List (by ownvalue)
        Collections.sort(resultList,
                getEntityAttributeComparator(this.sortAttributeName, this.sortAttributeNumerically));
        this.entities = resultList;

        if (resultList.size() > 0) {
            this.resultSummaryMsg = resultList.size() + " items were found!";
            this.advancedPaginator.setCurrentPage(0);
            int entitiesCount = this.entities.size();
            this.advancedPaginator.resetNumberOfPages(entitiesCount);
            this.updateAdvancedEntities();
        } else {
            this.resultSummaryMsg = "No items were found!";
        }
    }

    public Comparator<Entity> getEntityAttributeComparator(final String attName, final boolean numerically) {
        return new Comparator<Entity>() {
            @Override
            public int compare(Entity e1, Entity e2) {
                if (e1.isLightweight()) {
                    e1 = getWrapper().getEntityContent(e1);
                }
                Attribute att1 = e1.getAttributeByName(attName);
                if (e2.isLightweight()) {
                    e2 = getWrapper().getEntityContent(e2);
                }
                Attribute att2 = e2.getAttributeByName(attName);
                if (att1 == null && att2 != null) {
                    return 1;
                } else if (att1 != null && att2 == null) {
                    return -1;
                } else if (att1 == null && att2 == null) {
                    return 0;
                }
                if (numerically) {
                    Integer a1 = null;
                    Integer a2 = null;
                    try {
                        a1 = Integer.parseInt(att1.getValue());
                    } catch (Exception e) {
                    }
                    try {
                        a2 = Integer.parseInt(att2.getValue());
                    } catch (Exception e) {
                    }
                    if (a1 == null && a2 != null) {
                        return 1;
                    } else if (a1 != null && a2 == null) {
                        return -1;
                    } else if (a1 == null && a2 == null) {
                        return 0;
                    }
                   return Integer.compare(a1, a2);
                } else {
                    return att1.getValue().compareTo(att2.getValue());
                }
            }
        };
    }

    /**
     * @return the sortAttributeName
     */
    public String getSortAttributeName() {
        return sortAttributeName;
    }

    /**
     * @param sortAttributeName
     *            the sortAttributeName to set
     */
    public void setSortAttributeName(String sortAttributeName) {
        this.sortAttributeName = sortAttributeName;
    }

    /**
     * @return the sortAttributeNumerical
     */
    public boolean isSortAttributeNumerically() {
        return sortAttributeNumerically;
    }

    /**
     * @param sortAttributeNumerical
     *            the sortAttributeNumerical to set
     */
    public void setSortAttributeNumerically(boolean sortAttributeNumerical) {
        this.sortAttributeNumerically = sortAttributeNumerical;
    }
}