view src/main/java/de/mpiwg/itgroup/ismi/merge/PublicByAuthorBean.java @ 180:0d31c8be7c31

new MissingRelations feature and UI. including searchByRelations() with Filter in FullEntityRepositoryBean.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Wed, 13 Jun 2018 14:57:13 +0200
parents ca83d67a2dc9
children 52aa06772336
line wrap: on
line source

package de.mpiwg.itgroup.ismi.merge;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.faces.event.ActionEvent;

import org.apache.log4j.Logger;
import org.mpi.openmind.repository.bo.Attribute;
import org.mpi.openmind.repository.bo.Entity;
import org.mpi.openmind.repository.bo.Relation;

import de.mpiwg.itgroup.ismi.browse.EntityRepositoryBean;
import de.mpiwg.itgroup.ismi.browse.FullEntityRepositoryBean;
import de.mpiwg.itgroup.ismi.entry.beans.AbstractISMIBean;
import de.mpiwg.itgroup.ismi.entry.utils.PrivacityUtils;

public class PublicByAuthorBean extends AbstractISMIBean implements Serializable{
	
	private static Logger logger = Logger.getLogger(PublicByAuthorBean.class);
	
	private static final long serialVersionUID = 1L;
	
	protected String findAuthorName = "";
	protected Long selectedPersonId = 0l;
	protected Integer maxMamsNr = 0;
	
	protected Entity selectedPerson;
	protected List<Entity> selectedPersonTexts;
	protected List<String> selectedPersonSubjects;
    protected Map<String,List<Entity>> selectedPersonSubjectMap;

    protected List<String> subjectList;
    protected Map<String,List<String>> subjectParents;
    protected Map<String,List<Entity>> subjectTexts;
    protected Map<String,List<Entity>> subjectPublicTexts;
    protected Map<String,List<Entity>> subjectPrivateTexts;
    protected Map<String,Entity> subjectEntity;
	
	private FullEntityRepositoryBean browseBean;
	
	
	public PublicByAuthorBean(){
		//logger.info("AdvancedSearchBean");
	    browseBean = new FullEntityRepositoryBean();
	    browseBean.setObjectClass(PERSON);
	    
	    selectedPersonTexts = new ArrayList<Entity>();
	    selectedPersonSubjectMap = new HashMap<String,List<Entity>>();
	}
	
	public synchronized void reset(){
		logger.info("PublicByAuthorBean.reset()");
	    browseBean = new FullEntityRepositoryBean();
	    browseBean.setObjectClass(PERSON);
	    
	    selectedPersonTexts = new ArrayList<Entity>();
	    selectedPersonSubjectMap = new HashMap<String,List<Entity>>();
	    
	    makeSubjectTree();
	    updateSubjectTexts();
        logger.info("PublicByAuthorBean.reset() Done.");
	}
	
	/**
	 * Creates the subjectParents Map with all subject names as keys and 
	 * a List with their parents as values.
	 * 
	 * Also creates the subjectList List of subject names.
	 */
	protected void makeSubjectTree() {
        logger.debug("Start makeSubjectTree...");
	    subjectList = new ArrayList<String>();
	    subjectEntity = new HashMap<String,Entity>();
	    subjectParents = new HashMap<String,List<String>>();
	    /*
	     * get all subjects
	     */
	    List<Entity> subjects = getWrapper().getEntitiesByDef(SUBJECT);
	    for (Entity subject : subjects) {
	        if (subject.isLightweight()) {
	            subject = getWrapper().getEntityContent(subject);
	        }
	        /*
	         * add subject
	         */
            String subjectName = subject.getOwnValue();
            subjectList.add(subjectName);
            subjectEntity.put(subjectName, subject);
	        ArrayList<String> parents = new ArrayList<String>();
	        Entity parent = subject;
	        int cnt = 0; 
	        /*
	         * find subject's parents
	         */
	        do {
	            if (parent.isLightweight()) {
	                parent = getWrapper().getEntityContent(parent);
	            }
	            List<Relation> parRels = parent.getSourceRelations(is_part_of, SUBJECT);
	            if (!parRels.isEmpty()) {
	                Long parentId = parRels.get(0).getTargetId();
	                parent = getWrapper().getEntityById(parentId);
	                parents.add(parent.getOwnValue());
	            } else {
	                break;
	            }
	        } while (++cnt < 5);
	        subjectParents.put(subjectName, parents);
	    }
        logger.debug("Done makeSubjectTree.");
	}

