2021-07-23 17:40:50 +02:00
|
|
|
package org.gcube.portlets.user.geoportaldataentry.server;
|
|
|
|
|
2022-03-17 18:08:58 +01:00
|
|
|
import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.projects;
|
|
|
|
import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.useCaseDescriptors;
|
2021-07-23 17:40:50 +02:00
|
|
|
|
2022-03-17 18:08:58 +01:00
|
|
|
import java.io.File;
|
2021-07-23 17:40:50 +02:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.InputStream;
|
2022-03-17 18:08:58 +01:00
|
|
|
import java.rmi.RemoteException;
|
2021-07-23 17:40:50 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-03-17 18:08:58 +01:00
|
|
|
import org.bson.Document;
|
|
|
|
import org.gcube.application.geoportal.client.utils.Serialization;
|
2022-05-20 12:31:58 +02:00
|
|
|
import org.gcube.application.geoportal.common.faults.InvalidRequestException;
|
2022-03-17 18:08:58 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.document.Project;
|
|
|
|
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
|
2022-03-03 18:44:21 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.rest.TempFile;
|
2021-07-23 17:40:50 +02:00
|
|
|
import org.gcube.application.geoportal.common.rest.MongoConcessioni;
|
2022-03-17 18:08:58 +01:00
|
|
|
import org.gcube.application.geoportal.common.rest.Projects;
|
|
|
|
import org.gcube.application.geoportal.common.rest.UseCaseDescriptorsI;
|
|
|
|
import org.gcube.application.geoportal.common.utils.FileSets;
|
2021-07-23 17:40:50 +02:00
|
|
|
import org.gcube.application.geoportal.common.utils.StorageUtils;
|
|
|
|
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;
|
|
|
|
|
2022-03-17 18:08:58 +01:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
2021-07-23 17:40:50 +02:00
|
|
|
/**
|
2021-12-06 17:24:27 +01:00
|
|
|
* The Class MongoServiceCommon.
|
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-10-05 19:17:05 +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
|
|
|
|
2022-03-17 18:08:58 +01:00
|
|
|
UseCaseDescriptorsI client = null;
|
|
|
|
|
|
|
|
public UseCaseDescriptorsI useCaseDescriptorsClient() {
|
2022-05-20 12:31:58 +02:00
|
|
|
LOG.debug("useCaseDescriptorsClient called");
|
2022-03-17 18:08:58 +01:00
|
|
|
return useCaseDescriptors().build();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Projects<Project> getProjectsClient(String profileID) {
|
2022-05-20 12:31:58 +02:00
|
|
|
LOG.debug("getProjectsClient called for profileID: " + profileID);
|
2022-03-17 18:08:58 +01:00
|
|
|
return projects(profileID).build();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Project createNew(String profileID, String jsonDocument) throws RemoteException {
|
2022-05-20 12:31:58 +02:00
|
|
|
LOG.debug("createNew called for profileID: " + profileID);
|
2022-03-17 18:08:58 +01:00
|
|
|
Document myDocument = Document.parse(jsonDocument);
|
|
|
|
Projects<Project> client = getProjectsClient(profileID);
|
|
|
|
// Create project
|
|
|
|
Project project = client.createNew(myDocument);
|
|
|
|
|
|
|
|
return project;
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:44:33 +02:00
|
|
|
public void registerFileSet(String profileID, Project project, String parentPath, String fieldName,
|
2022-08-11 12:07:07 +02:00
|
|
|
String fieldDefinition, File... files)
|
|
|
|
throws RemoteException, FileNotFoundException, JsonProcessingException, InvalidRequestException {
|
2022-05-20 12:31:58 +02:00
|
|
|
LOG.debug("registerFileSet called for profileID: " + profileID);
|
2022-03-17 18:08:58 +01:00
|
|
|
|
|
|
|
Projects<Project> client = getProjectsClient(profileID);
|
|
|
|
// Prepare request
|
|
|
|
RegisterFileSetRequest fsRequest = FileSets.prepareRequest(new StorageUtils(), parentPath, fieldName,
|
2022-08-05 12:44:33 +02:00
|
|
|
fieldDefinition, files);
|
2023-01-25 11:41:20 +01:00
|
|
|
|
2022-03-17 18:08:58 +01:00
|
|
|
project = client.registerFileSet(project.getId(), fsRequest);
|
2022-05-20 12:31:58 +02:00
|
|
|
LOG.trace("Resulting Project : " + project);
|
|
|
|
LOG.debug("Resulting Project as JSON: " + Serialization.write(project));
|
2022-03-17 18:08:58 +01:00
|
|
|
}
|
|
|
|
|
2022-08-11 12:07:07 +02:00
|
|
|
|
2021-07-23 17:40:50 +02:00
|
|
|
/**
|
|
|
|
* Gets the instance mongo concessioni.
|
|
|
|
*
|
|
|
|
* @return the instance mongo concessioni
|
|
|
|
*/
|
|
|
|
public MongoConcessioni getInstanceMongoConcessioni() {
|
2022-03-17 18:08:58 +01:00
|
|
|
// return mongoConcessioni().build();
|
|
|
|
LOG.warn("\n\n\nMUST BE PORTED TO NEW MONGO CLIENT");
|
|
|
|
return null;
|
2021-07-23 17:40:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2022-11-09 15:12:42 +01:00
|
|
|
// /**
|
|
|
|
// * 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;
|
|
|
|
// }
|
2021-10-05 19:17:05 +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-10-05 19:17:05 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|