Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonReader;
Expand All @@ -70,6 +71,7 @@
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

@Stateless
@Path("files")
public class Files extends AbstractApiBean {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public DataFile execute(CommandContext ctxt) throws CommandException {
}

logger.fine("target file: " + localFile);
String newlyDetectedContentType = FileTypeDetection.determineFileType(localFile);
String fileName = fileToRedetect.getLatestFileMetadata().getLabel();
String newlyDetectedContentType = FileTypeDetection.determineFileType(localFile, fileName);
fileToRedetect.setContentType(newlyDetectedContentType);
} catch (IOException ex) {
throw new CommandException("Exception while attempting to get the bytes of the file during file type redetection: " + ex.getLocalizedMessage(), this);
Expand Down
102 changes: 58 additions & 44 deletions src/main/java/edu/harvard/iq/dataverse/util/DatasetFieldWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,101 +15,115 @@
import java.util.logging.Logger;

/**
* A means of iterating over {@link DatasetField}s, or a collection of them.
* As these may have a complex structure (compound values, etc), this object
* allows processing them via an event stream, similar to SAX parsing of XML.
* Visiting of the fields is done in display order.
*
* A means of iterating over {@link DatasetField}s, or a collection of them. As
* these may have a complex structure (compound values, etc), this object allows
* processing them via an event stream, similar to SAX parsing of XML. Visiting
* of the fields is done in display order.
*
* @author michael
*/
public class DatasetFieldWalker {

private static final Logger logger = Logger.getLogger(DatasetFieldWalker.class.getCanonicalName());

public interface Listener {
void startField( DatasetField f );
void endField( DatasetField f );
void primitiveValue( DatasetFieldValue dsfv );
void controledVocabularyValue( ControlledVocabularyValue cvv );
void startCompoundValue( DatasetFieldCompoundValue dsfcv );
void endCompoundValue( DatasetFieldCompoundValue dsfcv );

void startField(DatasetField f);

void endField(DatasetField f);

void primitiveValue(DatasetFieldValue dsfv);

void controledVocabularyValue(ControlledVocabularyValue cvv);

void startCompoundValue(DatasetFieldCompoundValue dsfcv);

void endCompoundValue(DatasetFieldCompoundValue dsfcv);
}

/**
* Convenience method to walk over a field.
*
* @param dsf the field to walk over.
* @param l the listener to execute on {@code dsf}'s values and structure.
*/
public static void walk( DatasetField dsf, Listener l ) {
public static void walk(DatasetField dsf, Listener l) {
DatasetFieldWalker joe = new DatasetFieldWalker(l);
SettingsServiceBean nullServiceBean = null;
joe.walk(dsf, nullServiceBean);
}

/**
* Convenience method to walk over a list of fields. Traversal
* is done in display order.
* Convenience method to walk over a list of fields. Traversal is done in
* display order.
*
* @param fields the fields to go over. Does not have to be sorted.
* @param exclude the fields to skip
* @param l the listener to execute on each field values and structure.
*/
public static void walk(List<DatasetField> fields, SettingsServiceBean settingsService, Listener l) {
DatasetFieldWalker joe = new DatasetFieldWalker(l);
for ( DatasetField dsf : sort( fields, DatasetField.DisplayOrder) ) {
for (DatasetField dsf : sort(fields, DatasetField.DisplayOrder)) {
joe.walk(dsf, settingsService);
}
}

private Listener l;



public DatasetFieldWalker(Listener l) {
this.l = l;
}
public DatasetFieldWalker(){
this( null );

public DatasetFieldWalker() {
this(null);
}

public void walk(DatasetField fld, SettingsServiceBean settingsService) {
l.startField(fld);
DatasetFieldType datasetFieldType = fld.getDatasetFieldType();
boolean excludeEmailFromExport = settingsService != null
&& settingsService.isTrueForKey(SettingsServiceBean.Key.ExcludeEmailFromExport, false);

if ( datasetFieldType.isControlledVocabulary() ) {
for ( ControlledVocabularyValue cvv
: sort(fld.getControlledVocabularyValues(), ControlledVocabularyValue.DisplayOrder) ) {
if (datasetFieldType.isControlledVocabulary()) {
for (ControlledVocabularyValue cvv
: sort(fld.getControlledVocabularyValues(), ControlledVocabularyValue.DisplayOrder)) {
l.controledVocabularyValue(cvv);
}

} else if ( datasetFieldType.isPrimitive() ) {
for ( DatasetFieldValue pv : sort(fld.getDatasetFieldValues(), DatasetFieldValue.DisplayOrder) ) {
if (settingsService != null && settingsService.isTrueForKey(SettingsServiceBean.Key.ExcludeEmailFromExport, false) && DatasetFieldType.FieldType.EMAIL.equals(pv.getDatasetField().getDatasetFieldType().getFieldType())) {

} else if (datasetFieldType.isPrimitive()) {
for (DatasetFieldValue pv : sort(fld.getDatasetFieldValues(), DatasetFieldValue.DisplayOrder)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be fair to remove this and the comment. Any reason to keep it?

//if (settingsService != null && settingsService.isTrueForKey(SettingsServiceBean.Key.ExcludeEmailFromExport, false) && DatasetFieldType.FieldType.EMAIL.equals(pv.getDatasetField().getDatasetFieldType().getFieldType())) {
// Please note that settingsService.isTrueForKey(...) is not free,
// it translates into one named database query on the setting table.
// Not implying that this will solve the EJB rollback issue in #7527
// somehow, but it is insane that we were doing that for every primitive
// field in the dataset metadata. -- L.A.
if (DatasetFieldType.FieldType.EMAIL.equals(pv.getDatasetField().getDatasetFieldType().getFieldType()) && excludeEmailFromExport) {
continue;
}
l.primitiveValue(pv);
}
} else if ( datasetFieldType.isCompound() ) {
for ( DatasetFieldCompoundValue dsfcv : sort( fld.getDatasetFieldCompoundValues(), DatasetFieldCompoundValue.DisplayOrder) ) {
l.startCompoundValue(dsfcv);
for ( DatasetField dsf : sort(dsfcv.getChildDatasetFields(), DatasetField.DisplayOrder ) ) {
walk(dsf, settingsService);
}
l.endCompoundValue(dsfcv);
}

} else if (datasetFieldType.isCompound()) {
for (DatasetFieldCompoundValue dsfcv : sort(fld.getDatasetFieldCompoundValues(), DatasetFieldCompoundValue.DisplayOrder)) {
l.startCompoundValue(dsfcv);
for (DatasetField dsf : sort(dsfcv.getChildDatasetFields(), DatasetField.DisplayOrder)) {
walk(dsf, settingsService);
}
l.endCompoundValue(dsfcv);
}
}
l.endField(fld);
}



public void setL(Listener l) {
this.l = l;
}
static private <T> Iterable<T> sort( List<T> list, Comparator<T> cmp ) {

static private <T> Iterable<T> sort(List<T> list, Comparator<T> cmp) {
ArrayList<T> tbs = new ArrayList<>(list);
Collections.sort(tbs, cmp);
return tbs;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import java.io.IOException;

public class FileTypeDetection {

public static String determineFileType(File file) throws IOException {
return FileUtil.determineFileType(file, file.getName());
// Question: why do we need this utility? - as opposed to just calling the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good question - I vote we just remove, up to you if you want to handle in this PR though. (as it would require changing the places you call it; maybe moving the test? Though my guess is if we don't now, we never will.

// static method in FileUtil directly? - L.A.
public static String determineFileType(File file, String fileName) throws IOException {
return FileUtil.determineFileType(file, fileName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void tearDownClass() {
public void testDetermineFileTypeJupyterNoteboook() throws Exception {
File file = new File("src/test/java/edu/harvard/iq/dataverse/util/irc-metrics.ipynb");
// https://jupyter.readthedocs.io/en/latest/reference/mimetype.html
assertEquals("application/x-ipynb+json", FileTypeDetection.determineFileType(file));
assertEquals("application/x-ipynb+json", FileTypeDetection.determineFileType(file, file.getName()));
}

}