    /**
     * update subjectTexts, subjectPublicTexts and subjectPrivateTexts.
     * 
     */
    private void updateSubjectTexts() {
        logger.debug("Start updateSubjectTexts...");
        subjectTexts = new HashMap<String,List<Entity>>();
        subjectPublicTexts = new HashMap<String,List<Entity>>();
        subjectPrivateTexts = new HashMap<String,List<Entity>>();
        for (Entry<String, Entity> subjectItem : subjectEntity.entrySet()) {
            String subjectName = subjectItem.getKey();
            Entity subject = subjectItem.getValue();
            /*
             * find subject's texts
             */
            ArrayList<Entity> textList = new ArrayList<Entity>();
            ArrayList<Entity> pubTextList = new ArrayList<Entity>();
            ArrayList<Entity> privTextList = new ArrayList<Entity>();
            for (Relation textRel : subject.getTargetRelations(has_subject, TEXT)) {
                Long textId = textRel.getSourceId();
                Entity text = getWrapper().getEntityByIdWithContent(textId);
                textList.add(text);
                if (text.getIsPublic()) {
                    pubTextList.add(text);
                } else {
                    privTextList.add(text);
                }
            }
            subjectTexts.put(subjectName, textList);
            subjectPublicTexts.put(subjectName, pubTextList);
            subjectPrivateTexts.put(subjectName, privTextList);
        }
        logger.debug("Done updateSubjectTexts...");
    }	
	
	public void actionReset() {
	    reset();
	}
	
    public void listenerAuthorIdSearch(ActionEvent event) {
        setSelectedPersonById();
	}

    protected void setSelectedPersonById() {
        Entity ent = getWrapper().getEntityById(selectedPersonId);
        if (ent != null) {
            ent = getWrapper().getEntityContent(ent);
            selectedPerson = ent;
            updateSelectedPerson();
        }
    }
	
	/**
	 * Updates the information in selectedPersonTexts, selectedPersonSubjects
	 * and selectedPersonSubjectMap.
	 */
	public void updateSelectedPerson() {
	    // load all texts by this author
	    selectedPersonTexts = new ArrayList<Entity>();
	    selectedPersonSubjectMap = new HashMap<String,List<Entity>>();
	    List<Relation> textRels = selectedPerson.getTargetRelations("was_created_by", "TEXT");
	    for (Relation rel : textRels) {
	        Long textID = rel.getSourceId();
	        // get text
	        Entity text = getWrapper().getEntityByIdWithContent(textID);
	        selectedPersonTexts.add(text);
	        // get subject
	        List<Relation> subjectRels = text.getSourceRelations("has_subject", "SUBJECT");
            if (subjectRels.isEmpty()) {
                String subject = "NO SUBJECT";
                List<Entity> texts = selectedPersonSubjectMap.get(subject);
                if (texts == null) {
                    texts = new ArrayList<Entity>();
                }
                // add text to list
                texts.add(text);
                // add list to subject map
                selectedPersonSubjectMap.put(subject, texts);

            } else {
                for (Relation subjectRel : subjectRels) {
                    Long subjectId = subjectRel.getTargetId();
                    Entity subject = getWrapper().getEntityByIdWithContent(subjectId);
                    List<Entity> texts = selectedPersonSubjectMap.get(subject.getOwnValue());
                    if (texts == null) {
                        texts = new ArrayList<Entity>();
                    }
                    // add text to list
                    texts.add(text);
                    // add list to subject map
                    selectedPersonSubjectMap.put(subject.getOwnValue(), texts);
                }
            }
	    }
        // update list of subjects
	    selectedPersonSubjects = new ArrayList<String>();
	    for (String sub : selectedPersonSubjectMap.keySet()) {
	        selectedPersonSubjects.add(sub);
	    }
	}
	

