view src/main/java/de/mpiwg/itgroup/ismi/browse/FullEntityRepositoryBean.java @ 190:b36a57a452a6

new Clean UI to find non-public references and witnesses and make them public.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Fri, 09 Nov 2018 15:13:32 +0100
parents 34ac2e1b323a
children 91f177641ec7
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;
import org.mpi.openmind.repository.bo.Relation;
import org.mpi.openmind.repository.services.utils.RelationFilter;

/**
 * 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;

    /**
     * updateAdvancedEntities() method that makes sure that the current Entities
     * are not lightweight.
     * 
     * @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;
    }

    /**
     * Loads all entities of this.objectClass and sorts by this.sortAttributeName.
     * 
     * Sort attributes as integer if this.sortAttributeNumerically.
     *  
     * @throws Exception
     */
    public void sortByAttributes() throws Exception {
        logger.debug("Start sortByAttributes...");
        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 attribute)
        Collections.sort(resultList,
                getEntityAttributeComparator(this.sortAttributeName, this.sortAttributeNumerically));

        updateEntities(resultList);
        logger.debug("Done sortByAttributes.");
    }

	/**
	 * Update paginator and summary message with new entities list.
	 * 
	 * @param resultList
	 */
	public void updateEntities(List<Entity> resultList) {
        this.entities = resultList;
		if (resultList.size() > 0) {
            int entitiesCount = this.entities.size();
            this.resultSummaryMsg = entitiesCount + " items were found!";
            this.advancedPaginator.setCurrentPage(0);
            this.advancedPaginator.resetNumberOfPages(entitiesCount);
            this.updateAdvancedEntities();
        } else {
            this.resultSummaryMsg = "No items were found!";
            this.currentEntities = resultList;
        }
	}

    /**
     * Returns Comparator for Entities that uses the Attribute attname.
     * 
     * Sorts as integer if numerically.
     * 
     * @param attName
     * @param numerically
     * @return
     */
    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());
                }
            }
        };
    }
    
    /**
     * Loads all entities of this.objectClass matching the given RelationFilters.
     * 
     * Filters Relations by relObjectClass and either srcObjectClass or tarObjectClass.
     * 
     * Requires all RelationFilters to match.
     *  
     * @throws Exception
     */
    public void searchByRelations(List<RelationFilter> relationFilters) {
        logger.debug("Start searchByRelations...");
        this.resultMode = MODE_ADVANCED;
        this.setPage("");
        this.entities = new ArrayList<Entity>();
        this.currentEntities = new ArrayList<Entity>();
        this.resultSummaryMsg = "";        
        
        /*
         * get all entities and filter result (by relation)
         */
        List<Entity> resultList = getWrapper().getEntitiesByDef(this.objectClass);
        for (Entity entity : resultList) {
        	if (entity.isLightweight()) {
        		entity = getWrapper().getEntityContent(entity);
        	}
        	boolean condFailed = false;
        	for (RelationFilter filter : relationFilters) {
        		if (filter.tarObjectClass != null) {
            		List<Relation> rels = entity.getSourceRelations(filter.relObjectClass, filter.tarObjectClass);
            		if (filter.relationMissing) {
            			// is the relation missing?
            			if (!rels.isEmpty()) {
            				condFailed = true;
            				break;
            			}
            		} else {
            			if (rels.isEmpty()) {
            				condFailed = true;
            				break;
            			}
            		}
        		} else if (filter.srcObjectClass != null) {
            		List<Relation> rels = entity.getTargetRelations(filter.relObjectClass, filter.srcObjectClass);
            		if (filter.relationMissing) {
            			// is the relation missing?
            			if (!rels.isEmpty()) {
            				condFailed = true;
            				break;
            			}
            		} else {
            			if (rels.isEmpty()) {
            				condFailed = true;
            				break;
            			}
            		}
        		}
        	}
        	if (!condFailed) {
        		// all conditions matched
        		entities.add(entity);
        	}
        }
        
        // sort List (by ownValue)
        Collections.sort(entities);
        
        updateEntities(entities);
        logger.debug("Done searchByRelations.");
    }

    /**
     * @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;
    }
}