public List<PartData> retrieveOwnerEntries( String userId, String ownerEmail, ColumnField sort, boolean asc, int start, int limit) { List<Entry> entries; Account account = DAOFactory.getAccountDAO().getByEmail(userId); if (authorization.isAdmin(userId) || account.getEmail().equals(ownerEmail)) { entries = dao.retrieveOwnerEntries(ownerEmail, sort, asc, start, limit); } else { Set<Group> accountGroups = new HashSet<>(account.getGroups()); GroupController controller = new GroupController(); Group everybodyGroup = controller.createOrRetrievePublicGroup(); accountGroups.add(everybodyGroup); // retrieve entries for user that can be read by others entries = dao.retrieveUserEntries(account, ownerEmail, accountGroups, sort, asc, start, limit); } ArrayList<PartData> data = new ArrayList<>(); for (Entry entry : entries) { PartData info = ModelToInfoFactory.createTableViewData(userId, entry, false); info.setViewCount(DAOFactory.getAuditDAO().getHistoryCount(entry)); data.add(info); } return data; }
public long updatePart(String userId, long partId, PartData part) { Entry existing = dao.get(partId); authorization.expectWrite(userId, existing); Entry entry = InfoToModelFactory.updateEntryField(part, existing); entry.getLinkedEntries().clear(); if (part.getLinkedParts() != null && part.getLinkedParts().size() > 0) { for (PartData data : part.getLinkedParts()) { Entry linked = dao.getByPartNumber(data.getPartId()); // check permissions on link if (!authorization.canRead(userId, linked)) { continue; } if (!canLink(entry, linked)) continue; entry.getLinkedEntries().add(linked); } } entry.setModificationTime(Calendar.getInstance().getTime()); if (entry.getVisibility() == Visibility.DRAFT.getValue()) { List<EntryField> invalidFields = EntryUtil.validates(part); if (invalidFields.isEmpty()) entry.setVisibility(Visibility.OK.getValue()); } entry = dao.update(entry); // check pi email String piEmail = entry.getPrincipalInvestigatorEmail(); if (StringUtils.isNotEmpty(piEmail)) { Account pi = DAOFactory.getAccountDAO().getByEmail(piEmail); if (pi != null) { // add write permission for the PI (method also checks to see if permission already exists) AccessPermission accessPermission = new AccessPermission(); accessPermission.setArticle(AccessPermission.Article.ACCOUNT); accessPermission.setArticleId(pi.getId()); accessPermission.setType(AccessPermission.Type.WRITE_ENTRY); accessPermission.setTypeId(entry.getId()); permissionsController.addPermission(userId, accessPermission); } } return entry.getId(); }
public List<PartData> getEntriesSharedWithUser( String userId, ColumnField field, boolean asc, int start, int limit) { Account account = DAOFactory.getAccountDAO().getByEmail(userId); GroupController groupController = new GroupController(); Group publicGroup = groupController.createOrRetrievePublicGroup(); Set<Group> accountGroups = account.getGroups(); accountGroups.remove(publicGroup); List<Entry> entries = dao.sharedWithUserEntries(account, accountGroups, field, asc, start, limit); ArrayList<PartData> data = new ArrayList<>(); for (Entry entry : entries) { PartData info = ModelToInfoFactory.createTableViewData(userId, entry, false); info.setViewCount(DAOFactory.getAuditDAO().getHistoryCount(entry)); data.add(info); } return data; }
/** * Moves the specified list of entries to the deleted folder * * @param userId unique identifier for user making the request. Must have write access privileges * on the entries in the list * @param list unique identifiers for entries * @return true or false if operation succeeds on all listed entries or not */ public boolean moveEntriesToTrash(String userId, ArrayList<PartData> list) { List<Entry> toTrash = new LinkedList<>(); for (PartData data : list) { Entry entry = dao.get(data.getId()); if (entry == null || !authorization.canWriteThoroughCheck(userId, entry)) return false; toTrash.add(entry); } // add to bin try { for (Entry entry : toTrash) { entry.setVisibility(Visibility.DELETED.getValue()); dao.update(entry); } } catch (DAOException de) { Logger.error(de); return false; } return true; }
/** * sets the corresponding fields in data only if they are not null * * @param data PartData object to converted to Entry * @param entry if null, a new entry is created otherwise entry is used * @return converted PartData object */ public static Entry updateEntryField(PartData data, Entry entry) { EntryType type = data.getType(); if (type == null) return entry; switch (type) { case PLASMID: entry = setPlasmidFields(data.getPlasmidData(), entry); break; case STRAIN: entry = setStrainFields(data.getStrainData(), entry); break; case PART: break; case ARABIDOPSIS: entry = setSeedFields(data.getArabidopsisSeedData(), entry); break; } entry = setCommon(entry, data); return entry; }
public static Entry infoToEntry(PartData info) { EntryType type = info.getType(); Entry entry; switch (type) { case PLASMID: entry = setPlasmidFields(info.getPlasmidData(), new Plasmid()); break; case STRAIN: entry = setStrainFields(info.getStrainData(), new Strain()); break; case ARABIDOPSIS: entry = setSeedFields(info.getArabidopsisSeedData(), new ArabidopsisSeed()); break; case PART: default: entry = new Part(); break; } if (entry == null) return null; // common fields if (StringUtils.isEmpty(info.getRecordId())) entry.setRecordId(UUID.randomUUID().toString()); else entry.setRecordId(info.getRecordId()); entry.setVersionId(entry.getRecordId()); if (info.getCreationTime() == 0) entry.setCreationTime(new Date()); else entry.setCreationTime(new Date(info.getCreationTime())); entry.setModificationTime(entry.getCreationTime()); entry = setCommon(entry, info); return entry; }
protected PartData retrieveEntryDetails(String userId, Entry entry) { // user must be able to read if not public entry if (!permissionsController.isPubliclyVisible(entry)) authorization.expectRead(userId, entry); PartData partData = ModelToInfoFactory.getInfo(entry); if (partData == null) return null; boolean hasSequence = sequenceDAO.hasSequence(entry.getId()); partData.setHasSequence(hasSequence); boolean hasOriginalSequence = sequenceDAO.hasOriginalSequence(entry.getId()); partData.setHasOriginalSequence(hasOriginalSequence); // permissions partData.setCanEdit(authorization.canWriteThoroughCheck(userId, entry)); partData.setPublicRead(permissionsController.isPubliclyVisible(entry)); // create audit event if not owner // todo : remote access check if (userId != null && authorization.getOwner(entry) != null && !authorization.getOwner(entry).equalsIgnoreCase(userId)) { try { Audit audit = new Audit(); audit.setAction(AuditType.READ.getAbbrev()); audit.setEntry(entry); audit.setUserId(userId); audit.setLocalUser(true); audit.setTime(new Date(System.currentTimeMillis())); auditDAO.create(audit); } catch (Exception e) { Logger.error(e); } } // retrieve more information about linked entries if any (default only contains id) if (partData.getLinkedParts() != null) { ArrayList<PartData> newLinks = new ArrayList<>(); for (PartData link : partData.getLinkedParts()) { Entry linkedEntry = dao.get(link.getId()); if (!authorization.canRead(userId, linkedEntry)) continue; link = ModelToInfoFactory.createTipView(linkedEntry); Sequence sequence = sequenceDAO.getByEntry(linkedEntry); if (sequence != null) { link.setBasePairCount(sequence.getSequence().length()); link.setFeatureCount(sequence.getSequenceFeatures().size()); } newLinks.add(link); } partData.getLinkedParts().clear(); partData.getLinkedParts().addAll(newLinks); } // check if there is a parent available List<Entry> parents = dao.getParents(entry.getId()); if (parents == null) return partData; for (Entry parent : parents) { if (!authorization.canRead(userId, parent)) continue; if (parent.getVisibility() != Visibility.OK.getValue() && !authorization.canWriteThoroughCheck(userId, entry)) continue; EntryType type = EntryType.nameToType(parent.getRecordType()); PartData parentData = new PartData(type); parentData.setId(parent.getId()); parentData.setName(parent.getName()); parentData.setVisibility(Visibility.valueToEnum(parent.getVisibility())); partData.getParents().add(parentData); } return partData; }
/** * Retrieves and sets the default values for the entry. Some of these values (e.g. PI, and Funding * Source) are set by individual users as part of their personal preferences * * @param userId Unique identifier for user requesting the values. * @param type entry type * @return PartData object with the retrieve part defaults */ public PartData getPartDefaults(String userId, EntryType type) { PartData partData = new PartData(type); PreferencesController preferencesController = new PreferencesController(); // pi defaults String value = preferencesController.getPreferenceValue( userId, PreferenceKey.PRINCIPAL_INVESTIGATOR.name()); if (value != null) { Account piAccount = accountController.getByEmail(value); if (piAccount == null) { partData.setPrincipalInvestigator(value); } else { partData.setPrincipalInvestigator(piAccount.getFullName()); partData.setPrincipalInvestigatorEmail(piAccount.getEmail()); partData.setPrincipalInvestigatorId(piAccount.getId()); } } // funding source defaults value = preferencesController.getPreferenceValue(userId, PreferenceKey.FUNDING_SOURCE.name()); if (value != null) { partData.setFundingSource(value); } // owner and creator details Account account = accountController.getByEmail(userId); if (account != null) { partData.setOwner(account.getFullName()); partData.setOwnerEmail(account.getEmail()); partData.setCreator(partData.getOwner()); partData.setCreatorEmail(partData.getOwnerEmail()); } // set the entry type defaults return EntryUtil.setPartDefaults(partData); }
private static Entry setCommon(Entry entry, PartData info) { if (entry == null || info == null) return null; if (info.getName() != null) entry.setName(info.getName()); if (info.getSelectionMarkers() != null) { Set<SelectionMarker> markers = getSelectionMarkers(info.getSelectionMarkers(), entry); entry.setSelectionMarkers(markers); } if (info.getReferences() != null) entry.setReferences(info.getReferences()); if (StringUtils.isBlank(entry.getPartNumber())) entry.setPartNumber(info.getPartId()); Date currentTime = new Date(); if (entry.getCreationTime() == null) entry.setCreationTime(currentTime); entry.setModificationTime(currentTime); if (info.getOwnerEmail() != null) { entry.setOwnerEmail(info.getOwnerEmail()); } if (info.getOwner() != null) entry.setOwner(info.getOwner()); if (info.getCreatorEmail() != null) { entry.setCreatorEmail(info.getCreatorEmail()); } if (info.getCreator() != null) entry.setCreator(info.getCreator()); if (info.getStatus() == null) { if (StringUtils.isBlank(entry.getStatus())) entry.setStatus(""); } else entry.setStatus(info.getStatus()); if (info.getAlias() != null) entry.setAlias(info.getAlias()); if (info.getBioSafetyLevel() == null) { if (entry.getBioSafetyLevel() == null) entry.setBioSafetyLevel(0); } else entry.setBioSafetyLevel(info.getBioSafetyLevel()); if (info.getShortDescription() != null) entry.setShortDescription(info.getShortDescription()); if (info.getLongDescription() != null) entry.setLongDescription(info.getLongDescription()); if (info.getIntellectualProperty() != null) entry.setIntellectualProperty(info.getIntellectualProperty()); Set<Link> links = getLinks(info.getLinks(), entry); entry.setLinks(links); Visibility visibility = info.getVisibility(); if (visibility != null) entry.setVisibility(visibility.getValue()); // checking for null instead of blank since it could be cleared if (info.getFundingSource() != null) entry.setFundingSource(info.getFundingSource()); if (info.getPrincipalInvestigator() != null) entry.setPrincipalInvestigator(info.getPrincipalInvestigator()); if (info.getPrincipalInvestigatorEmail() != null) entry.setPrincipalInvestigatorEmail(info.getPrincipalInvestigatorEmail()); if (info.getKeywords() != null) entry.setKeywords(info.getKeywords()); // parameters List<Parameter> parameters = getParameters(info.getCustomFields(), entry); entry.setParameters(parameters); return entry; }