2022-12-07 09:43:48 +01:00
package org.gcube.portlets.user.geoportaldataentry.client.ui.edit ;
2023-04-17 16:03:40 +02:00
import java.util.Arrays ;
import java.util.HashMap ;
import java.util.List ;
import org.gcube.application.geoportalcommon.shared.geoportal.DocumentDV ;
import org.gcube.application.geoportalcommon.shared.geoportal.ResultDocumentDV ;
import org.gcube.application.geoportalcommon.shared.geoportal.config.FilePathDV ;
2022-12-12 17:28:44 +01:00
import org.gcube.application.geoportalcommon.shared.geoportal.config.GcubeProfileDV ;
2022-12-07 09:43:48 +01:00
import org.gcube.application.geoportaldatamapper.shared.MetaDataProfileBeanExt ;
import org.gcube.application.geoportaldatamapper.shared.ProjectEdit ;
2023-04-17 16:03:40 +02:00
import org.gcube.portlets.user.geoportaldataentry.client.ConstantsGeoPortalDataEntryApp.ACTION_PERFORMED_ON_ITEM ;
2022-12-12 17:28:44 +01:00
import org.gcube.portlets.user.geoportaldataentry.client.GeoPortalDataEntryApp ;
2022-12-07 09:43:48 +01:00
import org.gcube.portlets.user.geoportaldataentry.client.GeoportalDataEntryServiceAsync ;
2023-04-17 16:03:40 +02:00
import org.gcube.portlets.user.geoportaldataentry.client.events.OperationPerformedOnItemEvent ;
2022-12-12 17:28:44 +01:00
import org.gcube.portlets.user.geoportaldataentry.client.ui.card.GeoNaFormCardModel ;
2023-04-17 16:03:40 +02:00
import org.gcube.portlets.user.geoportaldataentry.client.ui.report.ReportTemplateToHTML ;
2022-12-07 09:43:48 +01:00
import org.gcube.portlets.user.geoportaldataentry.client.ui.utils.LoaderIcon ;
2023-04-17 16:03:40 +02:00
import org.gcube.portlets.user.geoportaldataentry.shared.CommitReport ;
import org.gcube.portlets.user.geoportaldataentry.shared.GeoNaFormDataObject ;
2022-12-12 17:28:44 +01:00
import org.gcube.portlets.widgets.mpformbuilder.client.form.generic.CreateMetadataForm.OPERATION ;
2023-04-17 16:03:40 +02:00
import org.gcube.portlets.widgets.mpformbuilder.client.form.generic.GenericFormEvents.GenericFormEventsListener ;
import org.gcube.portlets.widgets.mpformbuilder.shared.GenericDatasetBean ;
2022-12-07 09:43:48 +01:00
import com.github.gwtbootstrap.client.ui.Alert ;
2023-01-30 14:47:33 +01:00
import com.github.gwtbootstrap.client.ui.Button ;
2022-12-07 09:43:48 +01:00
import com.github.gwtbootstrap.client.ui.ControlGroup ;
import com.github.gwtbootstrap.client.ui.ListBox ;
2023-04-17 16:03:40 +02:00
import com.github.gwtbootstrap.client.ui.Modal ;
2022-12-07 09:43:48 +01:00
import com.github.gwtbootstrap.client.ui.constants.AlertType ;
import com.google.gwt.core.client.GWT ;
import com.google.gwt.event.dom.client.ChangeEvent ;
import com.google.gwt.event.dom.client.ChangeHandler ;
2023-04-17 16:03:40 +02:00
import com.google.gwt.event.dom.client.ClickEvent ;
import com.google.gwt.event.dom.client.ClickHandler ;
2022-12-07 09:43:48 +01:00
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 ;
2023-04-17 16:03:40 +02:00
import com.google.gwt.user.client.ui.FlowPanel ;
import com.google.gwt.user.client.ui.HTML ;
2022-12-07 09:43:48 +01:00
import com.google.gwt.user.client.ui.HTMLPanel ;
2022-12-12 17:28:44 +01:00
import com.google.gwt.user.client.ui.ScrollPanel ;
2022-12-07 09:43:48 +01:00
import com.google.gwt.user.client.ui.Widget ;
public class UpdateRecord extends Composite {
private static UpdateRecordUiBinder uiBinder = GWT . create ( UpdateRecordUiBinder . class ) ;
interface UpdateRecordUiBinder extends UiBinder < Widget , UpdateRecord > {
}
@UiField
ListBox listBoxSections ;
@UiField
2022-12-13 11:14:07 +01:00
ScrollPanel scrollSectionContent ;
2022-12-12 17:28:44 +01:00
@UiField
2022-12-13 11:14:07 +01:00
HTMLPanel htmlPanelContainer ;
2022-12-07 09:43:48 +01:00
@UiField
HTMLPanel alertHTMLPanel ;
2022-12-12 17:28:44 +01:00
2022-12-07 09:43:48 +01:00
@UiField
ControlGroup controlsControlGroup ;
2023-04-17 16:03:40 +02:00
2023-01-30 14:47:33 +01:00
@UiField
Button buttonUpdate ;
2022-12-07 09:43:48 +01:00
2022-12-13 11:14:07 +01:00
public static final String PLACEHOLDER_LIST_BOX = " Select section... " ;
2023-04-17 16:03:40 +02:00
private LoaderIcon loaderProjectSections = new LoaderIcon ( " Loading Project sections... please wait " ) ;
private GeoNaFormCardModel currentCardSelected ;
private String profileID ;
2022-12-07 09:43:48 +01:00
2023-04-17 16:03:40 +02:00
private String projectID ;
private HashMap < String , List < FilePathDV > > sectionPathFilePaths = new HashMap < > ( ) ;
private MetadataFormCardEventHandler formCardEventHandler = new MetadataFormCardEventHandler ( ) ;
private ProjectEdit projectEditDTO ;
private HandlerManager appManagerBus ;
2023-04-21 16:34:11 +02:00
public UpdateRecord ( HandlerManager appManagerBus , String profileID , String projectID , int modalWidth , int modalHeight ) {
2022-12-07 09:43:48 +01:00
initWidget ( uiBinder . createAndBindUi ( this ) ) ;
2023-04-17 16:03:40 +02:00
this . appManagerBus = appManagerBus ;
this . profileID = profileID ;
this . projectID = projectID ;
setUpdateButtonEnabled ( false ) ;
2022-12-07 09:43:48 +01:00
2022-12-13 11:14:07 +01:00
htmlPanelContainer . setVisible ( false ) ;
2022-12-12 17:28:44 +01:00
2022-12-07 09:43:48 +01:00
alertHTMLPanel . add ( loaderProjectSections ) ;
2023-04-17 16:03:40 +02:00
2023-04-21 15:27:29 +02:00
scrollSectionContent . setHeight ( ( modalHeight - 300 ) + " px " ) ;
2023-04-21 16:34:11 +02:00
listBoxSections . setWidth ( ( modalWidth - 50 ) + " px " ) ;
2023-04-17 16:03:40 +02:00
2022-12-07 09:43:48 +01:00
GeoportalDataEntryServiceAsync . Util . getInstance ( ) . getProjectEdit ( profileID , projectID ,
new AsyncCallback < ProjectEdit > ( ) {
@Override
public void onSuccess ( ProjectEdit result ) {
2023-04-17 16:03:40 +02:00
projectEditDTO = result ;
2022-12-13 11:14:07 +01:00
htmlPanelContainer . setVisible ( true ) ;
2022-12-07 09:43:48 +01:00
try {
alertHTMLPanel . remove ( loaderProjectSections ) ;
} catch ( Exception e ) {
}
2022-12-13 11:14:07 +01:00
listBoxSections . addItem ( PLACEHOLDER_LIST_BOX , PLACEHOLDER_LIST_BOX ) ;
2023-04-17 16:03:40 +02:00
int sectionArray = 0 ;
2022-12-07 09:43:48 +01:00
for ( final MetaDataProfileBeanExt profileBean : result . getTheProfileBeans ( ) ) {
2023-04-17 16:03:40 +02:00
GcubeProfileDV profileDV = profileBean . getGcubeProfileDV ( ) ;
String sectionPath = profileDV . getParentName ( ) ! = null ? profileDV . getParentName ( ) : " " ;
sectionPath + = profileDV . getSectionName ( ) ;
// increment section stored as array
if ( profileDV . getMaxOccurs ( ) = = 0 | | profileDV . getMaxOccurs ( ) > 1 ) {
sectionPath + = " [ " + sectionArray + " ] " ;
sectionArray + + ;
} else {
sectionArray = 0 ;
}
List < FilePathDV > filePaths = profileDV . getFilePaths ( ) ;
if ( filePaths ! = null )
sectionPathFilePaths . put ( sectionPath , filePaths ) ;
GWT . log ( " Adding type: " + profileBean . getType ( ) + " , in the section path: " + sectionPath ) ;
listBoxSections . addItem ( profileBean . getType ( ) , sectionPath ) ;
2022-12-07 09:43:48 +01:00
}
2022-12-12 17:28:44 +01:00
listBoxSections . addChangeHandler ( new ChangeHandler ( ) {
@Override
public void onChange ( ChangeEvent event ) {
2023-04-17 16:03:40 +02:00
setUpdateButtonEnabled ( false ) ;
2022-12-12 17:28:44 +01:00
MetaDataProfileBeanExt selectedBean = result . getTheProfileBeans ( )
2022-12-13 11:14:07 +01:00
. get ( listBoxSections . getSelectedIndex ( ) - 1 ) ; // -1 because the first element is
// the PLACEHOLDER "Select
// section..."
2022-12-12 17:28:44 +01:00
GWT . log ( " Change handler fired " + selectedBean ) ;
controlsControlGroup . setVisible ( true ) ;
2022-12-13 11:14:07 +01:00
scrollSectionContent . clear ( ) ;
2022-12-12 17:28:44 +01:00
GcubeProfileDV gcubeProfile = selectedBean . getGcubeProfileDV ( ) ;
2022-12-13 11:14:07 +01:00
2023-04-17 16:03:40 +02:00
currentCardSelected = GeoPortalDataEntryApp . buildNewFormCardModelFromProfile (
gcubeProfile , 1 , selectedBean , OPERATION . UPDATE , appManagerBus ) ;
currentCardSelected . getMetadataForm ( ) . addListener ( formCardEventHandler ) ;
scrollSectionContent . add ( currentCardSelected . getMetadataForm ( ) ) ;
2022-12-13 11:14:07 +01:00
2022-12-12 17:28:44 +01:00
}
} ) ;
2022-12-07 09:43:48 +01:00
}
@Override
public void onFailure ( Throwable caught ) {
2023-04-17 16:03:40 +02:00
projectEditDTO = null ;
2022-12-13 11:14:07 +01:00
htmlPanelContainer . setVisible ( true ) ;
2022-12-07 09:43:48 +01:00
alertHTMLPanel . remove ( loaderProjectSections ) ;
String errorMsg = caught . getMessage ( ) ;
Alert alert = new Alert ( errorMsg , AlertType . ERROR ) ;
alert . setClose ( false ) ;
try {
alertHTMLPanel . remove ( loaderProjectSections ) ;
} catch ( Exception e ) {
}
alertHTMLPanel . add ( alert ) ;
Window . alert ( errorMsg ) ;
}
} ) ;
2023-04-17 16:03:40 +02:00
bindEvents ( ) ;
2022-12-07 09:43:48 +01:00
}
2023-04-17 16:03:40 +02:00
private void setUpdateButtonEnabled ( boolean bool ) {
buttonUpdate . setEnabled ( bool ) ;
}
private void bindEvents ( ) {
buttonUpdate . addClickHandler ( new ClickHandler ( ) {
@Override
public void onClick ( ClickEvent event ) {
alertHTMLPanel . clear ( ) ;
boolean isFormValid = currentCardSelected . getMetadataForm ( ) . isFormDataValid ( ) ;
if ( ! isFormValid ) {
Alert alert = new Alert ( " Error/s detected, please check your data entry... " , AlertType . WARNING ) ;
alert . setClose ( true ) ;
alertHTMLPanel . add ( alert ) ;
return ;
}
GeoNaFormDataObject gfdo = new GeoNaFormDataObject (
Arrays . asList ( currentCardSelected . getMetadataForm ( ) . getFormDataBean ( ) ) ,
currentCardSelected . getGcubeProfile ( ) ) ;
String sectionPath = listBoxSections . getSelectedValue ( ) ;
GWT . log ( " sectionPath is: " + sectionPath ) ;
List < FilePathDV > listFilePaths = sectionPathFilePaths . get ( sectionPath ) ;
final Modal modal = new Modal ( true , true ) ;
DocumentDV theDocument = projectEditDTO . getTheProjectDV ( ) . getTheDocument ( ) ;
modal . setTitle ( " Updating... " ) ;
final FlowPanel panelContainer = new FlowPanel ( ) ;
LoaderIcon loader = new LoaderIcon ( " Operation in progress... please wait " ) ;
modal . add ( loader ) ;
String htmlMsg = " Updating the section <b> " + listBoxSections . getSelectedItemText ( ) + " </b> of the project with: " ;
htmlMsg + = " <ul> " ;
htmlMsg + = " <li>id: " + projectID + " </li> " ;
htmlMsg + = " <li>profile: " + profileID + " </li> " ;
htmlMsg + = " </ul> " ;
htmlMsg + = " <br> " ;
panelContainer . add ( new HTML ( htmlMsg ) ) ;
panelContainer . add ( loader ) ;
panelContainer . add ( new HTML ( " <br><br> " ) ) ;
modal . add ( panelContainer ) ;
// modal3.setWidth(950);
// modal3.setHeight("700px");
modal . setCloseVisible ( false ) ;
GeoportalDataEntryServiceAsync . Util . getInstance ( ) . updateGeportalDataForm ( profileID , projectID , gfdo ,
sectionPath , listFilePaths , new AsyncCallback < CommitReport > ( ) {
@Override
public void onFailure ( Throwable caught ) {
modal . setCloseVisible ( true ) ;
modal . setTitle ( " Error :-( " ) ;
panelContainer . clear ( ) ;
2023-04-21 16:34:11 +02:00
String errorMsg = " Sorry, an error occurred when updating the project with id: " + projectID + " . Please, try again. If the problem persists, please contact the support " ;
2023-04-17 16:03:40 +02:00
Alert alert = new Alert ( errorMsg , AlertType . ERROR ) ;
alert . setClose ( false ) ;
modal . add ( alert ) ;
}
@Override
public void onSuccess ( CommitReport result ) {
modal . setCloseVisible ( true ) ;
modal . setTitle ( " Project updated! " ) ;
panelContainer . clear ( ) ;
Alert alert = new Alert ( ) ;
alert . setClose ( false ) ;
alert . setType ( AlertType . SUCCESS ) ;
String htmlMsg = " The project with: " ;
htmlMsg + = " <ul> " ;
htmlMsg + = " <li>id: " + projectID + " </li> " ;
htmlMsg + = " <li>profile: " + profileID + " </li> " ;
htmlMsg + = " </ul> " ;
htmlMsg + = " <br> " ;
htmlMsg + = " has been updated correctly! " ;
alert . setHTML ( htmlMsg ) ;
ReportTemplateToHTML rtth2 = new ReportTemplateToHTML ( " Project " , result . getProjectAsJSON ( ) ,
false , false ) ;
rtth2 . showAsJSON ( false ) ;
panelContainer . add ( alert ) ;
panelContainer . add ( rtth2 ) ;
appManagerBus . fireEvent ( new OperationPerformedOnItemEvent < ResultDocumentDV > (
profileID , null , ACTION_PERFORMED_ON_ITEM . UPDATED_PROJECT ) ) ;
}
} ) ;
modal . show ( ) ;
}
} ) ;
}
2023-01-30 14:47:33 +01:00
public void noUpdateMode ( ) {
buttonUpdate . setVisible ( false ) ;
}
2022-12-07 09:43:48 +01:00
2023-04-17 16:03:40 +02:00
/ * *
* The Class MetadataFormCardEventHandler .
*
* @author Francesco Mangiacrapa at ISTI - CNR ( francesco . mangiacrapa @isti.cnr.it )
*
* Oct 12 , 2020
* /
private class MetadataFormCardEventHandler implements GenericFormEventsListener {
/ * *
* On form data valid .
*
* @param genericDatasetBean the generic dataset bean
* /
@Override
public void onFormDataValid ( GenericDatasetBean genericDatasetBean ) {
setUpdateButtonEnabled ( true ) ;
}
/ * *
* On form data edit .
* /
@Override
public void onFormDataEdit ( ) {
setUpdateButtonEnabled ( false ) ;
}
/ * *
* On form aborted .
* /
@Override
public void onFormAborted ( ) {
}
/ * *
* On validation error .
*
* @param throwable the throwable
* @param errorMsg the error msg
* /
@Override
public void onValidationError ( Throwable throwable , String errorMsg ) {
}
}
2022-12-07 09:43:48 +01:00
}