use of org.jbei.ice.lib.group.GroupController in project ice by JBEI.
the class BulkUploads method create.
/**
* Creates a new bulk upload. If upload type is not bulk edit, then the status is set to in progress.
* Default permissions consisting of read permissions for the public groups that the requesting user is
* a part of are added
*
* @param userId identifier for user making request
* @param info bulk upload data
* @return created upload
*/
public BulkUploadInfo create(String userId, BulkUploadInfo info) {
Account account = accountController.getByEmail(userId);
BulkUpload upload = new BulkUpload();
upload.setName(info.getName());
upload.setAccount(account);
upload.setCreationTime(new Date());
upload.setLastUpdateTime(upload.getCreationTime());
if (info.getStatus() == BulkUploadStatus.BULK_EDIT) {
// only one instance of bulk edit is allowed to remain
clearBulkEdits(userId);
upload.setStatus(BulkUploadStatus.BULK_EDIT);
} else
upload.setStatus(BulkUploadStatus.IN_PROGRESS);
upload.setImportType(info.getType());
// set default permissions
GroupController groupController = new GroupController();
ArrayList<Group> publicGroups = groupController.getAllPublicGroupsForAccount(account);
for (Group group : publicGroups) {
Permission permission = new Permission();
permission.setCanRead(true);
permission.setUpload(upload);
permission.setGroup(group);
permission = DAOFactory.getPermissionDAO().create(permission);
upload.getPermissions().add(permission);
}
upload = dao.create(upload);
if (info.getEntryList() != null) {
for (PartData data : info.getEntryList()) {
Entry entry = entryDAO.get(data.getId());
// todo if entry is in another bulk upload, then update will fail
if (entry == null)
continue;
upload.getContents().add(entry);
}
}
dao.update(upload);
return upload.toDataTransferObject();
}
use of org.jbei.ice.lib.group.GroupController in project ice by JBEI.
the class EntriesAsCSV method writeList.
/**
* Iterate through list of entries and extract values
*
* @throws IOException on Exception write values to file
*/
private void writeList(EntryFieldLabel... fields) throws IOException {
// filter entries based on what the user is allowed to see if the user is not an admin
Account account = this.accountDAO.getByEmail(userId);
if (account.getType() != AccountType.ADMIN) {
List<Group> accountGroups = new GroupController().getAllGroups(account);
entries = permissionDAO.getCanReadEntries(account, accountGroups, entries);
}
if (entries == null) {
Logger.warn("No entries to convert to csv format");
return;
}
// write headers
Path tmpPath = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY));
File tmpFile = File.createTempFile("ice-", ".csv", tmpPath.toFile());
csvPath = tmpFile.toPath();
FileWriter fileWriter = new FileWriter(tmpFile);
if (fields == null || fields.length == 0)
fields = getEntryFields();
String[] headers = getCSVHeaders(fields);
Set<Long> sequenceSet = new HashSet<>();
try (CSVWriter writer = new CSVWriter(fileWriter)) {
writer.writeNext(headers);
// write entry fields
for (long entryId : entries) {
Entry entry = dao.get(entryId);
// get contents and write data out
String[] line = new String[fields.length + 4];
line[0] = entry.getCreationTime().toString();
line[1] = entry.getPartNumber();
// write field values
int i = 1;
for (EntryFieldLabel field : fields) {
line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
i += 1;
}
// write sequence information
if (this.includeSequences && sequenceDAO.hasSequence(entryId)) {
line[i + 1] = getSequenceName(entry);
sequenceSet.add(entryId);
} else {
line[i + 1] = "";
}
// get parents for entry
EntryLinks links = new EntryLinks(this.userId, entry.getPartNumber());
StringBuilder parents = new StringBuilder();
for (PartData data : links.getParents()) {
parents.append(data.getPartId()).append(",");
}
if (!StringUtils.isEmpty(parents.toString()))
parents = new StringBuilder(parents.substring(0, parents.length() - 1));
line[i + 2] = ("\"" + parents + "\"");
// write line
writer.writeNext(line);
}
}
if (!sequenceSet.isEmpty())
writeZip(sequenceSet);
}
use of org.jbei.ice.lib.group.GroupController in project ice by JBEI.
the class EntryPermissions method getEntryPermissions.
/**
* Retrieves permissions associated with a part. Requires that the requesting user has write permissions
* on the specified part
*
* @return list of available permissions for the specified part
* @throws PermissionException if the requesting user does not have write permissions for the part
*/
public List<AccessPermission> getEntryPermissions() {
// viewing permissions requires write permissions
authorization.expectWrite(userId, entry);
ArrayList<AccessPermission> accessPermissions = new ArrayList<>();
List<Permission> permissions = permissionDAO.getEntryPermissions(entry);
GroupController groupController = new GroupController();
Group publicGroup = groupController.createOrRetrievePublicGroup();
for (Permission permission : permissions) {
if (permission.getAccount() == null && permission.getGroup() == null)
continue;
if (permission.getGroup() != null && permission.getGroup() == publicGroup)
continue;
accessPermissions.add(permission.toDataTransferObject());
}
return accessPermissions;
}
use of org.jbei.ice.lib.group.GroupController in project ice by JBEI.
the class EntryCreator method createEntry.
/**
* Create an entry in the database.
* <p/>
* Generates a new Part Number, the record id (UUID), version id, and timestamps.
* Optionally set the record globally visible or schedule an index rebuild.
*
* @param account account of user creating entry
* @param entry entry record being created
* @param accessPermissions list of permissions to associate with created entry
* @return entry that was saved in the database.
*/
public Entry createEntry(Account account, Entry entry, ArrayList<AccessPermission> accessPermissions) {
if (entry.getRecordId() == null) {
entry.setRecordId(Utils.generateUUID());
entry.setVersionId(entry.getRecordId());
}
entry.setCreationTime(Calendar.getInstance().getTime());
entry.setModificationTime(entry.getCreationTime());
if (StringUtils.isEmpty(entry.getOwner()))
entry.setOwner(account.getFullName());
if (StringUtils.isEmpty(entry.getOwnerEmail()))
entry.setOwnerEmail(account.getEmail());
if (entry.getSelectionMarkers() != null) {
for (SelectionMarker selectionMarker : entry.getSelectionMarkers()) {
selectionMarker.setEntry(entry);
}
}
if (entry.getLinks() != null) {
for (Link link : entry.getLinks()) {
link.setEntry(entry);
}
}
if (entry.getStatus() == null)
entry.setStatus("");
if (entry.getBioSafetyLevel() == null)
entry.setBioSafetyLevel(0);
entry = dao.create(entry);
// check for pi
String piEmail = entry.getPrincipalInvestigatorEmail();
if (StringUtils.isNotEmpty(piEmail)) {
Account pi = DAOFactory.getAccountDAO().getByEmail(piEmail);
if (pi != null) {
// add write permission for the PI
addWritePermission(pi, entry);
}
}
// add write permissions for owner
addWritePermission(account, entry);
// add read permission for all public groups
ArrayList<Group> groups = new GroupController().getAllPublicGroupsForAccount(account);
for (Group group : groups) {
addReadPermission(null, group, entry);
}
if (accessPermissions != null) {
for (AccessPermission accessPermission : accessPermissions) {
if (accessPermission.getArticle() == AccessPermission.Article.ACCOUNT) {
Account accessAccount = DAOFactory.getAccountDAO().get(accessPermission.getArticleId());
// add account read permission
addReadPermission(accessAccount, null, entry);
} else {
// add group read permission
Group group = DAOFactory.getGroupDAO().get(accessPermission.getArticleId());
addReadPermission(null, group, entry);
}
}
}
// rebuild blast database
if (sequenceDAO.hasSequence(entry.getId())) {
BlastPlus.scheduleBlastIndexRebuildTask(true);
}
return entry;
}
use of org.jbei.ice.lib.group.GroupController in project ice by JBEI.
the class EntriesAsCSV method writeList.
/**
* Iterate through list of entries and extract values
*
* @param userId identifier of user making request
* @throws IOException on Exception write values to file
*/
private void writeList(String userId, EntryField... fields) throws IOException {
// filter entries based on what the user is allowed to see if the user is not an admin
Account account = this.accountDAO.getByEmail(userId);
if (account.getType() != AccountType.ADMIN) {
List<Group> accountGroups = new GroupController().getAllGroups(account);
entries = permissionDAO.getCanReadEntries(account, accountGroups, entries);
}
if (entries == null) {
Logger.warn("No entries to convert to csv format");
return;
}
// write headers
Path tmpPath = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY));
File tmpFile = File.createTempFile("ice-", ".csv", tmpPath.toFile());
csvPath = tmpFile.toPath();
FileWriter fileWriter = new FileWriter(tmpFile);
if (fields == null || fields.length == 0)
fields = getEntryFields();
String[] headers = getCSVHeaders(fields);
Set<Long> sequenceSet = new HashSet<>();
try (CSVWriter writer = new CSVWriter(fileWriter)) {
writer.writeNext(headers);
// write entry fields
for (long entryId : entries) {
Entry entry = dao.get(entryId);
// get contents and write data out
String[] line = new String[fields.length + 3];
line[0] = entry.getCreationTime().toString();
line[1] = entry.getPartNumber();
int i = 1;
for (EntryField field : fields) {
line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
i += 1;
}
if (this.includeSequences && sequenceDAO.hasSequence(entryId)) {
line[i + 1] = getSequenceName(entry);
sequenceSet.add(entryId);
} else {
line[i + 1] = "";
}
writer.writeNext(line);
}
}
writeZip(userId, sequenceSet);
}
Aggregations