2021-07-23 17:40:50 +02:00
|
|
|
package org.gcube.portlets.user.geoportaldataentry.server;
|
|
|
|
|
|
|
|
import static org.gcube.application.geoportal.client.GeoportalAbstractPlugin.mongoConcessioni;
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.ArrayList;
|
2021-09-14 17:51:20 +02:00
|
|
|
import java.util.Comparator;
|
2021-07-23 17:40:50 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2021-09-14 17:51:20 +02:00
|
|
|
import org.gcube.application.geoportal.common.model.legacy.Concessione;
|
2021-07-23 17:40:50 +02:00
|
|
|
import org.gcube.application.geoportal.common.rest.MongoConcessioni;
|
|
|
|
import org.gcube.application.geoportal.common.rest.TempFile;
|
|
|
|
import org.gcube.application.geoportal.common.utils.StorageUtils;
|
2021-09-14 17:51:20 +02:00
|
|
|
import org.gcube.application.geoportalcommon.shared.products.ConcessioneDV;
|
2021-07-23 17:40:50 +02:00
|
|
|
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
|
|
|
|
import org.gcube.portlets.widgets.mpformbuilder.shared.upload.FileUploaded;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2021-09-16 12:15:07 +02:00
|
|
|
|
2021-07-23 17:40:50 +02:00
|
|
|
/**
|
2021-09-16 12:15:07 +02:00
|
|
|
* The Class MongoServiceUtil.
|
2021-07-23 17:40:50 +02:00
|
|
|
*
|
2021-09-16 12:15:07 +02:00
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
2021-07-23 17:40:50 +02:00
|
|
|
*
|
2021-09-16 12:15:07 +02:00
|
|
|
* Sep 16, 2021
|
2021-07-23 17:40:50 +02:00
|
|
|
*/
|
2021-09-16 12:15:07 +02:00
|
|
|
public class MongoServiceUtil {
|
2021-07-23 17:40:50 +02:00
|
|
|
|
2021-09-16 12:15:07 +02:00
|
|
|
private static Logger LOG = LoggerFactory.getLogger(MongoServiceUtil.class);
|
2021-07-23 17:40:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the instance mongo concessioni.
|
|
|
|
*
|
|
|
|
* @return the instance mongo concessioni
|
|
|
|
*/
|
|
|
|
public MongoConcessioni getInstanceMongoConcessioni() {
|
|
|
|
return mongoConcessioni().build();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the temp file on storage.
|
|
|
|
*
|
|
|
|
* @param is the is
|
|
|
|
* @param fileName the file name
|
|
|
|
* @return the temp file
|
|
|
|
*/
|
|
|
|
public TempFile createTempFileOnStorage(InputStream is, String fileName) {
|
|
|
|
LOG.debug("createTempFileOnStorage called");
|
|
|
|
StorageUtils storage = new StorageUtils();
|
|
|
|
TempFile toUpload = null;
|
|
|
|
try {
|
|
|
|
LOG.info("calling putOntoStorage the stream with the fileName: " + fileName);
|
|
|
|
toUpload = storage.putOntoStorage(is, fileName);
|
|
|
|
} catch (RemoteBackendException | FileNotFoundException e) {
|
|
|
|
LOG.error("Error when uploading stream on Storage: ", e);
|
|
|
|
}
|
|
|
|
return toUpload;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To tem files.
|
|
|
|
*
|
|
|
|
* @param listFileUploaded the list file uploaded
|
|
|
|
* @return the list
|
|
|
|
*/
|
|
|
|
public List<TempFile> toTemFiles(List<FileUploaded> listFileUploaded) {
|
|
|
|
LOG.debug("toTemFiles called");
|
|
|
|
if (listFileUploaded == null || listFileUploaded.isEmpty())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// Building TempFile
|
|
|
|
List<TempFile> files = new ArrayList<TempFile>(listFileUploaded.size());
|
|
|
|
for (FileUploaded fileUploaded : listFileUploaded) {
|
|
|
|
FileInputStream fis;
|
|
|
|
try {
|
|
|
|
fis = new FileInputStream(fileUploaded.getTempSystemPath());
|
|
|
|
// Creating TempFile
|
|
|
|
TempFile storageTempFile = createTempFileOnStorage(fis, fileUploaded.getFileName());
|
|
|
|
files.add(storageTempFile);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
LOG.error("Error on loading temp file with path: " + fileUploaded.getTempSystemPath(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
2021-09-14 17:51:20 +02:00
|
|
|
|
2021-07-23 17:40:50 +02:00
|
|
|
/**
|
|
|
|
* To JSON.
|
|
|
|
*
|
2021-09-14 17:51:20 +02:00
|
|
|
* @param theObj the the obj
|
2021-07-23 17:40:50 +02:00
|
|
|
* @return the string
|
|
|
|
*/
|
|
|
|
public String toJSON(Object theObj) {
|
|
|
|
LOG.debug("toJSON called");
|
|
|
|
try {
|
2021-09-14 17:51:20 +02:00
|
|
|
|
2021-09-20 18:08:43 +02:00
|
|
|
// if (theObj instanceof Serializable) {
|
2021-07-23 17:40:50 +02:00
|
|
|
return org.gcube.application.geoportal.client.utils.Serialization.write(theObj);
|
2021-09-20 18:08:43 +02:00
|
|
|
// }
|
|
|
|
// throw new Exception("The input object is not serializable");
|
2021-09-14 17:51:20 +02:00
|
|
|
|
2021-07-23 17:40:50 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.warn("Error on deserializing: ", e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 17:51:20 +02:00
|
|
|
/**
|
|
|
|
* The Class ConcessioneValidationReportStatusComparator.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
|
|
*
|
|
|
|
* Sep 14, 2021
|
|
|
|
*/
|
|
|
|
public static class ConcessioneValidationReportStatusComparator implements Comparator<Concessione> {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare.
|
|
|
|
*
|
|
|
|
* @param o1 the o 1
|
|
|
|
* @param o2 the o 2
|
|
|
|
* @return the int
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int compare(Concessione o1, Concessione o2) {
|
|
|
|
if (o1 == null || o1.getReport() == null || o1.getReport().getStatus() == null)
|
|
|
|
return 1;
|
|
|
|
if (o2 == null || o2.getReport() == null || o2.getReport().getStatus() == null)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return o1.getReport().getStatus().name().compareTo(o2.getReport().getStatus().name());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-16 12:15:07 +02:00
|
|
|
/**
|
|
|
|
* The Class ConcessioneDVValidationReportStatusComparator.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
|
|
*
|
|
|
|
* Sep 16, 2021
|
|
|
|
*/
|
2021-09-14 17:51:20 +02:00
|
|
|
public static class ConcessioneDVValidationReportStatusComparator implements Comparator<ConcessioneDV> {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare.
|
|
|
|
*
|
|
|
|
* @param o1 the o 1
|
|
|
|
* @param o2 the o 2
|
|
|
|
* @return the int
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int compare(ConcessioneDV o1, ConcessioneDV o2) {
|
2021-09-15 16:14:27 +02:00
|
|
|
if (o1.getValidationStatus() == null)
|
2021-09-14 17:51:20 +02:00
|
|
|
return 1;
|
2021-09-15 16:14:27 +02:00
|
|
|
if (o2.getValidationStatus() == null)
|
2021-09-14 17:51:20 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return o1.getValidationStatus().name().compareTo(o2.getValidationStatus().name());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-23 17:40:50 +02:00
|
|
|
}
|