geoportal-data-entry-app/src/main/java/org/gcube/portlets/user/geoportaldataentry/client/ui/edit/UpdateFileset.java

353 lines
11 KiB
Java

package org.gcube.portlets.user.geoportaldataentry.client.ui.edit;
import java.util.List;
import org.gcube.application.geoportalcommon.shared.products.BaseConcessioneDV;
import org.gcube.application.geoportalcommon.shared.products.ConcessioneDV;
import org.gcube.application.geoportalcommon.shared.products.content.WorkspaceContentDV;
import org.gcube.application.geoportalcommon.shared.products.model.AbstractRelazioneScavoDV;
import org.gcube.application.geoportalcommon.shared.products.model.LayerConcessioneDV;
import org.gcube.application.geoportalcommon.shared.products.model.RelazioneScavoDV;
import org.gcube.application.geoportalcommon.shared.products.model.UploadedImageDV;
import org.gcube.portlets.user.geoportaldataentry.client.ConstantsGeoPortalDataEntryApp.RECORD_TYPE;
import org.gcube.portlets.user.geoportaldataentry.client.GeoPortalDataEntryApp;
import org.gcube.portlets.widgets.mpformbuilder.client.form.MetaDataField;
import org.gcube.portlets.widgets.mpformbuilder.client.ui.metadata.MetaDataFieldSkeleton;
import org.gcube.portlets.widgets.mpformbuilder.shared.metadata.DataTypeWrapper;
import org.gcube.portlets.widgets.mpformbuilder.shared.metadata.MetadataFieldWrapper;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.ControlGroup;
import com.github.gwtbootstrap.client.ui.Controls;
import com.github.gwtbootstrap.client.ui.Label;
import com.github.gwtbootstrap.client.ui.ListBox;
import com.github.gwtbootstrap.client.ui.constants.LabelType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
/**
* The Class UpdateFileset.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Sep 27, 2021
*/
public class UpdateFileset extends Composite {
private static final String _FORM_FIELDS_SIZE = "740px";
private static final String UPLOAD_MISSING_FILE = "You must upload a file";
private static UpdateFilesetUiBinder uiBinder = GWT.create(UpdateFilesetUiBinder.class);
/**
* The Interface UpdateFilesetUiBinder.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Sep 27, 2021
*/
interface UpdateFilesetUiBinder extends UiBinder<Widget, UpdateFileset> {
}
@UiField
ListBox listBoxPaths;
@UiField
ControlGroup cgSelectFile;
@UiField
Controls controlsContent;
@UiField
HTMLPanel uploadFileContainer;
@UiField
Button buttonUpdate;
private List<String> listFileSetPaths;
private boolean listBoxPathsPlaceholder = true;
private String recordId;
private BaseConcessioneDV selectedConcessione;
private ConcessioneDV fullConcessione;
private RECORD_TYPE recordType;
// private MultipleDilaogUpload mDU;
private boolean listBoxIndexPlaceholder = true;
private HandlerManager uiBus = new HandlerManager(null);
private MetaDataField fieldUploadWidget;
/**
* Instantiates a new update fileset.
*
* @param listFileSetPaths the list file set paths
*/
public UpdateFileset(BaseConcessioneDV selectedConcessione, RECORD_TYPE recordType, List<String> listFileSetPaths) {
initWidget(uiBinder.createAndBindUi(this));
this.selectedConcessione = selectedConcessione;
this.recordType = recordType;
this.listFileSetPaths = listFileSetPaths;
listBoxPaths.addItem("Select a section...");
for (String path : listFileSetPaths) {
listBoxPaths.addItem(path);
}
listBoxPaths.setWidth(_FORM_FIELDS_SIZE);
// add handler on select
listBoxPaths.addChangeHandler(new ChangeHandler() {
@Override
public void onChange(ChangeEvent event) {
GWT.log("Profile type selection changed...");
cgSelectFile.setVisible(false);
if (listBoxPathsPlaceholder) {
listBoxPaths.removeItem(0); // this is the placeholder, removing it once
listBoxPathsPlaceholder = false;
}
showUploadFileGUI();
}
});
GeoPortalDataEntryApp.geoportalDataEntryService.getRecord(selectedConcessione.getItemId(), recordType,
new AsyncCallback<ConcessioneDV>() {
@Override
public void onSuccess(ConcessioneDV theRecord) {
fullConcessione = theRecord;
}
@Override
public void onFailure(Throwable caught) {
Window.alert(caught.getMessage());
}
});
buttonUpdate.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if (checkValidUpload() == null) {
Window.alert("I can save...");
} else {
Window.alert("New file not found. " + UPLOAD_MISSING_FILE);
}
}
});
}
/**
* Show upload file GUI.
*/
private void showUploadFileGUI() {
uploadFileContainer.setVisible(true);
buttonUpdate.setVisible(false);
uploadFileContainer.clear();
controlsContent.clear();
listBoxIndexPlaceholder = true;
fieldUploadWidget = null;
// listBoxIndex.clear();
cgSelectFile.setVisible(true);
ListBox listBoxIndex = new ListBox();
listBoxIndex.setWidth(_FORM_FIELDS_SIZE);
listBoxIndex.addItem("Select a content...");
if (listBoxPaths.getSelectedItemText().contains("abstract_relazione")) {
AbstractRelazioneScavoDV ar = fullConcessione.getAbstractRelazioneScavo();
if (ar == null) {
showMessage("abstract_relazione" + " NOT AVAILABLE", LabelType.WARNING);
return;
}
int posizIndex = 0;
fillListBoxToBeReplaced(listBoxIndex, "abstract_relazione", posizIndex, ar.getTitolo(),
ar.getListWsContent());
} else if (listBoxPaths.getSelectedItemText().contains("immagini")) {
List<UploadedImageDV> listImmagini = fullConcessione.getImmaginiRappresentative();
if (listImmagini == null || listImmagini.isEmpty()) {
showMessage("immagini" + " NOT AVAILABLE", LabelType.WARNING);
return;
}
int posizIndex = 0;
for (UploadedImageDV uploadedImageDV : listImmagini) {
fillListBoxToBeReplaced(listBoxIndex, "immagini", posizIndex, uploadedImageDV.getTitolo(),
uploadedImageDV.getListWsContent());
posizIndex++;
}
} else if (listBoxPaths.getSelectedItemText().contains("relazione")) {
RelazioneScavoDV realzione = fullConcessione.getRelazioneScavo();
if (realzione == null || realzione.getListWsContent() == null) {
showMessage("relazione" + " NOT AVAILABLE", LabelType.WARNING);
return;
}
int posizIndex = 0;
fillListBoxToBeReplaced(listBoxIndex, "relazione", posizIndex, realzione.getTitolo(),
realzione.getListWsContent());
} else if (listBoxPaths.getSelectedItemText().contains("posizionamentoScavo")) {
LayerConcessioneDV posiz = fullConcessione.getPosizionamentoScavo();
if (posiz == null || posiz.getListWsContent() == null || posiz.getListWsContent().isEmpty()) {
showMessage("posizionamentoScavo" + " NOT AVAILABLE", LabelType.WARNING);
return;
}
int posizIndex = 0;
fillListBoxToBeReplaced(listBoxIndex, "posizionamentoScavo", posizIndex, posiz.getTitolo(),
posiz.getListWsContent());
} else if (listBoxPaths.getSelectedItemText().contains("piante")) {
List<LayerConcessioneDV> piante = fullConcessione.getPianteFineScavo();
if (piante == null || piante.isEmpty()) {
showMessage("piante" + " NOT AVAILABLE", LabelType.WARNING);
return;
}
int posizIndex = 0;
for (LayerConcessioneDV lcDV : piante) {
fillListBoxToBeReplaced(listBoxIndex, "piante", posizIndex, lcDV.getTitolo(), lcDV.getListWsContent());
posizIndex++;
}
}
controlsContent.add(listBoxIndex);
}
private void fillListBoxToBeReplaced(ListBox listBoxIndex, String section, int posizIndex, String title,
List<WorkspaceContentDV> listWSC) {
if (listWSC == null || listWSC.isEmpty()) {
showMessage(section + " DOES NOT CONTAIN FILES", LabelType.WARNING);
return;
}
listBoxIndex.addItem(title, posizIndex + "");
// adding handler once
if (posizIndex == 0) {
listBoxIndex.addChangeHandler(new ChangeHandler() {
@Override
public void onChange(ChangeEvent event) {
if (listBoxIndexPlaceholder) {
listBoxIndex.removeItem(0); // this is the placeholder, removing it once
listBoxIndexPlaceholder = false;
}
showFileBrowseInteraction(listWSC);
}
});
}
//
}
private void showFileBrowseInteraction(List<WorkspaceContentDV> listWSC) {
uploadFileContainer.clear();
if (listWSC.size() > 0) {
FlexTable table = new FlexTable();
table.addStyleName("table-current-content");
table.setHTML(0, 0, "<span style='color:rgb(155, 80, 78); font-weight:bold;'>Current content:</span>");
table.setHTML(1, 0, "<span style='color:rgb(155, 80, 78);'>Filename</span>");
table.setHTML(1, 1, "<span style='color:rgb(155, 80, 78);'>MimeType<span>");
table.setHTML(1, 2, "<span style='color:rgb(155, 80, 78);'>Link</span>");
int i = 2;
for (WorkspaceContentDV wsContent : listWSC) {
table.setHTML(i, 0, wsContent.getName());
table.setHTML(i, 1, wsContent.getMimetype());
String link = "<a href=" + wsContent.getLink() + ">Download</a>";
table.setHTML(i, 2, link);
i++;
}
uploadFileContainer.add(table);
}
HTML label = new HTML();
label.getElement().getStyle().setMarginTop(10, Unit.PX);
label.setHTML("<b>With new content:</b>");
uploadFileContainer.add(label);
// mDU = new MultipleDilaogUpload();
MetadataFieldWrapper uploadField = new MetadataFieldWrapper();
uploadField.setFieldName("File");
uploadField.setMandatory(true);
uploadField.setType(DataTypeWrapper.File);
uploadField.setMaxOccurs(1000);
try {
fieldUploadWidget = new MetaDataField(uploadField, uiBus);
//BrowseFilePanel fieldsetContent = new BrowseFilePanel(fieldUploadWidget);
uploadFileContainer.add(fieldUploadWidget);
buttonUpdate.setVisible(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Test if profile data are valid.
*
* @return the string
*/
private String checkValidUpload() {
for (MetaDataFieldSkeleton field : fieldUploadWidget.getListOfMetadataFields()) {
field.removeError();
String error = field.isFieldValueValid();
if (error != null) {
field.showError();
String errorMsg = field.getFieldNameOriginal() + " is not valid. Suggestion: " + error;
return errorMsg;
}
}
return null;
}
private void showMessage(String txt, LabelType type) {
Label l = new Label();
l.setType(type);
l.setText(txt);
uploadFileContainer.add(l);
}
}