	public void actionAllAuthors() {
	    browseBean.setObjectClass(PERSON);
	    browseBean.setSortAttributeName("mams_number");
	    browseBean.setSortAttributeNumerically(true);
	    try {
            browseBean.sortByAttributes();
        } catch (Exception e) {
            logger.error(e);
        }
	}
	
    public String actionSelectPerson() {
        Entity entity = (Entity) getRequestBean("entity");
        selectedPersonId = entity.getId();
        setSelectedPersonById();
        // switch tab
        getSessionBean().setSelectedPublicByAuthorTab("autsub");
        return null;
     }

    
    /**
     * Makes the selected text and its related objects public.
     * Uses PrivacityUtils.setTextAndMorePrivacity().
     * @return
     */
    public String actionMakeTextAndRelatedPublic() {
        Entity text = (Entity) getRequestBean("text");
        logger.info("MAKE PUBLIC by text for text="+text.getOwnValue());
        List<String> textMsg = new ArrayList<String>();
        try {
            List<Entity> entities = PrivacityUtils.setTextAndMorePrivacity(text, true, textMsg, getWrapper());
            // save only public state
            getWrapper().saveEntityListAsNodeWithoutContent(entities, null);
        } catch (Exception e) {
            logger.error(e);
        }
        for (String msg : textMsg) {
            this.addGeneralMsg(msg);
            logger.debug(msg);
        }
        addGeneralMsg("The text \""+text.getOwnValue()+"\" was made public!");            
        return null;
    }
    
    /**
     * Makes the selected subject and its texts and related objects public.
     * Uses PrivacityUtils.setTextAndMorePrivacity().
     * @return
     */
    public String actionMakePersonSubjectAndRelatedPublic() {
        String subject = (String) getRequestBean("subject");
        logger.info("MAKE PUBLIC by author and subject for author="+selectedPerson.getOwnValue()+" and subject="+subject);
        int pubCnt = 0;
        List<Entity> texts = selectedPersonSubjectMap.get(subject);
        for (Entity text : texts) {
            List<String> textMsg = new ArrayList<String>();
            try {
                List<Entity> entities = PrivacityUtils.setTextAndMorePrivacity(text, true, textMsg, getWrapper());
                // save only public state
                getWrapper().saveEntityListAsNodeWithoutContent(entities, null);
            } catch (Exception e) {
                logger.error(e);
            }
            for (String msg : textMsg) {
                this.addGeneralMsg(msg);
                logger.debug(msg);
            }
            pubCnt += 1;
        }
        addGeneralMsg(pubCnt+" texts with subject "+subject+" were made public!");            
        return null;
    }
    
    /**
     * Makes the selected subject and its texts and related objects public.
     * Uses PrivacityUtils.setTextAndMorePrivacity().
     * @return
     */
    public String actionMakeMamsSubjectAndRelatedPublic() {
        String subject = (String) getRequestBean("subject");
        logger.info("MAKE PUBLIC by MAMS and subject for subject="+subject+" and maxMams="+maxMamsNr);
        if (maxMamsNr < 1) {
            addErrorMsg("MAMS number too small!");
            return null;
        }
        /*
         * go through all texts for the subject
         */
        int pubCnt = 0;
        List<Entity> texts = subjectTexts.get(subject);
        for (Entity text : texts) {
            if (text.isLightweight()) {
                text = getWrapper().getEntityContent(text);
            }
            /*
             * check author's MAMS number
             */
            List<Relation> authorRels = text.getSourceRelations(rel_was_created_by, PERSON);
            if (authorRels.isEmpty()) {
                // skip text with no author
                continue;
            }
            long authorId = authorRels.get(0).getTargetId();
            Entity author = getWrapper().getEntityByIdWithContent(authorId);
            try {
                Attribute mamsAtt = author.getAttributeByName("mams_number");
                int mamsNr = Integer.parseInt(mamsAtt.getValue());
                if (mamsNr > maxMamsNr) {
                    // skip if MAMS number too big
                    continue;
                }
            } catch (Exception e) {
                // attribute missing or wrong
                continue;
            }
            /*
             * make text and related objects public
             */
            List<String> textMsg = new ArrayList<String>();
            try {
                List<Entity> entities = PrivacityUtils.setTextAndMorePrivacity(text, true, textMsg, getWrapper());
                // save only public state
                getWrapper().saveEntityListAsNodeWithoutContent(entities, null);
                pubCnt += 1;
            } catch (Exception e) {
                logger.error(e);
            }
            for (String msg : textMsg) {
                this.addGeneralMsg(msg);
                logger.debug(msg);
            }
        }
        if (pubCnt == 0) {
            addGeneralMsg("No text matched your criteria.");
        } else {
            addGeneralMsg(pubCnt+" texts with MAMS number < "+maxMamsNr+" were made public!");            
        }
        updateSubjectTexts();
        return null;
    }
    
