Mercurial > hg > ismi-richfaces
view src/main/java/de/mpiwg/itgroup/ismi/merge/PublicByAuthorBean.java @ 176:c63d39034b60
PublicByAuthor feature with new "Select by public and MAMS" text-UI.
author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
---|---|
date | Fri, 08 Jun 2018 15:32:27 +0200 |
parents | 3d8b31508128 |
children | af1018d06443 |
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 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"); this.reset(); } public void reset(){ logger.info("PublicByAuthorBean.reset()"); browseBean = new FullEntityRepositoryBean(); browseBean.setObjectClass(PERSON); selectedPersonTexts = new ArrayList<Entity>(); selectedPersonSubjectMap = new HashMap<String,List<Entity>>(); makeSubjectTree(); 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() { subjectList = new ArrayList<String>(); subjectEntity = new HashMap<String,Entity>(); subjectParents = new HashMap<String,List<String>>(); subjectTexts = new HashMap<String,List<Entity>>(); subjectPublicTexts = new HashMap<String,List<Entity>>(); subjectPrivateTexts = new HashMap<String,List<Entity>>(); /* * 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); /* * 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); } } 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"); 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); } logger.debug("MAKE TEXT PUBLIC"); for (String msg : textMsg) { this.addGeneralMsg(msg); logger.debug(msg); } 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.debug("MAKE SUBJECT PUBLIC"); 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); } } 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.debug("MAKE PUBLIC BY MAMS AND SUBJECT"); if (maxMamsNr < 1) { addErrorMsg("MAMS number too small!"); return null; } /* * go through all texts for the subject */ 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); } catch (Exception e) { logger.error(e); } for (String msg : textMsg) { this.addGeneralMsg(msg); logger.debug(msg); } } 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 selectedPersonId */ public long getSelectedPersonId() { return selectedPersonId; } /** * @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 selectedPersonSubjects */ public List<String> getSelectedPersonSubjects() { return selectedPersonSubjects; } /** * @return the subjectParents */ public Map<String, List<String>> getSubjectParents() { return subjectParents; } /** * @return the subjectList */ public List<String> getSubjectList() { 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() { return subjectTexts; } /** * @return the subjectPublicTexts */ public Map<String, List<Entity>> getSubjectPublicTexts() { return subjectPublicTexts; } /** * @return the subjectPrivateTexts */ public Map<String, List<Entity>> getSubjectPrivateTexts() { return subjectPrivateTexts; } /** * @return the subjectEntity */ public Map<String, Entity> getSubjectEntity() { return subjectEntity; } }