Skip to content

Commit

Permalink
#2290 - UI to test replace functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
raprasad committed Oct 4, 2016
1 parent d1a76d3 commit b7ffd04
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/DataFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.Comparator;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,9 @@ private boolean step_025_loadFileToReplace(DataFile existingFile){
if (existingFile == null){
this.addErrorSevere(getBundleErr("existing_file_to_replace_is_null"));
return false;
}
}

if (existingFile.getOwner() != this.dataset){
if (!existingFile.getOwner().equals(this.dataset)){
addError(getBundleErr("existing_file_to_replace_not_in_dataset"));
return false;
}
Expand Down Expand Up @@ -687,6 +687,7 @@ private boolean step_025_loadFileToReplaceById(Long dataFileId){
}

DataFile existingFile = fileService.find(dataFileId);

if (existingFile == null){
this.addError(getBundleErr("existing_file_to_replace_not_found_by_id") + " " + dataFileId);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,33 @@

import edu.harvard.iq.dataverse.DataFile;
import edu.harvard.iq.dataverse.DataFileServiceBean;
import edu.harvard.iq.dataverse.Dataset;
import edu.harvard.iq.dataverse.DatasetPage;
import edu.harvard.iq.dataverse.DatasetServiceBean;
import edu.harvard.iq.dataverse.DatasetVersionServiceBean;
import edu.harvard.iq.dataverse.DataverseLinkingServiceBean;
import edu.harvard.iq.dataverse.DataverseRequestServiceBean;
import edu.harvard.iq.dataverse.DataverseSession;
import edu.harvard.iq.dataverse.EjbDataverseEngine;
import edu.harvard.iq.dataverse.FileMetadata;
import edu.harvard.iq.dataverse.PermissionServiceBean;
import edu.harvard.iq.dataverse.PermissionsWrapper;
import edu.harvard.iq.dataverse.UserNotificationServiceBean;
import edu.harvard.iq.dataverse.authorization.AuthenticationServiceBean;
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
import edu.harvard.iq.dataverse.ingest.IngestServiceBean;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import edu.harvard.iq.dataverse.util.SystemConfig;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.lang.StringUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

Expand All @@ -46,9 +47,15 @@ public class FileUploadTestPage implements java.io.Serializable {

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

private boolean replaceOperation = false;
private Long datasetId;
private Dataset dataset;
private DataFile fileToReplace;

@EJB
IngestServiceBean ingestService;
@Inject DataverseSession session;
@Inject
DataverseSession session;
@EJB
DatasetServiceBean datasetService;
@EJB
Expand All @@ -72,11 +79,48 @@ public class FileUploadTestPage implements java.io.Serializable {
@EJB
EjbDataverseEngine commandEngine;


public String init() {

Map<String, String> params =FacesContext.getCurrentInstance().
getExternalContext().getRequestParameterMap();


msgt("params: " + params.toString());

if (params.containsKey("ds_id")){
String ds_id = params.get("ds_id");
if ((!ds_id.isEmpty()) && (StringUtils.isNumeric(ds_id))){
dataset = datasetService.find(Long.parseLong(ds_id));
}
}

if (params.containsKey("fid")){
String fid = params.get("fid");
if ((!fid.isEmpty()) && (StringUtils.isNumeric(fid))){
fileToReplace = datafileService.find(Long.parseLong(fid));
}
}

if (fileToReplace != null){
replaceOperation = true;
}else{
replaceOperation = false;
}

return null;
}



public List<FileMetadata> getDatasetFileMetadatas(){

if (dataset == null){
return null;
}
return dataset.getLatestVersion().getFileMetadatasSorted();
}

public String yesYes(){
return "yes yes";
}
Expand All @@ -90,11 +134,29 @@ private void msgt(String s){
msg(s);
msg("-------------------------------");
}

public Dataset getDataset(){
return dataset;
}

public void setDataset(Dataset ds){
dataset = ds;
}

public DataFile getFileToReplace(){
return fileToReplace;
}

public void setFileToReplace(DataFile df){
fileToReplace = df;
}


public void handleFileUpload(FileUploadEvent event) {
msgt("handleFileUpload");


String foo = (String) event.getComponent().getAttributes().get("isReplaceOperation"); // bar
msgt("Foo: " + foo);
//FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
//FacesContext.getCurrentInstance().addMessage(null, message);

Expand All @@ -104,14 +166,15 @@ public void handleFileUpload(FileUploadEvent event) {
msg("getFileName: " + uFile.getFileName());
msg("getContentType: " + uFile.getContentType());

addFile(uFile);
addReplaceFile(uFile);
//msg("file name: " + event.getFileName());
// dFileList = ingestService.createDataFiles(workingVersion, uFile.getInputstream(), uFile.getFileName(), uFile.getContentType());

}


public void addFile(UploadedFile laFile){

public void addReplaceFile(UploadedFile laFile){


//DataverseRequest dvRequest2 = createDataverseRequest(authUser);
Expand All @@ -134,10 +197,18 @@ public void addFile(UploadedFile laFile){
}


addFileHelper.runAddFileByDatasetId(new Long(10),
if (this.replaceOperation){
addFileHelper.runReplaceFile(dataset,
laFile.getFileName(),
laFile.getContentType(),
inputStream,
fileToReplace.getId());
}else{
addFileHelper.runAddFile(dataset,
laFile.getFileName(),
laFile.getContentType(),
inputStream);
}

if (addFileHelper.hasError()){
msgt("upload error");
Expand All @@ -146,4 +217,5 @@ public void addFile(UploadedFile laFile){
msg("Look at that! You added a file! (hey hey, it may have worked)");
}
}
}

} // end class FileUploadTestPage
61 changes: 59 additions & 2 deletions src/main/webapp/file_upload_test.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,74 @@
<h:outputText value="#{FileUploadTestPage.yesYes()}" escape="false" />
</p>

<ui:fragment rendered="#{FileUploadTestPage.fileToReplace == null}">
<h3>Add New File</h3>
</ui:fragment>
<ui:fragment rendered="#{(FileUploadTestPage.fileToReplace != null) and (FileUploadTestPage.fileToReplace.released)}">
<h3>Replace File: <span style="color:#006699;">#{FileUploadTestPage.fileToReplace.displayName }</span></h3>
</ui:fragment>

<h:form>
<p:fileUpload fileUploadListener="#{FileUploadTestPage.handleFileUpload}" mode="advanced" dragDropSupport="false"
update="messages" sizeLimit="100000" fileLimit="2" allowTypes="/(\.|\/)(gif|jpe?g|png|txt)$/" />
sizeLimit="100000" fileLimit="2" allowTypes="/(\.|\/)(gif|jpe?g|png|txt)$/"
update="@all" />

<p:growl id="messages" showDetail="true" />
</h:form>


<hr />

</ui:fragment>


<ui:fragment rendered="#{FileUploadTestPage.fileToReplace != null}">

<!--<h4>file to replace: #{FileUploadTestPage.fileToReplace.displayName }</h4>
<hr />
-->
</ui:fragment>
<!-- start: DATASET + file listing -->
<ui:fragment rendered="#{FileUploadTestPage.dataset == null}">
No dataset chosen.
</ui:fragment>
<ui:fragment rendered="#{FileUploadTestPage.dataset != null}">
<h4>#{FileUploadTestPage.dataset.displayName } v#{FileUploadTestPage.dataset.versionNumber }</h4>

<table class="table table-bordered table-hover">
<tr>
<th>Name</th>
<th>Content type</th>
<th>Hash</th>
<th>Is Released</th>
</tr>
<ui:repeat value="#{FileUploadTestPage.getDatasetFileMetadatas()}" var="fm">
<tr>
<td>
<ui:fragment rendered="#{fm.dataFile.released}">
<a href="file_upload_test.xhtml?ds_id=#{FileUploadTestPage.dataset.id}&amp;fid=#{fm.dataFile.id}">#{fm.label}</a>
</ui:fragment>
<ui:fragment rendered="#{!fm.dataFile.released}">
#{fm.label}
</ui:fragment>
</td>
<td><h:outputText value="#{fm.dataFile.contentType}"/></td>
<td><h:outputText value="#{fm.dataFile.checkSum}"/></td>
<td>
<ui:fragment rendered="#{fm.dataFile.released}">
<span class="label label-success">replacable</span>
</ui:fragment>
<ui:fragment rendered="#{!fm.dataFile.released}">
<span class="label label-warning">nope</span>
</ui:fragment>

</td>
</tr>
</ui:repeat>
</table>
</ui:fragment>
<!-- end DATASET + file listing -->

</div>
</ui:define>
</ui:composition>
Expand Down

0 comments on commit b7ffd04

Please sign in to comment.