use of org.jbei.ice.lib.dto.entry.EntryField in project ice by JBEI.
the class BulkCSVUpload method processColumnHeaders.
/**
* @param headerArray list of column header values representing the entry fields
* @return mapping of col number to the entry type
* @throws IOException
*/
HashMap<Integer, HeaderValue> processColumnHeaders(String[] headerArray) throws IOException {
HashMap<Integer, HeaderValue> headers = new HashMap<>();
for (int i = 0; i < headerArray.length; i += 1) {
String fieldStr = headerArray[i].trim();
// account for "*" that indicates a header is required
if (fieldStr.lastIndexOf("*") != -1)
fieldStr = fieldStr.substring(0, fieldStr.length() - 1);
EntryField field = EntryField.fromString(fieldStr);
if (field != null) {
// field header maps as is to EntryField which indicates it is not a sub Type
HeaderValue headerValue = new EntryHeaderValue(false, field);
headers.put(i, headerValue);
continue;
}
// check if sample field
SampleField sampleField = SampleField.fromString(fieldStr);
if (sampleField != null) {
HeaderValue headerValue = new SampleHeaderValue(sampleField);
headers.put(i, headerValue);
continue;
}
// check sub Type
if (subType != null) {
HeaderValue headerValue = detectSubTypeHeaderValue(subType, fieldStr);
headers.put(i, headerValue);
continue;
}
// sub data is null, try to detect
subType = detectSubType(fieldStr);
if (subType == null) {
throw new IOException("Unknown field [" + fieldStr + "] for upload [" + addType.getDisplay() + "]");
}
HeaderValue headerValue = detectSubTypeHeaderValue(subType, fieldStr);
headers.put(i, headerValue);
}
if (headers.size() != headerArray.length) {
throw new IOException("Header size and input header array length differ");
}
return headers;
}
use of org.jbei.ice.lib.dto.entry.EntryField in project ice by JBEI.
the class BulkCSVUpload method detectSubTypeHeaderValue.
HeaderValue detectSubTypeHeaderValue(EntryType subType, String fieldStr) throws IOException {
int k = fieldStr.indexOf(subType.getDisplay());
String headerValue = fieldStr.substring(k + subType.getDisplay().length());
EntryField field = EntryField.fromString(headerValue.trim());
if (field == null) {
throw new IOException("Unknown field [" + fieldStr + "] for upload [" + addType.getDisplay() + "]");
}
return new EntryHeaderValue(true, field);
}
use of org.jbei.ice.lib.dto.entry.EntryField in project ice by JBEI.
the class RemoteEntriesAsCSV method writeList.
private boolean writeList(List<RemotePartner> partners) throws IOException {
Path tmpPath = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY));
File tmpFile = File.createTempFile("remote-ice-", ".csv", tmpPath.toFile());
csvPath = tmpFile.toPath();
FileWriter fileWriter = new FileWriter(tmpFile);
List<EntryField> fields = getEntryFields();
String[] headers = getCSVHeaders(fields);
// csv file headers
File tmpZip = File.createTempFile("zip-", ".zip", tmpPath.toFile());
FileOutputStream fos = new FileOutputStream(tmpZip);
try (CSVWriter writer = new CSVWriter(fileWriter);
ZipOutputStream zos = new ZipOutputStream(fos)) {
writer.writeNext(headers);
// go through partners
for (RemotePartner partner : partners) {
try {
Logger.info("Retrieving from " + partner.getUrl());
PartnerEntries partnerEntries = remoteEntries.getPublicEntries(partner.getId(), 0, Integer.MAX_VALUE, null, true);
Results<PartData> webEntries = partnerEntries.getEntries();
if (webEntries == null || webEntries.getData() == null) {
Logger.error("Could not retrieve entries for " + partner.getUrl());
continue;
}
Logger.info("Obtained " + webEntries.getResultCount() + " from " + partner.getUrl());
// go through entries for each partner and write to the zip file
writeDataEntries(partner, webEntries.getData(), fields, writer, zos);
} catch (Exception e) {
Logger.warn("Exception retrieving entries " + e.getMessage());
}
}
// write local entries
if (this.includeLocal) {
Logger.info("Retrieving local public entries");
Group publicGroup = new GroupController().createOrRetrievePublicGroup();
Set<Group> groups = new HashSet<>();
groups.add(publicGroup);
EntryDAO entryDAO = DAOFactory.getEntryDAO();
List<Entry> results = entryDAO.retrieveVisibleEntries(null, groups, ColumnField.CREATED, true, 0, Integer.MAX_VALUE, null);
writeLocalEntries(results, fields, writer, zos);
}
// write the csv file to the zip
writeZip(tmpZip, zos);
}
return true;
}
use of org.jbei.ice.lib.dto.entry.EntryField in project ice by JBEI.
the class RemoteEntriesAsCSV method writeLocalEntries.
protected void writeLocalEntries(List<Entry> entries, List<EntryField> fields, CSVWriter writer, ZipOutputStream zos) {
if (entries == null)
return;
SequenceDAO sequenceDAO = DAOFactory.getSequenceDAO();
Configuration configuration = DAOFactory.getConfigurationDAO().get(ConfigurationKey.URI_PREFIX);
String thisUrl = configuration == null ? "" : configuration.getValue();
for (Entry entry : entries) {
String[] line = new String[fields.size() + 4];
line[0] = thisUrl;
line[1] = entry.getCreationTime().toString();
line[2] = entry.getPartNumber();
int i = 2;
for (EntryField field : fields) {
line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
i += 1;
}
// write sequence to zip file
long entryId = entry.getId();
if (sequenceDAO.hasSequence(entryId)) {
String name = entry.getPartNumber() + ".gb";
try {
Sequence sequence = sequenceDAO.getByEntry(entry);
line[i + 1] = name;
GenbankFormatter genbankFormatter = new GenbankFormatter(name);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
genbankFormatter.format(sequence, byteStream);
ByteArrayWrapper wrapper = new ByteArrayWrapper(byteStream.toByteArray(), name);
putZipEntry(wrapper, zos);
} catch (Exception e) {
line[i + 1] = "";
}
} else {
line[i + 1] = "";
}
writer.writeNext(line);
}
}
use of org.jbei.ice.lib.dto.entry.EntryField 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