use of org.jbei.ice.lib.dto.entry.EntryFieldLabel 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.dto.entry.EntryFieldLabel in project ice by JBEI.
the class EntriesAsCSV method getEntryFields.
private EntryFieldLabel[] getEntryFields() {
Set<String> recordTypes = new HashSet<>(dao.getRecordTypes(entries));
List<EntryFieldLabel> fields = EntryFields.getCommonFields();
for (String recordType : recordTypes) {
EntryType type = EntryType.nameToType(recordType);
if (type == null) {
Logger.error("Could not convert entry type " + recordType);
continue;
}
switch(type) {
case SEED:
EntryFields.addArabidopsisSeedHeaders(fields);
break;
case STRAIN:
EntryFields.addStrainHeaders(fields);
break;
case PLASMID:
EntryFields.addPlasmidHeaders(fields);
break;
case PROTEIN:
EntryFields.addProteinHeaders(fields);
break;
}
}
return fields.toArray(new EntryFieldLabel[0]);
}
use of org.jbei.ice.lib.dto.entry.EntryFieldLabel in project ice by JBEI.
the class EntriesAsCSV method customize.
public ByteArrayOutputStream customize(EntrySelection selection, SequenceFormat format, boolean onePerFolder) throws IOException {
Entries retriever = new Entries(this.userId);
this.entries = retriever.getEntriesFromSelectionContext(selection);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
for (long entryId : this.entries) {
// get the entry
Entry entry = DAOFactory.getEntryDAO().get(entryId);
if (entry == null) {
// write to csv file
Logger.error("ERROR : no entry " + entryId);
continue;
}
if (!sequenceDAO.hasSequence(entry.getId())) {
continue;
}
// get the sequence
InputStreamWrapper wrapper = new PartSequence(userId, Long.toString(entryId)).toFile(format, onePerFolder);
if (wrapper == null) {
Logger.error("ERROR : no sequence " + entryId);
continue;
}
if (onePerFolder)
wrapper.setName(entry.getPartNumber() + File.separatorChar + wrapper.getName());
else
wrapper.setName(wrapper.getName());
putZipEntry(wrapper, zos);
}
this.includeSequences = false;
writeList(selection.getFields().toArray(new EntryFieldLabel[0]));
// write the csv file to zip file
FileInputStream fis = new FileInputStream(csvPath.toFile());
InputStreamWrapper wrapper = new InputStreamWrapper(fis, "entries.csv");
putZipEntry(wrapper, zos);
}
return baos;
}
use of org.jbei.ice.lib.dto.entry.EntryFieldLabel in project ice by JBEI.
the class EntriesAsCSV method getCSVHeaders.
protected String[] getCSVHeaders(EntryFieldLabel[] fields) {
// get headers
String[] headers = new String[fields.length + 4];
headers[0] = "Created";
headers[1] = "Part ID";
int i = 1;
for (EntryFieldLabel field : fields) {
i += 1;
headers[i] = field.getLabel();
}
headers[i + 1] = "Sequence File";
headers[i + 2] = "Parent IDs";
return headers;
}
use of org.jbei.ice.lib.dto.entry.EntryFieldLabel in project ice by JBEI.
the class RemoteEntriesAsCSV method writeDataEntries.
protected void writeDataEntries(RemotePartner partner, List<PartData> entries, List<EntryFieldLabel> fields, CSVWriter writer, ZipOutputStream zos) {
if (entries == null)
return;
for (PartData partData : entries) {
String[] line = new String[fields.size() + 4];
line[0] = partner.getUrl();
line[1] = new Date(partData.getCreationTime()).toString();
line[2] = partData.getPartId();
int i = 2;
for (EntryFieldLabel field : fields) {
line[i + 1] = PartDataUtil.entryFieldToValue(partData, field);
i += 1;
}
// write sequence to zip file
if (partData.isHasSequence()) {
try {
// get remote sequence
FeaturedDNASequence featuredDNASequence = remoteEntries.getPublicEntrySequence(partner.getId(), Long.toString(partData.getId()));
if (featuredDNASequence != null) {
String name = partData.getPartId() + ".gb";
// write sequence to zip
line[i + 1] = name;
Sequence sequence = SequenceUtil.dnaSequenceToSequence(featuredDNASequence);
GenbankFormatter genbankFormatter = new GenbankFormatter(name);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
genbankFormatter.format(sequence, byteStream);
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteStream.toByteArray());
InputStreamWrapper wrapper = new InputStreamWrapper(inputStream, name);
putZipEntry(wrapper, zos);
} else {
line[i + 1] = "";
}
} catch (Exception e) {
line[i + 1] = "";
}
} else {
line[i + 1] = "";
}
writer.writeNext(line);
}
}
Aggregations