247 lines
7.7 KiB
Java
247 lines
7.7 KiB
Java
package org.gcube.portlets.user.geoportaldataentry.server;
|
|
|
|
import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.projects;
|
|
import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.useCaseDescriptors;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.URL;
|
|
import java.rmi.RemoteException;
|
|
import java.util.ArrayList;
|
|
import java.util.Comparator;
|
|
import java.util.List;
|
|
|
|
import org.bson.Document;
|
|
import org.gcube.application.geoportal.client.utils.Serialization;
|
|
import org.gcube.application.geoportal.common.faults.InvalidRequestException;
|
|
import org.gcube.application.geoportal.common.model.document.Project;
|
|
import org.gcube.application.geoportal.common.model.legacy.Concessione;
|
|
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
|
|
import org.gcube.application.geoportal.common.model.rest.TempFile;
|
|
import org.gcube.application.geoportal.common.rest.MongoConcessioni;
|
|
import org.gcube.application.geoportal.common.rest.Projects;
|
|
import org.gcube.application.geoportal.common.rest.UseCaseDescriptorsI;
|
|
import org.gcube.application.geoportal.common.utils.FileSets;
|
|
import org.gcube.application.geoportal.common.utils.StorageUtils;
|
|
import org.gcube.application.geoportalcommon.shared.products.ConcessioneDV;
|
|
import org.gcube.application.geoportalcommon.shared.products.content.WorkspaceContentDV;
|
|
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;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
/**
|
|
* The Class MongoServiceCommon.
|
|
*
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
*
|
|
* Sep 16, 2021
|
|
*/
|
|
public class MongoServiceUtil {
|
|
|
|
private static Logger LOG = LoggerFactory.getLogger(MongoServiceUtil.class);
|
|
|
|
UseCaseDescriptorsI client = null;
|
|
|
|
public UseCaseDescriptorsI useCaseDescriptorsClient() {
|
|
LOG.debug("useCaseDescriptorsClient called");
|
|
return useCaseDescriptors().build();
|
|
}
|
|
|
|
public Projects<Project> getProjectsClient(String profileID) {
|
|
LOG.debug("getProjectsClient called for profileID: " + profileID);
|
|
return projects(profileID).build();
|
|
}
|
|
|
|
public Project createNew(String profileID, String jsonDocument) throws RemoteException {
|
|
LOG.debug("createNew called for profileID: " + profileID);
|
|
Document myDocument = Document.parse(jsonDocument);
|
|
Projects<Project> client = getProjectsClient(profileID);
|
|
// Create project
|
|
Project project = client.createNew(myDocument);
|
|
|
|
return project;
|
|
}
|
|
|
|
public void registerFileSet(String profileID, Project project, File theFile, String parentPath, String fieldName,
|
|
String fieldDefinition) throws RemoteException, FileNotFoundException, JsonProcessingException, InvalidRequestException {
|
|
LOG.debug("registerFileSet called for profileID: " + profileID);
|
|
|
|
Projects<Project> client = getProjectsClient(profileID);
|
|
// Prepare request
|
|
RegisterFileSetRequest fsRequest = FileSets.prepareRequest(new StorageUtils(), parentPath, fieldName,
|
|
fieldDefinition, theFile);
|
|
project = client.registerFileSet(project.getId(), fsRequest);
|
|
LOG.trace("Resulting Project : " + project);
|
|
LOG.debug("Resulting Project as JSON: " + Serialization.write(project));
|
|
}
|
|
|
|
/**
|
|
* Gets the instance mongo concessioni.
|
|
*
|
|
* @return the instance mongo concessioni
|
|
*/
|
|
public MongoConcessioni getInstanceMongoConcessioni() {
|
|
// return mongoConcessioni().build();
|
|
LOG.warn("\n\n\nMUST BE PORTED TO NEW MONGO CLIENT");
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* To tem files from WSC.
|
|
*
|
|
* @param listFiles the list files
|
|
* @return the list
|
|
*/
|
|
public List<TempFile> toTemFilesFromWSC(List<WorkspaceContentDV> listFiles) {
|
|
LOG.debug("toTemFilesFromWSC called");
|
|
if (listFiles == null || listFiles.isEmpty())
|
|
return null;
|
|
|
|
// Building TempFile
|
|
List<TempFile> files = new ArrayList<TempFile>(listFiles.size());
|
|
for (WorkspaceContentDV fileUploaded : listFiles) {
|
|
InputStream is;
|
|
try {
|
|
is = new URL(fileUploaded.getLink()).openStream();
|
|
// Creating TempFile
|
|
TempFile storageTempFile = createTempFileOnStorage(is, fileUploaded.getName());
|
|
files.add(storageTempFile);
|
|
} catch (IOException e) {
|
|
LOG.error("Error on creating temp file from URL: " + fileUploaded.getLink(), e);
|
|
}
|
|
}
|
|
return files;
|
|
}
|
|
|
|
/**
|
|
* To JSON.
|
|
*
|
|
* @param theObj the the obj
|
|
* @return the string
|
|
*/
|
|
public String toJSON(Object theObj) {
|
|
LOG.debug("toJSON called");
|
|
try {
|
|
|
|
// if (theObj instanceof Serializable) {
|
|
return org.gcube.application.geoportal.client.utils.Serialization.write(theObj);
|
|
// }
|
|
// throw new Exception("The input object is not serializable");
|
|
|
|
} catch (Exception e) {
|
|
LOG.warn("Error on deserializing: ", e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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());
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* The Class ConcessioneDVValidationReportStatusComparator.
|
|
*
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
*
|
|
* Sep 16, 2021
|
|
*/
|
|
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) {
|
|
if (o1.getValidationStatus() == null)
|
|
return 1;
|
|
if (o2.getValidationStatus() == null)
|
|
return -1;
|
|
|
|
return o1.getValidationStatus().name().compareTo(o2.getValidationStatus().name());
|
|
}
|
|
|
|
}
|
|
|
|
}
|