statistical-algorithms-impo.../src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/generator/IntegrationInfoGenerator.java

88 lines
2.6 KiB
Java

package org.gcube.portlets.user.statisticalalgorithmsimporter.server.generator;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterServiceException;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.InterpreterPackageInfo;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Project;
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 IntegrationInfoGenerator {
private static final String EXTENTION_JAVA = ".txt";
public static final Logger logger = LoggerFactory
.getLogger(IntegrationInfoGenerator.class);
private Project project;
public IntegrationInfoGenerator(Project project) {
super();
this.project = project;
}
@Override
public String toString() {
return "IntegrationInfoGenerator [project=" + project + "]";
}
public Path createIntegrationInfo() throws StatAlgoImporterServiceException {
try {
Path tempFile = Files.createTempFile(project.getInputData()
.getProjectInfo().getClassName(), EXTENTION_JAVA);
List<String> lines = createInfo();
Files.write(tempFile, lines, Charset.defaultCharset(),
StandardOpenOption.WRITE);
logger.debug(tempFile.toString());
return tempFile;
} catch (IOException e) {
logger.error(e.getLocalizedMessage());
e.printStackTrace();
throw new StatAlgoImporterServiceException(e.getLocalizedMessage(),
e);
}
}
private List<String> createInfo() {
ArrayList<String> infos = new ArrayList<String>();
if (project.getInputData().getInterpreterInfo() != null) {
if (project.getInputData().getInterpreterInfo().getVersion() != null) {
infos.add("Interpreter Version: "
+ project.getInputData().getInterpreterInfo()
.getVersion());
} else {
infos.add("Interpreter Version: Any");
}
infos.add("");
if (project.getInputData().getInterpreterInfo()
.getInterpreterPackagesInfo() != null
&& project.getInputData().getInterpreterInfo()
.getInterpreterPackagesInfo().size() > 0) {
infos.add("Packages:");
for (InterpreterPackageInfo info : project.getInputData()
.getInterpreterInfo().getInterpreterPackagesInfo()) {
infos.add("" + info.getName() + " " + info.getVersion());
}
}
}
return infos;
}
}