267 lines
9.1 KiB
Java
267 lines
9.1 KiB
Java
package org.gcube.portlets.user.statisticalalgorithmsimporter.server.generator;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.StandardOpenOption;
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
import org.gcube.application.framework.core.session.ASLSession;
|
|
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.server.SessionUtil;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.server.storage.FilesStorage;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterServiceException;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Project;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.ProjectTarget;
|
|
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.workspace.ItemDescription;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
*
|
|
* @author Giancarlo Panichi email: <a
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
*
|
|
*/
|
|
public class ProjectBuilder {
|
|
private static final String ALGORITHM_MIMETYPE = "text/plain";
|
|
private static final String ALGORITHM_DESCRIPTION = "Statistical Algorithm Java Code";
|
|
private static final String ALGORITHM_EXTENTION = ".java";
|
|
private static final String INTEGRATION_INFO_NAME = "IntegrationInfo";
|
|
private static final String INTEGRATION_INFO_MIMETYPE = "text/plain";
|
|
private static final String INTEGRATION_INFO_DESCRIPTION = "Integration Info";
|
|
private static final String INTEGRATION_INFO_EXTENTION = ".txt";
|
|
private static final String PROJECT_PACKAGE_MIMETYPE = "application/zip";
|
|
private static final String PROJECT_PACKAGE_DESCRIPTION = "Statical Algorithm Project Package";
|
|
private static final String PROJECT_PACKAGE_EXTENTION = ".zip";
|
|
|
|
public static final Logger logger = LoggerFactory
|
|
.getLogger(ProjectBuilder.class);
|
|
|
|
private Project project;
|
|
private ASLSession aslSession;
|
|
private HttpSession session;
|
|
private ProjectTarget projectTarget;
|
|
|
|
public ProjectBuilder(Project project, ASLSession aslSession,
|
|
HttpSession session) {
|
|
this.project = project;
|
|
this.aslSession = aslSession;
|
|
this.session = session;
|
|
}
|
|
|
|
private void checkInfo() throws StatAlgoImporterServiceException {
|
|
if (project == null) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Attention No Project Found!");
|
|
}
|
|
|
|
if (project.getProjectFolder() == null) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Attention No Project Folder Found!");
|
|
}
|
|
|
|
if (project.getInputData() == null) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Attention No Input Set!");
|
|
}
|
|
|
|
if (project.getInputData().getProjectInfo() == null) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Attention No Project Information Set!");
|
|
}
|
|
|
|
if (project.getInputData().getProjectInfo().getClassName() == null) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Attention No Project Class Name Set!");
|
|
}
|
|
|
|
if (project.getMainCode() == null) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Attention No Main Code Set");
|
|
}
|
|
|
|
if (project.getInputData() == null) {
|
|
throw new StatAlgoImporterServiceException(
|
|
"Attention No Input Data Set!");
|
|
}
|
|
|
|
}
|
|
|
|
public void build() throws StatAlgoImporterServiceException {
|
|
checkInfo();
|
|
createProjectPackage();
|
|
createAlgorithm();
|
|
createIntegrationInfo();
|
|
|
|
SessionUtil.setProjectSession(session, project);
|
|
|
|
}
|
|
|
|
protected void createAlgorithm() throws StatAlgoImporterServiceException {
|
|
AlgorithmGenerator algorithmGenerator = new AlgorithmGenerator(project);
|
|
Path algorithm = algorithmGenerator.createAlgorithm();
|
|
|
|
FilesStorage filesStorage = new FilesStorage();
|
|
WorkspaceItem algorithmItem;
|
|
|
|
try {
|
|
algorithmItem = filesStorage.createItemOnWorkspace(
|
|
aslSession.getUsername(),
|
|
Files.newInputStream(algorithm, StandardOpenOption.READ),
|
|
project.getInputData().getProjectInfo().getClassName()
|
|
+ ALGORITHM_EXTENTION, ALGORITHM_DESCRIPTION,
|
|
ALGORITHM_MIMETYPE, project.getProjectTarget()
|
|
.getTargetFolder().getId());
|
|
} catch (IOException e) {
|
|
logger.error(e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage(),
|
|
e);
|
|
}
|
|
|
|
ItemDescription codeSource;
|
|
try {
|
|
codeSource = new ItemDescription(algorithmItem.getId(),
|
|
algorithmItem.getName(), algorithmItem.getOwner()
|
|
.getPortalLogin(), algorithmItem.getPath(),
|
|
algorithmItem.getType().name());
|
|
codeSource.setPublicLink(algorithmItem.getPublicLink(true));
|
|
} catch (InternalErrorException e) {
|
|
logger.error(e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
projectTarget.setCodeSource(codeSource);
|
|
logger.debug("ProjectTarget: " + projectTarget);
|
|
project.setProjectTarget(projectTarget);
|
|
|
|
}
|
|
|
|
private void createProjectPackage() throws StatAlgoImporterServiceException {
|
|
FilesStorage filesStorage = new FilesStorage();
|
|
filesStorage.deleteTargetFolder(aslSession.getUsername(), project
|
|
.getProjectFolder().getItemDescription().getId());
|
|
|
|
File projectPackageFile = filesStorage.zipFolder(
|
|
aslSession.getUsername(), project.getProjectFolder()
|
|
.getItemDescription().getId());
|
|
|
|
WorkspaceFolder targetFolder = filesStorage.createTargetFolder(
|
|
aslSession.getUsername(), project.getProjectFolder()
|
|
.getItemDescription().getId());
|
|
logger.debug("TargetFolder:" + targetFolder);
|
|
|
|
ItemDescription tFolder;
|
|
try {
|
|
tFolder = new ItemDescription(targetFolder.getId(),
|
|
targetFolder.getName(), targetFolder.getOwner()
|
|
.getPortalLogin(), targetFolder.getPath(),
|
|
targetFolder.getType().name());
|
|
} catch (InternalErrorException e) {
|
|
logger.error(e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
|
|
}
|
|
projectTarget = new ProjectTarget(tFolder);
|
|
|
|
InputStream inputStream;
|
|
try {
|
|
inputStream = Files.newInputStream(projectPackageFile.toPath(),
|
|
StandardOpenOption.READ);
|
|
} catch (IOException e) {
|
|
logger.error("Error input stream generation: "
|
|
+ e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
}
|
|
|
|
WorkspaceItem projectPackageItem;
|
|
try {
|
|
projectPackageItem = filesStorage.createItemOnWorkspace(
|
|
aslSession.getUsername(), inputStream, project
|
|
.getInputData().getProjectInfo().getClassName()
|
|
+ PROJECT_PACKAGE_EXTENTION,
|
|
PROJECT_PACKAGE_DESCRIPTION, PROJECT_PACKAGE_MIMETYPE,
|
|
targetFolder.getId());
|
|
} catch (InternalErrorException e) {
|
|
logger.error(e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
logger.debug("ProjectPackageItem:" + projectPackageItem);
|
|
ItemDescription packageUrl;
|
|
try {
|
|
packageUrl = new ItemDescription(projectPackageItem.getId(),
|
|
projectPackageItem.getName(), projectPackageItem.getOwner()
|
|
.getPortalLogin(), projectPackageItem.getPath(),
|
|
projectPackageItem.getType().name());
|
|
packageUrl.setPublicLink(projectPackageItem.getPublicLink(true));
|
|
} catch (InternalErrorException e) {
|
|
logger.error(e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
projectTarget.setPackageUrl(packageUrl);
|
|
project.setProjectTarget(projectTarget);
|
|
|
|
}
|
|
|
|
private void createIntegrationInfo()
|
|
throws StatAlgoImporterServiceException {
|
|
IntegrationInfoGenerator integrationInfoGenerator = new IntegrationInfoGenerator(
|
|
project);
|
|
Path infoFile = integrationInfoGenerator.createIntegrationInfo();
|
|
|
|
FilesStorage filesStorage = new FilesStorage();
|
|
WorkspaceItem infoItem;
|
|
|
|
try {
|
|
infoItem = filesStorage.createItemOnWorkspace(
|
|
aslSession.getUsername(),
|
|
Files.newInputStream(infoFile, StandardOpenOption.READ),
|
|
INTEGRATION_INFO_NAME
|
|
+ INTEGRATION_INFO_EXTENTION, INTEGRATION_INFO_DESCRIPTION,
|
|
INTEGRATION_INFO_MIMETYPE, project.getProjectTarget()
|
|
.getTargetFolder().getId());
|
|
} catch (IOException e) {
|
|
logger.error(e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage(),
|
|
e);
|
|
}
|
|
|
|
ItemDescription integrationItemDescription;
|
|
try {
|
|
integrationItemDescription = new ItemDescription(infoItem.getId(),
|
|
infoItem.getName(), infoItem.getOwner().getPortalLogin(),
|
|
infoItem.getPath(), infoItem.getType().name());
|
|
integrationItemDescription.setPublicLink(infoItem
|
|
.getPublicLink(true));
|
|
} catch (InternalErrorException e) {
|
|
logger.error(e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new StatAlgoImporterServiceException(e.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
projectTarget.setIntegrationInfo(integrationItemDescription);
|
|
logger.debug("ProjectTarget: " + projectTarget);
|
|
project.setProjectTarget(projectTarget);
|
|
|
|
}
|
|
|
|
}
|