133 lines
4.2 KiB
Java
133 lines
4.2 KiB
Java
package org.gcube.portlets.admin.accountingmanager.server.csv;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.PrintStream;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.StandardOpenOption;
|
|
|
|
import org.apache.commons.csv.CSVFormat;
|
|
import org.apache.commons.csv.CSVPrinter;
|
|
import org.gcube.portlets.admin.accountingmanager.client.state.AccountingStateData;
|
|
import org.gcube.portlets.admin.accountingmanager.server.csv.file.CSVDataRow;
|
|
import org.gcube.portlets.admin.accountingmanager.server.csv.file.CSVModel;
|
|
import org.gcube.portlets.admin.accountingmanager.server.csv.file.CSVModel4Storage;
|
|
import org.gcube.portlets.admin.accountingmanager.server.csv.file.CSVModelBuilder;
|
|
import org.gcube.portlets.admin.accountingmanager.server.csv.file.CSVModelDirector;
|
|
import org.gcube.portlets.admin.accountingmanager.server.storage.StorageUtil;
|
|
import org.gcube.portlets.admin.accountingmanager.shared.exception.ServiceException;
|
|
import org.gcube.portlets.admin.accountingmanager.shared.workspace.ItemDescription;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* CSV Manager
|
|
*
|
|
* @author Giancarlo Panichi email: <a
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
|
*
|
|
*/
|
|
public class CSVManager {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(CSVManager.class);
|
|
private String userName;
|
|
|
|
public CSVManager(String userName){
|
|
this.userName=userName;
|
|
}
|
|
|
|
public ItemDescription saveOnWorkspace(AccountingStateData accountingStateData) throws ServiceException {
|
|
try {
|
|
if (accountingStateData == null) {
|
|
logger.error("AccountingStateData is null");
|
|
throw new ServiceException("AccountingStateData is null!");
|
|
}
|
|
|
|
if (accountingStateData.getAccountingType() == null) {
|
|
logger.error("Accounting Type is null");
|
|
throw new ServiceException("Accounting Type is null!");
|
|
}
|
|
|
|
CSVModelBuilder csvModelBuilder = null;
|
|
|
|
switch (accountingStateData.getAccountingType()) {
|
|
case SERVICE:
|
|
break;
|
|
case STORAGE:
|
|
csvModelBuilder = new CSVModel4Storage(accountingStateData);
|
|
break;
|
|
case JOB:
|
|
case PORTLET:
|
|
case TASK:
|
|
default:
|
|
logger.error("Accounting Type not supported!");
|
|
throw new ServiceException("Accounting Type not supported!!");
|
|
}
|
|
|
|
CSVModelDirector director = new CSVModelDirector();
|
|
director.setCSVModelBuilder(csvModelBuilder);
|
|
director.constructCSVModel();
|
|
CSVModel csvModel = director.getCSVModel();
|
|
logger.debug("CSVModel: " + csvModel);
|
|
|
|
if (csvModel == null) {
|
|
logger.error("CSV model created is null");
|
|
throw new ServiceException("CSVModel created is null!");
|
|
}
|
|
|
|
Path tempFile = Files.createTempFile(csvModel.getFileName(),
|
|
csvModel.getFileExtension());
|
|
logger.debug("Temp File: " + tempFile.toString());
|
|
|
|
// Create the CSVFormat object
|
|
CSVFormat format = CSVFormat.RFC4180.withHeader()
|
|
.withDelimiter(',');
|
|
|
|
PrintStream printStream = new PrintStream(tempFile.toFile());
|
|
// CSV Write Example using CSVPrinter
|
|
CSVPrinter printer = new CSVPrinter(printStream, format);
|
|
printer.printRecord(csvModel.getHeader());
|
|
for (CSVDataRow row : csvModel.getRows()) {
|
|
printer.printRecord(row.getData());
|
|
}
|
|
// close the printer
|
|
printer.close();
|
|
printStream.close();
|
|
|
|
String destinationFolderId = StorageUtil
|
|
.createAccountingFolderOnWorkspace(userName);
|
|
ItemDescription itemDescription=null;
|
|
try (InputStream is = Files.newInputStream(tempFile,
|
|
StandardOpenOption.READ)) {
|
|
itemDescription=StorageUtil.saveOnWorkspace(userName, destinationFolderId,
|
|
csvModel.getFileName() + csvModel.getFileExtension(),
|
|
csvModel.getFileName(), is);
|
|
}
|
|
|
|
try {
|
|
Files.delete(tempFile);
|
|
} catch (IOException e) {
|
|
logger.error("Error in deleting temp file: "
|
|
+ e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new ServiceException("Error deleting temp file: "+e.getLocalizedMessage(),
|
|
e);
|
|
}
|
|
|
|
return itemDescription;
|
|
|
|
} catch (ServiceException e) {
|
|
throw e;
|
|
} catch (Throwable e) {
|
|
logger.error("Error saving data: " + e.getLocalizedMessage());
|
|
e.printStackTrace();
|
|
throw new ServiceException("Error saving data: "
|
|
+ e.getLocalizedMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|