use of org.jbei.ice.lib.dto.entry.PartData in project ice by JBEI.
the class CollectionEntriesTest method testGetEntriesByVisibility.
@Test
public void testGetEntriesByVisibility() throws Exception {
Account account = AccountCreator.createTestAccount("CollectionEntriesTest.testGetEntriesByVisibility", false);
Assert.assertNotNull(account);
long id = TestEntryCreator.createTestPart(account.getEmail());
Entry entry = DAOFactory.getEntryDAO().get(id);
entry.setVisibility(Visibility.DRAFT.getValue());
DAOFactory.getEntryDAO().update(entry);
CollectionEntries collectionEntries = new CollectionEntries(account.getEmail(), CollectionType.DRAFTS);
Results<PartData> results = collectionEntries.getEntries(ColumnField.CREATED, true, 0, 13);
Assert.assertNotNull(results);
Assert.assertEquals(1, results.getData().size());
Assert.assertEquals(1, results.getResultCount());
}
use of org.jbei.ice.lib.dto.entry.PartData in project ice by JBEI.
the class BulkCSVUpload method getBulkUploadDataFromFile.
// NOTE: this also validates the part data (with the exception of the actual files)
List<PartWithSample> getBulkUploadDataFromFile(InputStream inputStream) throws IOException {
List<PartWithSample> partDataList = new LinkedList<>();
// initialize parser to null; when not-null in the loop below, then the header has been parsed
CSVParser parser = null;
HashMap<Integer, HeaderValue> headers = null;
// parse CSV file
try {
LineIterator it = IOUtils.lineIterator(inputStream, "UTF-8");
int index = 0;
while (it.hasNext()) {
String line = it.nextLine().trim();
// check if first time parsing (first line)
if (parser == null) {
// to indicate the type of parser to use (tab or comma separated)
if (line.contains("\t") && !line.contains(","))
parser = new CSVParser('\t');
else
parser = new CSVParser();
// get column headers
String[] fieldStrArray = parser.parseLine(line);
headers = processColumnHeaders(fieldStrArray);
continue;
}
// skip any empty lines (holes) in the csv file
if (StringUtils.isBlank(line) || line.replaceAll(",", "").trim().isEmpty())
continue;
// at this point we must have headers since that should be the first item in the file
if (headers == null)
throw new IOException("Could not parse file headers");
// parser != null; process line contents with available headers
String[] valuesArray = parser.parseLine(line);
PartData partData = new PartData(addType);
PartSample partSample = null;
if (subType != null) {
partData.getLinkedParts().add(new PartData(subType));
}
// for each column
for (int i = 0; i < valuesArray.length; i += 1) {
HeaderValue headerForColumn = headers.get(i);
// process sample information
if (headerForColumn.isSampleField()) {
// todo : move to another method
if (partSample == null)
partSample = new PartSample();
setPartSampleData(((SampleHeaderValue) headerForColumn).getSampleField(), partSample, valuesArray[i]);
} else {
EntryHeaderValue entryHeaderValue = (EntryHeaderValue) headerForColumn;
EntryField field = entryHeaderValue.getEntryField();
PartData data;
String value = valuesArray[i];
boolean isSubType = entryHeaderValue.isSubType();
if (isSubType)
data = partData.getLinkedParts().get(0);
else
data = partData;
// get the data for the field
switch(field) {
case ATT_FILENAME:
ArrayList<AttachmentInfo> attachments = data.getAttachments();
if (attachments == null) {
attachments = new ArrayList<>();
data.setAttachments(attachments);
}
attachments.clear();
attachments.add(new AttachmentInfo(value));
break;
case SEQ_FILENAME:
data.setSequenceFileName(value);
break;
case SEQ_TRACE_FILES:
// todo
break;
case EXISTING_PART_NUMBER:
Entry entry = DAOFactory.getEntryDAO().getByPartNumber(value);
if (entry == null)
throw new IOException("Could not locate part number \"" + value + "\" for linking");
PartData toLink = entry.toDataTransferObject();
data.getLinkedParts().add(toLink);
break;
default:
partData = EntryUtil.setPartDataFromField(partData, value, field, isSubType);
}
}
}
// validate
List<EntryField> fields = EntryUtil.validates(partData);
if (!fields.isEmpty()) {
invalidFields.clear();
invalidFields.addAll(fields);
return null;
}
partData.setIndex(index);
PartWithSample partWithSample = new PartWithSample(partSample, partData);
partDataList.add(partWithSample);
index += 1;
}
} finally {
IOUtils.closeQuietly(inputStream);
}
return partDataList;
}
use of org.jbei.ice.lib.dto.entry.PartData in project ice by JBEI.
the class BulkEntryCreator method doUpdate.
protected PartData doUpdate(String userId, Entry entry, PartData data) {
if (entry == null)
return null;
entry = InfoToModelFactory.updateEntryField(data, entry);
if (entry == null)
return null;
entry.setModificationTime(new Date());
entry = entryDAO.update(entry);
data.setModificationTime(entry.getModificationTime().getTime());
// check if there is any linked parts. update if so (expect a max of 1)
if (data.getLinkedParts() == null || data.getLinkedParts().size() == 0)
return data;
// retrieve the entry (this is the only time you can create another entry on update)
// bulk upload can only link 1
PartData linkedPartData = data.getLinkedParts().get(0);
Entry linkedEntry = entryDAO.get(linkedPartData.getId());
if (linkedEntry == null && !StringUtils.isEmpty(linkedPartData.getPartId())) {
// try partId
linkedEntry = entryDAO.getByPartNumber(linkedPartData.getPartId());
linkedPartData.setId(linkedEntry.getId());
}
if (linkedEntry == null) {
linkedEntry = InfoToModelFactory.infoToEntry(linkedPartData);
if (linkedEntry != null) {
linkedEntry.setVisibility(Visibility.DRAFT.getValue());
Account account = accountController.getByEmail(userId);
linkedEntry.setOwner(account.getFullName());
linkedEntry.setOwnerEmail(account.getEmail());
linkedEntry = entryDAO.create(linkedEntry);
entry.getLinkedEntries().add(linkedEntry);
entryDAO.update(linkedEntry);
}
} else {
// linking to existing
EntryLinks entryLinks = new EntryLinks(userId, Long.toString(entry.getId()));
entryLinks.addLink(linkedPartData, LinkType.CHILD);
}
// recursively update
PartData linked = doUpdate(userId, linkedEntry, linkedPartData);
data.getLinkedParts().clear();
if (linked != null)
data.getLinkedParts().add(linked);
return data;
}
use of org.jbei.ice.lib.dto.entry.PartData in project ice by JBEI.
the class BulkEntryCreator method createEntries.
public boolean createEntries(String userId, long draftId, List<PartWithSample> data, HashMap<String, InputStream> files) {
BulkUpload draft = dao.get(draftId);
if (draft == null)
return false;
// check permissions
authorization.expectWrite(userId, draft);
SampleService sampleService = new SampleService();
EntryAuthorization entryAuthorization = new EntryAuthorization();
for (PartWithSample partWithSample : data) {
if (partWithSample == null)
continue;
PartData partData = partWithSample.getPartData();
if (partData == null)
continue;
Entry entry = InfoToModelFactory.infoToEntry(partData);
if (entry == null)
continue;
entry.setVisibility(Visibility.DRAFT.getValue());
Account account = accountController.getByEmail(userId);
entry.setOwner(account.getFullName());
entry.setOwnerEmail(account.getEmail());
// check if there is any linked parts. create if so (expect a max of 1)
if (partData.getLinkedParts() != null && partData.getLinkedParts().size() > 0) {
// create linked
PartData linked = partData.getLinkedParts().get(0);
// for existing the link already....exists so just verify
if (linked.getId() == 0) {
Entry linkedEntry = InfoToModelFactory.infoToEntry(linked);
if (linkedEntry != null) {
linkedEntry.setVisibility(Visibility.DRAFT.getValue());
linkedEntry.setOwner(account.getFullName());
linkedEntry.setOwnerEmail(account.getEmail());
linkedEntry = entryDAO.create(linkedEntry);
linked.setId(linkedEntry.getId());
linked.setModificationTime(linkedEntry.getModificationTime().getTime());
addWritePermission(account, linkedEntry);
// check for attachments and sequences for linked entry
saveFiles(linked, linkedEntry, files);
entry.getLinkedEntries().add(linkedEntry);
}
}
entry = entryDAO.create(entry);
// attempt to get linked entry and add
if (linked.getId() != 0) {
Entry linkedEntry = entryDAO.get(linked.getId());
if (linkedEntry != null && entryAuthorization.canWrite(userId, entry)) {
EntryLinks links = new EntryLinks(userId, Long.toString(entry.getId()));
links.addLink(linked, LinkType.CHILD);
}
}
} else {
entry = entryDAO.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);
draft.getContents().add(entry);
dao.update(draft);
// save files
saveFiles(partData, entry, files);
// save sample, if available
PartSample partSample = partWithSample.getPartSample();
if (partSample == null)
continue;
sampleService.createSample(userId, Long.toString(entry.getId()), partSample, null);
}
return true;
}
use of org.jbei.ice.lib.dto.entry.PartData in project ice by JBEI.
the class BulkEntryCreator method createEntryForUpload.
protected PartData createEntryForUpload(String userId, PartData data, BulkUpload upload) {
Entry entry = InfoToModelFactory.infoToEntry(data);
if (entry == null)
return null;
entry.setVisibility(Visibility.DRAFT.getValue());
Account account = accountController.getByEmail(userId);
entry.setOwner(account.getFullName());
entry.setOwnerEmail(account.getEmail());
// check if there is any linked parts.
if (data.getLinkedParts() != null && data.getLinkedParts().size() > 0) {
PartData linked = data.getLinkedParts().get(0);
// check if linking to existing
if (StringUtils.isEmpty(linked.getPartId())) {
// create new
Entry linkedEntry = InfoToModelFactory.infoToEntry(linked);
if (linkedEntry != null) {
linkedEntry.setVisibility(Visibility.DRAFT.getValue());
linkedEntry.setOwner(account.getFullName());
linkedEntry.setOwnerEmail(account.getEmail());
linkedEntry = entryDAO.create(linkedEntry);
linked.setId(linkedEntry.getId());
linked.setModificationTime(linkedEntry.getModificationTime().getTime());
data.getLinkedParts().clear();
data.getLinkedParts().add(linked);
// link to main entry in the database
entry.getLinkedEntries().add(linkedEntry);
}
} else {
// link existing
Entry linkedEntry = entryDAO.getByPartNumber(linked.getPartId());
if (!entry.getLinkedEntries().contains(linkedEntry)) {
entry.getLinkedEntries().add(linkedEntry);
}
}
}
entry = entryDAO.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);
upload.getContents().add(entry);
dao.update(upload);
data.setId(entry.getId());
data.setModificationTime(entry.getModificationTime().getTime());
return data;
}
Aggregations