	/**
	 * @return the findAuthorName
	 */
	public String getFindAuthorName() {
		return findAuthorName;
	}

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

    /**
     * @return the browseBean
     */
    public EntityRepositoryBean getBrowseBean() {
        return browseBean;
    }

    /**
     * @return the selectedPerson
     */
    public Entity getSelectedPerson() {
        return selectedPerson;
    }

    /**
     * @return the selectedPersonTexts
     */
    public List<Entity> getSelectedPersonTexts() {
        return selectedPersonTexts;
    }

    /**
     * @return the selectedPersonSubjects
     */
    public Map<String, List<Entity>> getSelectedPersonSubjectMap() {
        return selectedPersonSubjectMap;
    }

    /**
     * @param selectedPersonId the selectedPersonId to set
     */
    public void setSelectedPersonId(long selectedPersonId) {
        this.selectedPersonId = selectedPersonId;
    }

    /**
     * @return the selectedPersonId
     */
    public long getSelectedPersonId() {
        return selectedPersonId;
    }

    /**
     * @return the selectedPersonSubjects
     */
    public List<String> getSelectedPersonSubjects() {
        return selectedPersonSubjects;
    }
    
    /**
     * @return the subjectParents
     */
    public Map<String, List<String>> getSubjectParents() {
    	if (subjectParents == null) {
    	    makeSubjectTree();
    	    updateSubjectTexts();
    	}
        return subjectParents;
    }

    /**
     * @return the subjectList
     */
    public List<String> getSubjectList() {
    	if (subjectList == null) {
    	    makeSubjectTree();
    	    updateSubjectTexts();
    	}
        return subjectList;
    }

    /**
     * @return the maxMamsNr
     */
    public Integer getMaxMamsNr() {
        return maxMamsNr;
    }

    /**
     * @param maxMamsNr the maxMamsNr to set
     */
    public void setMaxMamsNr(Integer maxMamsNr) {
        this.maxMamsNr = maxMamsNr;
    }

    /**
     * @return the subjectTexts
     */
    public Map<String, List<Entity>> getSubjectTexts() {
    	if (subjectTexts == null) {
    	    makeSubjectTree();
    	    updateSubjectTexts();
    	}
        return subjectTexts;
    }

    /**
     * @return the subjectPublicTexts
     */
    public Map<String, List<Entity>> getSubjectPublicTexts() {
    	if (subjectPublicTexts == null) {
    	    makeSubjectTree();
    	    updateSubjectTexts();
    	}
        return subjectPublicTexts;
    }

    /**
     * @return the subjectPrivateTexts
     */
    public Map<String, List<Entity>> getSubjectPrivateTexts() {
    	if (subjectPrivateTexts == null) {
    	    makeSubjectTree();
    	    updateSubjectTexts();
    	}
        return subjectPrivateTexts;
    }

    /**
     * @return the subjectEntity
     */
    public Map<String, Entity> getSubjectEntity() {
    	if (subjectEntity == null) {
    	    makeSubjectTree();
    	    updateSubjectTexts();
    	}
        return subjectEntity;
    }

}