2013-02-06 23:25:43 +01:00
package org.gcube.portlets.user.reportgenerator.server.servlet ;
2013-12-17 19:19:51 +01:00
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor ;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor ;
2013-02-06 23:25:43 +01:00
import java.io.BufferedInputStream ;
import java.io.ByteArrayOutputStream ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.ObjectInputStream ;
import java.io.OutputStream ;
import java.util.ArrayList ;
import java.util.Calendar ;
import java.util.Date ;
import java.util.List ;
2013-11-07 19:00:34 +01:00
import java.util.UUID ;
2013-02-06 23:25:43 +01:00
import java.util.Vector ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpSession ;
2013-04-29 17:27:29 +02:00
import javax.xml.bind.DatatypeConverter ;
2013-02-06 23:25:43 +01:00
import net.sf.csv4j.CSVFileProcessor ;
import net.sf.csv4j.CSVLineProcessor ;
import net.sf.csv4j.ParseException ;
import net.sf.csv4j.ProcessingException ;
import org.apache.commons.io.IOUtils ;
import org.gcube.application.framework.accesslogger.library.impl.AccessLogger ;
import org.gcube.application.framework.core.session.ASLSession ;
import org.gcube.application.framework.core.session.SessionManager ;
2014-01-22 18:31:02 +01:00
import org.gcube.application.reporting.ReportsModeler ;
import org.gcube.application.reporting.component.interfaces.ReportComponent ;
import org.gcube.application.reporting.reader.ModelReader ;
import org.gcube.application.reporting.reader.Section ;
2013-12-04 23:21:12 +01:00
import org.gcube.application.rsg.client.RsgClient ;
import org.gcube.application.rsg.service.RsgService ;
2013-11-13 18:56:13 +01:00
import org.gcube.application.rsg.service.dto.ReportEntry ;
import org.gcube.application.rsg.service.dto.ReportType ;
2014-01-28 17:31:13 +01:00
import org.gcube.application.rsg.service.dto.response.Response ;
import org.gcube.application.rsg.service.dto.response.ResponseEntry ;
2013-12-04 23:21:12 +01:00
import org.gcube.application.rsg.support.builder.exceptions.ReportBuilderException ;
import org.gcube.application.rsg.support.builder.impl.ReportManagerReportBuilder ;
import org.gcube.application.rsg.support.model.components.impl.CompiledReport ;
2014-01-24 16:25:54 +01:00
import org.gcube.application.rsg.support.model.utils.CompiledReportUtils ;
2013-02-06 23:25:43 +01:00
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager ;
import org.gcube.applicationsupportlayer.social.NotificationsManager ;
2013-10-09 14:21:50 +02:00
import org.gcube.common.homelibrary.home.HomeLibrary ;
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException ;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException ;
import org.gcube.common.homelibrary.home.workspace.Workspace ;
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder ;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem ;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItemType ;
import org.gcube.common.homelibrary.home.workspace.WorkspaceSharedFolder ;
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemAlreadyExistException ;
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException ;
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException ;
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem ;
import org.gcube.common.homelibrary.home.workspace.folder.FolderItemType ;
import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalImage ;
import org.gcube.common.homelibrary.home.workspace.folder.items.Report ;
import org.gcube.common.homelibrary.home.workspace.folder.items.ReportTemplate ;
import org.gcube.common.homelibrary.home.workspace.folder.items.gcube.ImageDocument ;
import org.gcube.common.homelibrary.home.workspace.folder.items.ts.TimeSeries ;
2013-12-17 19:19:51 +01:00
import org.gcube.common.resources.gcore.ServiceEndpoint ;
import org.gcube.common.scope.api.ScopeProvider ;
2013-03-05 15:26:22 +01:00
import org.gcube.portal.custom.communitymanager.OrganizationsUtil ;
2013-02-06 23:25:43 +01:00
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper ;
import org.gcube.portlets.admin.wfdocslibrary.client.WfDocsLibrary ;
import org.gcube.portlets.admin.wfdocslibrary.server.db.MyDerbyStore ;
import org.gcube.portlets.admin.wfdocslibrary.server.db.Store ;
import org.gcube.portlets.d4sreporting.common.server.ServiceUtil ;
2013-03-01 19:42:50 +01:00
import org.gcube.portlets.d4sreporting.common.shared.BasicComponent ;
import org.gcube.portlets.d4sreporting.common.shared.BasicSection ;
2013-03-05 15:26:22 +01:00
import org.gcube.portlets.d4sreporting.common.shared.ComponentType ;
import org.gcube.portlets.d4sreporting.common.shared.Model ;
2013-03-01 19:42:50 +01:00
import org.gcube.portlets.d4sreporting.common.shared.RepTimeSeries ;
2013-03-08 15:49:26 +01:00
import org.gcube.portlets.d4sreporting.common.shared.RepeatableSequence ;
2013-03-05 15:26:22 +01:00
import org.gcube.portlets.d4sreporting.common.shared.Table ;
2013-02-06 23:25:43 +01:00
import org.gcube.portlets.d4sreporting.common.shared.TableCell ;
import org.gcube.portlets.user.reportgenerator.client.ReportConstants ;
import org.gcube.portlets.user.reportgenerator.client.ReportService ;
import org.gcube.portlets.user.reportgenerator.server.servlet.loggers.CreateReportLogEntry ;
import org.gcube.portlets.user.reportgenerator.server.servlet.loggers.OpenReportLogEntry ;
import org.gcube.portlets.user.reportgenerator.server.servlet.loggers.OpenWorkflowLogEntry ;
import org.gcube.portlets.user.reportgenerator.server.servlet.loggers.SaveWorkflowLogEntry ;
import org.gcube.portlets.user.reportgenerator.shared.SessionInfo ;
2013-03-05 15:26:22 +01:00
import org.gcube.portlets.user.reportgenerator.shared.UserBean ;
2013-11-25 17:12:01 +01:00
import org.gcube.portlets.user.reportgenerator.shared.VMEReportBean ;
2013-11-13 18:56:13 +01:00
import org.gcube.portlets.user.reportgenerator.shared.VMETypeIdentifier ;
2014-01-28 17:31:13 +01:00
import org.gcube.portlets.user.reportgenerator.shared.VmeExportResponse ;
import org.gcube.portlets.user.reportgenerator.shared.VmeResponseEntry ;
2013-10-09 14:21:50 +02:00
import org.gcube.portlets.widgets.exporter.shared.SaveReportFileException ;
import org.gcube.portlets.widgets.exporter.shared.SaveReportFileExistException ;
import org.gcube.portlets.widgets.exporter.shared.TypeExporter ;
2013-12-17 19:19:51 +01:00
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery ;
2014-01-22 18:31:02 +01:00
import org.junit.After ;
2013-10-09 14:21:50 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2013-02-06 23:25:43 +01:00
import com.google.gwt.user.server.rpc.RemoteServiceServlet ;
2013-03-05 15:26:22 +01:00
import com.liferay.portal.model.UserModel ;
2013-02-06 23:25:43 +01:00
import com.liferay.portal.service.LockLocalServiceUtil ;
2013-03-05 15:26:22 +01:00
import com.liferay.portal.service.UserLocalServiceUtil ;
2013-02-06 23:25:43 +01:00
import com.liferay.portlet.documentlibrary.model.DLFileEntry ;
/ * *
*
* class implementing services
* @author Massimiliano Assante , ISTI - CNR - massimiliano . assante @isti.cnr.it
* /
@SuppressWarnings ( " serial " )
public class ReportServiceImpl extends RemoteServiceServlet implements ReportService {
2013-10-09 14:21:50 +02:00
private static final Logger _log = LoggerFactory . getLogger ( ReportServiceImpl . class ) ;
2013-03-05 19:53:57 +01:00
/ * *
* used for debugging in eclipse
* /
private boolean withinPortal = true ;
2013-02-06 23:25:43 +01:00
2013-03-16 02:53:00 +01:00
private static final String TEST_SCOPE = " /gcube/devsec/devVRE " ;
2013-11-13 18:56:13 +01:00
2013-02-06 23:25:43 +01:00
/ * *
*
* /
public static final String CURRENT_REPORT_ID_ATTRIBUTE = " CURRENT_REPORT_ID_ATTRIBUTE " ;
/ * *
*
* /
public static final String CURRENT_REPORT_INSTANCE = " myReport " ;
/ * *
*
* /
public static final String PREVIOUS_REPORT_INSTANCE = " myPreviousReport " ;
2013-12-17 19:19:51 +01:00
/ * *
*
* /
public static final String RSG_WS_ADDRESS = " RSG_WS_ADDRESS " ;
2013-02-06 23:25:43 +01:00
/ * *
* the WF DB Store
* /
private Store store ;
//set to true if wanna test workflow menu mode
boolean testWorkflow = false ;
/ * *
* Called then servlet is intialized
* /
public void init ( ) {
2013-11-18 18:25:49 +01:00
_log . info ( " Initializing Servlet ReportServiceImpl... connecting to WF DB " ) ;
store = new MyDerbyStore ( ) ;
2013-02-06 23:25:43 +01:00
}
2013-03-08 15:49:26 +01:00
2013-02-06 23:25:43 +01:00
/ * *
* the current ASLSession
* @return .
* /
private ASLSession getASLSession ( ) {
String sessionID = this . getThreadLocalRequest ( ) . getSession ( ) . getId ( ) ;
2013-03-16 02:53:00 +01:00
2013-02-06 23:25:43 +01:00
String user = ( String ) this . getThreadLocalRequest ( ) . getSession ( ) . getAttribute ( ScopeHelper . USERNAME_ATTRIBUTE ) ;
if ( user = = null ) {
user = " massimiliano.assante " ;
this . getThreadLocalRequest ( ) . getSession ( ) . setAttribute ( ScopeHelper . USERNAME_ATTRIBUTE , user ) ;
2013-03-15 14:19:27 +01:00
SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) . setScope ( TEST_SCOPE ) ;
2013-03-16 02:53:00 +01:00
String email = user + " @isti.cnr.it " ;
String fullName = " Massimiliano Assante " ;
String thumbnailURL = " images/Avatar_default.png " ;
SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) . setUserEmailAddress ( email ) ;
SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) . setUserAvatarId ( thumbnailURL ) ;
SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) . setUserFullName ( fullName ) ;
2013-02-06 23:25:43 +01:00
}
2013-03-08 15:49:26 +01:00
2013-03-05 15:26:22 +01:00
if ( ! withinPortal )
_log . warn ( " \ n \ n****** Starting in Development MODE ****** \ n \ n " ) ;
2013-03-08 15:49:26 +01:00
2013-02-06 23:25:43 +01:00
return SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) ;
2013-03-08 15:49:26 +01:00
2013-02-06 23:25:43 +01:00
}
/ * *
* Retrieve the user saved template names
*
* @return a String [ ] containing the template names
* /
public String [ ] getUserTemplateNames ( ) {
ServiceUtil myUtil = new ServiceUtil ( getASLSession ( ) ) ;
Vector < String > tmp = new Vector < String > ( ) ;
String userDir = myUtil . getTemplateFolder ( getVreName ( ) , getUsername ( ) ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " userDir: " + userDir ) ;
2013-02-06 23:25:43 +01:00
File f = new File ( userDir ) ;
//checking if dir exists
if ( ! f . exists ( ) ) {
try {
f . mkdirs ( ) ;
return new String [ 0 ] ;
} catch ( SecurityException ex ) {
return new String [ 0 ] ;
}
}
else {
File [ ] f2 = f . listFiles ( ) ;
for ( int i = 0 ; i < f2 . length ; i + + ) {
if ( f2 [ i ] . isDirectory ( ) ) {
tmp . add ( f2 [ i ] . getName ( ) ) ;
}
}
return tmp . toArray ( new String [ 0 ] ) ;
}
}
/ * *
* @return a SerializableModel instance of the imported fimes xml
* /
2013-03-01 19:42:50 +01:00
public Model readImportedModel ( String tempPath ) {
Model toConvert = null ;
2013-02-06 23:25:43 +01:00
FileInputStream fis = null ;
ObjectInputStream in = null ;
try {
fis = new FileInputStream ( tempPath ) ;
in = new ObjectInputStream ( fis ) ;
2013-03-01 19:42:50 +01:00
toConvert = ( Model ) in . readObject ( ) ;
2013-02-06 23:25:43 +01:00
in . close ( ) ;
} catch ( IOException ex ) {
ex . printStackTrace ( ) ;
} catch ( ClassNotFoundException ex ) {
ex . printStackTrace ( ) ;
}
2013-10-29 17:08:14 +01:00
_log . debug ( " Converting Imported Fimes to Serializable object, num sections: " + toConvert . getSections ( ) . size ( ) ) ;
2013-02-06 23:25:43 +01:00
return ( toConvert ) ;
}
/ * *
* @return a SerializableModel instance of the templatename passed as parameter
* @param templateName : the template to read from disk
* @param templateObjectID the id in the basket
* @param isTemplate says if you ' re opening a template or a report
* @param isImporting says if your importing or youre loading a template in the UI
*
* /
2013-03-01 19:42:50 +01:00
public Model readModel ( String templateName , String templateObjectID , boolean isTemplate , boolean isImporting ) {
2013-02-06 23:25:43 +01:00
ServiceUtil myUtil = new ServiceUtil ( getASLSession ( ) ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " Reading " + templateName ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
Workspace root = null ;
WorkspaceItem item = null ;
try {
root = getWorkspaceArea ( ) ;
item = root . getItem ( templateObjectID ) ;
if ( ! isImporting )
storeReportItemIDInSession ( templateObjectID ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
_log . debug ( " ** -> getItem, id: " + templateObjectID + " Name: " + item . getName ( ) ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
String zipToExtract = " " ;
if ( item . getType ( ) = = WorkspaceItemType . FOLDER_ITEM ) {
_log . debug ( " Item is a FolderItem - OK... next step check if is a templet or a report " ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
FolderItem bi = ( FolderItem ) item ;
boolean fromBasket = false ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
if ( isTemplate ) {
if ( bi . getFolderItemType ( ) = = FolderItemType . REPORT_TEMPLATE ) {
ReportTemplate zippedTemplate = ( ReportTemplate ) bi ;
String zipFilename = " " ;
if ( ! isTemplate ) { //then is a report
zipFilename = templateName + " -report.zip " ; //gCube report
_log . debug ( " ********************** Reading template ----------------- " ) ;
}
else
zipFilename = templateName + " .zip " ; //gCube template
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
String zipPath = myUtil . getTemplatePath ( templateName , getVreName ( ) , getUsername ( ) ) ;
fromBasket = getTemplateFromBasket ( zippedTemplate , zipPath , zipFilename ) ;
zipToExtract = zipPath + zipFilename ;
}
2013-02-06 23:25:43 +01:00
}
2013-10-29 17:08:14 +01:00
if ( bi . getFolderItemType ( ) = = FolderItemType . REPORT ) {
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
_log . debug ( " Item is a REPORT " ) ;
Report zippedTemplate = ( Report ) bi ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
String zipFilename = " " ;
if ( ! isTemplate ) { //then is a report
zipFilename = templateName + " -report.zip " ; //gCube report
_log . debug ( " Reading report ----------------- " ) ;
}
else
zipFilename = templateName + " .zip " ; //d4science template
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
File toDelete = new File ( zipFilename ) ;
toDelete . delete ( ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
String zipPath = myUtil . getTemplatePath ( templateName , getVreName ( ) , getUsername ( ) ) ;
fromBasket = getReportFromBasket ( zippedTemplate , zipPath , zipFilename ) ;
zipToExtract = zipPath + zipFilename ;
}
2013-10-29 19:17:47 +01:00
2013-10-29 17:08:14 +01:00
_log . info ( " \ n \ n** zipToExtract: " + zipToExtract ) ;
2013-11-13 18:56:13 +01:00
2013-10-29 17:08:14 +01:00
if ( bi . getFolderItemType ( ) = = FolderItemType . REPORT | | bi . getFolderItemType ( ) = = FolderItemType . REPORT_TEMPLATE ) {
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
if ( fromBasket ) {
File toExtract = new File ( zipToExtract ) ;
File outputDir = new File ( myUtil . getTemplatePath ( templateName , getVreName ( ) , getUsername ( ) ) ) ;
ZipUtil . unzipArchive ( toExtract , outputDir ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
String templatePath = myUtil . getTemplatePath ( templateName , getVreName ( ) , getUsername ( ) ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
String modelFilename = " " ;
try {
modelFilename = seekModel ( templatePath , templateName ) ;
} catch ( FileNotFoundException e ) { e . printStackTrace ( ) ; }
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
String fileToRead = templatePath + modelFilename + " .d4st " ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
_log . debug ( " Loading fileToRead from Disk -> " + fileToRead ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
Model toReturn = null ;
Model toConvert = null ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
FileInputStream fis = null ;
ObjectInputStream in = null ;
try {
fis = new FileInputStream ( fileToRead ) ;
in = new ObjectInputStream ( fis ) ;
toConvert = ( Model ) in . readObject ( ) ;
in . close ( ) ;
} catch ( IOException ex ) {
ex . printStackTrace ( ) ;
} catch ( ClassNotFoundException ex ) {
ex . printStackTrace ( ) ;
}
_log . debug ( " Converting fileToRead to Serializable object " ) ;
toReturn = ( toConvert ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
// changes the template name model
toReturn . setTemplateName ( templateName ) ;
2013-10-29 19:17:47 +01:00
2013-10-29 17:08:14 +01:00
File toDelete = new File ( fileToRead ) ; //to delete the file extracted from the workspace
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
File toDelete1 = new File ( myUtil . getTemplatePath ( templateName , getVreName ( ) , getUsername ( ) ) ) ;
boolean deleted1 = toDelete1 . delete ( ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
File toDelete2 = new File ( zipToExtract ) ;
boolean deleted2 = toDelete2 . delete ( ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
boolean deleted = toDelete . delete ( ) ;
_log . debug ( " deleting extracted file: " + fileToRead + " result: " + deleted ) ;
_log . debug ( " dirToDelete: " + toDelete1 + " result: " + deleted1 ) ;
_log . debug ( " dirToDelete: " + toDelete2 + " result: " + deleted2 ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
//**** IMPORTANT ****
if ( ! isImporting ) {
storeTemplateInSession ( toReturn ) ;
_log . debug ( " storeTemplateInSession DONE " ) ;
}
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
AccessLogger log = AccessLogger . getAccessLogger ( ) ;
OpenReportLogEntry logEntry = new OpenReportLogEntry ( toReturn . getTemplateName ( ) , templateObjectID ) ;
log . logEntry ( getASLSession ( ) . getUsername ( ) , getASLSession ( ) . getScopeName ( ) , logEntry ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 17:08:14 +01:00
return toReturn ;
2013-02-06 23:25:43 +01:00
}
}
2013-10-29 17:08:14 +01:00
_log . error ( " FAILED TO READ RETURING EMPTY Serializable Template " ) ;
2013-03-01 19:42:50 +01:00
return new Model ( ) ;
2013-02-06 23:25:43 +01:00
}
2013-10-29 17:08:14 +01:00
_log . error ( " FAILED TO READ FROM BASKET RETURING EMPTY Serializable Template " ) ;
return new Model ( ) ;
2013-02-06 23:25:43 +01:00
}
/ * *
* handles the case that the user has changed the template name in the basket
* @param templatePath
* @param templateName
* @return
* @throws FileNotFoundException
* /
private String seekModel ( String templatePath , String templateName ) throws FileNotFoundException {
2013-03-05 15:26:22 +01:00
_log . debug ( " seekModel: tPath= " + templatePath ) ;
2013-02-06 23:25:43 +01:00
String fileToSeek = templatePath + templateName + " .d4st " ;
File toSeek = new File ( fileToSeek ) ;
if ( toSeek . exists ( ) ) {
2013-03-05 15:26:22 +01:00
_log . debug ( " seekModel: modelName is the SAME returning " ) ;
2013-02-06 23:25:43 +01:00
return templateName ;
}
else {
2013-03-05 15:26:22 +01:00
_log . debug ( " seekModel: modelName DIFFERENT upgrading " ) ;
2013-02-06 23:25:43 +01:00
File dirToLookIn = new File ( templatePath ) ;
File [ ] innerFiles = dirToLookIn . listFiles ( ) ;
2013-11-13 18:56:13 +01:00
for ( int i = 0 ; i < innerFiles . length ; i + + ) {
_log . debug ( " scanning files in extracted folder: " + innerFiles [ i ] . getName ( ) ) ;
2013-02-06 23:25:43 +01:00
if ( innerFiles [ i ] . getName ( ) . endsWith ( " .d4st " ) ) {
String toReturn = innerFiles [ i ] . getName ( ) ;
toReturn = toReturn . substring ( 0 , toReturn . length ( ) - 5 ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " seekModel: returning.. = " + toReturn ) ;
2013-02-06 23:25:43 +01:00
return toReturn ;
}
2013-11-13 18:56:13 +01:00
}
2013-02-06 23:25:43 +01:00
}
throw new FileNotFoundException ( ) ;
}
/ * *
* get the template instance from the Basket
* @param repTmp
* @return
* /
private boolean getTemplateFromBasket ( ReportTemplate repTmp , String pathToFile , String filename ) {
try {
File dir = new File ( pathToFile ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " DIR: " + pathToFile ) ;
2013-02-06 23:25:43 +01:00
if ( ! dir . exists ( ) )
dir . mkdirs ( ) ;
File f = new File ( pathToFile + filename ) ;
InputStream inputStream = null ;
try {
inputStream = repTmp . getData ( ) ;
} catch ( InternalErrorException e ) {
e . printStackTrace ( ) ;
return false ;
}
OutputStream out = new FileOutputStream ( f ) ;
byte buf [ ] = new byte [ 1024 ] ;
int len ;
while ( ( len = inputStream . read ( buf ) ) > 0 )
out . write ( buf , 0 , len ) ;
out . close ( ) ;
inputStream . close ( ) ;
2013-03-05 15:26:22 +01:00
_log . info ( " Successfully got ReportTemplate from Basket: " + pathToFile ) ;
2013-02-06 23:25:43 +01:00
return true ;
}
catch ( IOException e ) {
e . printStackTrace ( ) ;
return false ;
}
}
/ * *
* get the report instance from the Basket
* @param repTmp .
* @param pathToFile the directory where to save the file
* @param filename the filename to give to the newly created file
* @return
* /
private boolean getReportFromBasket ( Report repTmp , String pathToFile , String filename ) {
try {
File dir = new File ( pathToFile ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " DIR: " + pathToFile ) ;
2013-02-06 23:25:43 +01:00
if ( ! dir . exists ( ) )
dir . mkdirs ( ) ;
File f = new File ( pathToFile + filename ) ;
InputStream inputStream = null ;
try {
inputStream = repTmp . getData ( ) ;
} catch ( InternalErrorException e ) {
e . printStackTrace ( ) ;
return false ;
}
OutputStream out = new FileOutputStream ( f ) ;
byte buf [ ] = new byte [ 1024 ] ;
int len ;
while ( ( len = inputStream . read ( buf ) ) > 0 )
out . write ( buf , 0 , len ) ;
out . close ( ) ;
inputStream . close ( ) ;
2013-03-13 17:15:29 +01:00
_log . info ( " Successfully got ReportTemplate from HL: " + pathToFile ) ;
2013-02-06 23:25:43 +01:00
return true ;
}
catch ( IOException e ) {
e . printStackTrace ( ) ;
return false ;
}
}
2013-03-16 02:53:00 +01:00
2013-02-06 23:25:43 +01:00
/ * *
*
* @return the shared session
* /
public String getUsername ( ) {
if ( ! ReportConstants . isDeployed ) {
return " massimiliano.assante " ;
} else {
HttpServletRequest httpServletRequest = this . getThreadLocalRequest ( ) ;
HttpSession session = httpServletRequest . getSession ( ) ;
String user = ( String ) session . getAttribute ( ScopeHelper . USERNAME_ATTRIBUTE ) ;
if ( session . getAttribute ( ScopeHelper . USERNAME_ATTRIBUTE ) = = null )
{
user = " massimiliano.assante " ;
2013-03-05 15:26:22 +01:00
_log . warn ( " D4ScienceSession user NULL set to: " + user ) ;
2013-02-06 23:25:43 +01:00
}
2013-03-05 15:26:22 +01:00
_log . warn ( " ASLSession user: " + user ) ;
2013-02-06 23:25:43 +01:00
ASLSession d4session = SessionManager . getInstance ( ) . getASLSession ( session . getId ( ) , user ) ;
d4session . setAttribute ( ScopeHelper . USERNAME_ATTRIBUTE , user ) ;
return user ;
}
}
/ * *
*
* @return the current scope
* /
public String getVreName ( ) {
if ( ! ReportConstants . isDeployed ) {
2013-03-15 14:19:27 +01:00
return TEST_SCOPE ;
2013-02-06 23:25:43 +01:00
} else {
HttpServletRequest httpServletRequest = this . getThreadLocalRequest ( ) ;
HttpSession session = httpServletRequest . getSession ( ) ;
ASLSession d4session = SessionManager . getInstance ( ) . getASLSession ( session . getId ( ) , getUsername ( ) ) ;
String scope = d4session . getScopeName ( ) ;
if ( scope = = null ) {
2013-03-15 14:19:27 +01:00
scope = TEST_SCOPE ;
2013-03-05 15:26:22 +01:00
_log . warn ( " ASL Session scope NULL set to: " + scope ) ;
2013-02-06 23:25:43 +01:00
}
//need to remove the initial / of the scope
if ( scope . charAt ( 0 ) = = '/' )
scope = scope . substring ( 1 , scope . length ( ) ) ;
2013-03-05 15:26:22 +01:00
_log . info ( " SCOPE: " + scope ) ;
2013-02-06 23:25:43 +01:00
return scope ;
}
}
2014-01-23 19:11:55 +01:00
2013-12-17 19:19:51 +01:00
private String getRSGWSAddress ( ) {
String addr = ( String ) getASLSession ( ) . getAttribute ( RSG_WS_ADDRESS ) ;
_log . trace ( " getRSGWSAddress: " + addr ) ;
return addr ;
}
private void setRSGWSAddress ( String addr ) {
getASLSession ( ) . setAttribute ( RSG_WS_ADDRESS , addr ) ;
_log . trace ( " setting RSG WS address to " + addr ) ;
}
2013-02-06 23:25:43 +01:00
/ * *
*
2013-03-16 02:53:00 +01:00
* @return
* @throws WorkspaceFolderNotFoundException
2013-02-06 23:25:43 +01:00
* @throws InternalErrorException
* @throws HomeNotFoundException
* /
protected Workspace getWorkspaceArea ( ) throws WorkspaceFolderNotFoundException , InternalErrorException , HomeNotFoundException {
2013-10-09 14:21:50 +02:00
return HomeLibrary . getUserWorkspace ( getASLSession ( ) . getUsername ( ) ) ;
2013-02-06 23:25:43 +01:00
}
/ * *
* reads from the file system and returns the user workspace as TreeNode object
*
2013-03-16 02:53:00 +01:00
* @return the Default folder if if there is no basket in session , else the folder in session id
2013-02-06 23:25:43 +01:00
* /
2013-03-16 02:53:00 +01:00
public String getRootFolder ( ) {
2013-02-06 23:25:43 +01:00
try {
Workspace workspaceArea = getWorkspaceArea ( ) ;
WorkspaceFolder basket = workspaceArea . getRoot ( ) ;
return basket . getId ( ) ;
2013-03-16 02:53:00 +01:00
} catch ( Exception e ) { e . printStackTrace ( ) ;
2013-02-06 23:25:43 +01:00
}
2013-03-16 02:53:00 +01:00
return " Coud not open default folder " ;
2013-02-06 23:25:43 +01:00
}
/ * *
*
* @return the model previously stored in the session
* /
2013-03-01 19:42:50 +01:00
public Model readTemplateFromSession ( ) {
2013-02-06 23:25:43 +01:00
ASLSession d4Session = getASLSession ( ) ;
String templateid = ( String ) d4Session . getAttribute ( " idreport " ) ;
Object workflowid = getASLSession ( ) . getAttribute ( WfDocsLibrary . LAST_WORKFLOW_ID ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " (templateid != null && workflowid != null) = " + ( templateid ! = null ) + " - " + ( workflowid ! = null ) ) ;
2013-02-06 23:25:43 +01:00
if ( workflowid ! = null ) {
getASLSession ( ) . setAttribute ( WfDocsLibrary . LAST_WORKFLOW_ID , null ) ;
return null ;
}
String templateName = " " ;
2013-03-05 15:26:22 +01:00
_log . debug ( " TEMPLATE ID==NULL " + ( templateid = = null ) ) ;
2013-02-06 23:25:43 +01:00
if ( templateid ! = null ) {
if ( ! templateid . equals ( " " ) ) {
2013-03-05 15:26:22 +01:00
_log . debug ( " READING SESSION VARIABLE FOR REPORT ID... " + templateid ) ;
2013-02-06 23:25:43 +01:00
//reset the value
d4Session . setAttribute ( " idreport " , " " ) ;
Workspace root = null ;
WorkspaceItem item = null ;
try {
root = getWorkspaceArea ( ) ;
item = root . getItem ( templateid ) ;
2013-03-05 15:26:22 +01:00
_log . info ( " READ REPORT FROM WP... " + item . getName ( ) ) ;
2013-02-06 23:25:43 +01:00
templateName = item . getName ( ) ;
} catch ( WorkspaceFolderNotFoundException e ) { e . printStackTrace ( ) ;
} catch ( InternalErrorException e ) { e . printStackTrace ( ) ;
} catch ( HomeNotFoundException e ) { e . printStackTrace ( ) ;
} catch ( ItemNotFoundException e ) { e . printStackTrace ( ) ; }
2013-03-01 19:42:50 +01:00
Model toReturn = readModel ( templateName , templateid , false , false ) ;
2013-02-06 23:25:43 +01:00
return toReturn ;
}
if ( d4Session . getAttribute ( CURRENT_REPORT_INSTANCE ) ! = null )
2013-03-01 19:42:50 +01:00
return ( Model ) d4Session . getAttribute ( CURRENT_REPORT_INSTANCE ) ;
2013-02-06 23:25:43 +01:00
return null ;
}
else {
if ( d4Session . getAttribute ( CURRENT_REPORT_INSTANCE ) ! = null ) {
2013-03-05 15:26:22 +01:00
_log . debug ( " getAttribute( \" CURRENT_REPORT_INSTANCE \" )... " ) ;
2013-03-01 19:42:50 +01:00
Model model = ( Model ) d4Session . getAttribute ( CURRENT_REPORT_INSTANCE ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( model . getTemplateName ( ) ) ;
2013-02-06 23:25:43 +01:00
return model ;
}
}
ServiceUtil myUtil = new ServiceUtil ( getASLSession ( ) ) ;
String dirToClean = myUtil . getTemplateFolder ( getVreName ( ) , getUsername ( ) ) ;
2013-03-16 02:53:00 +01:00
_log . info ( " No data on session for Reports, cleaning temp dir: " + dirToClean ) ;
delTemplateDir ( new File ( dirToClean ) ) ;
2013-02-06 23:25:43 +01:00
return null ;
}
/ * *
* recurdively delete the templates folder of the dir dir
* @param dir the dir to delete
* /
public void delTemplateDir ( File dir ) {
try {
File [ ] files = dir . listFiles ( ) ;
for ( int i = 0 ; i < files . length ; i + + ) {
if ( files [ i ] . isDirectory ( ) )
delTemplateDir ( files [ i ] ) ;
files [ i ] . delete ( ) ;
}
} catch ( Exception e ) {
System . out . println ( " WARNING: Could not cleaning temp dir: reason unknown " ) ;
}
}
/ * *
* @param model to store in the session
* /
2013-03-01 19:42:50 +01:00
public void storeTemplateInSession ( Model model ) {
2013-02-06 23:25:43 +01:00
ASLSession d4Session = getASLSession ( ) ;
d4Session . setAttribute ( CURRENT_REPORT_INSTANCE , model ) ;
2013-05-06 17:38:24 +02:00
_log . trace ( " Saved in Session " ) ;
2013-02-06 23:25:43 +01:00
}
2013-04-29 17:27:29 +02:00
private void convertDynamicImagesFromHL ( Model model ) {
2013-03-05 15:26:22 +01:00
_log . debug ( " model == NULL " + ( model = = null ) ) ;
2013-03-01 19:42:50 +01:00
Vector < BasicSection > sections = model . getSections ( ) ;
for ( BasicSection section : sections ) {
for ( BasicComponent component : section . getComponents ( ) ) {
2013-02-06 23:25:43 +01:00
if ( component . getType ( ) = = ComponentType . DYNA_IMAGE ) {
2013-03-13 18:44:20 +01:00
_log . debug ( " Found Simple Image: " + component . getPossibleContent ( ) ) ;
2013-11-25 17:12:01 +01:00
if ( component . getId ( ) ! = null ) { // you need to convert only new images that stay in the HL Workspace, this is the check
String imageID = component . getId ( ) ;
2013-04-29 17:27:29 +02:00
component . setPossibleContent ( getdDataImagebase64 ( imageID ) ) ;
_log . trace ( " Image converted base 64 OK: " + component . getPossibleContent ( ) ) ;
2013-02-06 23:25:43 +01:00
}
}
2013-03-13 17:03:57 +01:00
if ( component . getType ( ) = = ComponentType . REPEAT_SEQUENCE | | component . getType ( ) = = ComponentType . BODY_TABLE_IMAGE ) { //there could be images inside
2013-03-08 15:49:26 +01:00
RepeatableSequence res = ( RepeatableSequence ) component . getPossibleContent ( ) ;
for ( BasicComponent co : res . getGroupedComponents ( ) ) {
2013-12-06 18:23:49 +01:00
if ( component . getType ( ) = = ComponentType . DYNA_IMAGE ) {
_log . debug ( " Found Image IN SEQUENCE, type is: " + component . getType ( ) ) ;
if ( co . getId ( ) ! = null ) { // you need to convert only new images that stay in the HL Workspace, this is the check
String imageID = co . getId ( ) ;
co . setPossibleContent ( getdDataImagebase64 ( imageID ) ) ;
_log . trace ( " Image converted base 64 OK, in SEQUENCE: " + co . getPossibleContent ( ) ) ;
}
2013-03-08 15:49:26 +01:00
}
}
}
2013-02-06 23:25:43 +01:00
}
}
}
2013-04-29 17:27:29 +02:00
/ * *
* convert the image into a String encoded base 64
* @param imageIDinWorkspace the id of the image in workspace
* @return the string representing the image converted to be put in the img src attribute e . g . data : image / ong ; base64 , a . . . z
* /
private String getdDataImagebase64 ( String imageIDinWorkspace ) {
Workspace root = null ;
try {
root = getWorkspaceArea ( ) ;
WorkspaceItem item = null ;
item = root . getItem ( imageIDinWorkspace ) ;
if ( item . getType ( ) = = WorkspaceItemType . FOLDER_ITEM ) {
_log . debug ( " Item is a Folder Item " ) ;
FolderItem imageItem = ( FolderItem ) item ;
InputStream data = null ;
if ( imageItem . getFolderItemType ( ) = = FolderItemType . EXTERNAL_IMAGE ) {
_log . debug ( " EXTERNAL_IMAGE -|- " + item . getType ( ) + " itemId= " + item . getId ( ) ) ;
ExternalImage image = ( ExternalImage ) item ;
_log . debug ( " EXTERNAL_IMAGE Name= " + item . getName ( ) + " Asking InputStream .. " ) ;
data = image . getData ( ) ;
_log . debug ( " Got inputStream " ) ;
}
else if ( imageItem . getFolderItemType ( ) = = FolderItemType . IMAGE_DOCUMENT ) {
ImageDocument image = ( ImageDocument ) item ;
if ( image . getMimeType ( ) . equals ( " image/tiff " ) )
data = image . getThumbnail ( ) ;
else
data = image . getData ( ) ;
}
if ( data ! = null ) {
_log . debug ( " Encoding image in base64 " ) ;
2013-10-29 17:08:14 +01:00
byte [ ] imageBytes = IOUtils . toByteArray ( data ) ;
2013-11-07 12:48:50 +01:00
String extension = ImagesUtil . getImageExtension ( imageItem ) ;
2013-04-29 17:27:29 +02:00
String srcAttrValue = " data:image/ " + extension + " ;base64, " + DatatypeConverter . printBase64Binary ( imageBytes ) ;
_log . debug ( " Encoded image= " + srcAttrValue ) ;
2013-10-29 17:08:14 +01:00
return srcAttrValue ;
2013-04-29 17:27:29 +02:00
}
return null ;
}
else
return null ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return null ;
}
}
2013-10-29 17:08:14 +01:00
2013-02-06 23:25:43 +01:00
/ * *
* @param reportItemid the report itemd id in basket to store in the session
* /
public void storeReportItemIDInSession ( String reportItemid ) {
ASLSession d4Session = getASLSession ( ) ;
d4Session . setAttribute ( CURRENT_REPORT_ID_ATTRIBUTE , reportItemid ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " WROTE REPORT ID IN SESSION: " + reportItemid ) ;
2013-02-06 23:25:43 +01:00
}
/ * *
* @return the report item id in basket , or " " if doesn ' t exist
* /
public String getReportItemIDFromSession ( ) {
ASLSession d4Session = getASLSession ( ) ;
if ( d4Session . getAttribute ( CURRENT_REPORT_ID_ATTRIBUTE ) = = null )
return " " ;
else
return d4Session . getAttribute ( CURRENT_REPORT_ID_ATTRIBUTE ) . toString ( ) ;
}
/ * *
2013-10-29 19:17:47 +01:00
* used to save the report in the same folder
2013-02-06 23:25:43 +01:00
* /
2013-05-06 17:38:24 +02:00
public void saveReport ( Model toSave ) {
2013-02-06 23:25:43 +01:00
Workspace root = null ;
try {
root = getWorkspaceArea ( ) ;
2013-03-16 02:53:00 +01:00
} catch ( Exception e ) { e . printStackTrace ( ) ; }
2013-02-06 23:25:43 +01:00
WorkspaceItem item = null ;
String folderid = " " ;
String itemName = " " ;
try {
if ( getReportItemIDFromSession ( ) . equals ( " " ) ) {
2013-03-16 02:53:00 +01:00
folderid = getRootFolder ( ) ;
2013-02-06 23:25:43 +01:00
}
else {
item = root . getItem ( getReportItemIDFromSession ( ) ) ;
folderid = item . getParent ( ) . getId ( ) ;
itemName = item . getName ( ) ;
}
} catch ( ItemNotFoundException e ) {
2013-03-05 15:26:22 +01:00
_log . error ( " ITEM NOT FOUND -> " + getReportItemIDFromSession ( ) ) ;
2013-02-06 23:25:43 +01:00
} catch ( InternalErrorException e ) {
e . printStackTrace ( ) ;
}
2013-05-06 17:38:24 +02:00
saveReport ( toSave , folderid , itemName ) ;
2013-02-06 23:25:43 +01:00
}
2014-01-23 19:11:55 +01:00
2013-02-06 23:25:43 +01:00
/ * *
2013-02-15 16:54:13 +01:00
* @param save a report in another folder .
2013-02-06 23:25:43 +01:00
*
* /
2013-05-06 17:38:24 +02:00
public void saveReport ( Model toSave , String folderid , String newname ) {
2014-01-23 19:11:55 +01:00
2013-05-06 17:38:24 +02:00
Model model = toSave ;
2014-01-23 19:11:55 +01:00
2014-01-22 18:31:02 +01:00
ModelReader reader = new ModelReader ( model ) ;
System . out . println ( reader ) ;
2014-01-23 19:11:55 +01:00
2013-05-06 17:38:24 +02:00
storeTemplateInSession ( toSave ) ;
2013-03-13 18:44:20 +01:00
_log . info ( " Serializing Model in folder: " + folderid ) ;
2013-03-05 15:26:22 +01:00
_log . info ( " Trying to convert dynamic images ... " ) ;
2013-04-29 17:27:29 +02:00
convertDynamicImagesFromHL ( model ) ;
2013-02-06 23:25:43 +01:00
ServiceUtil myUtil = new ServiceUtil ( getASLSession ( ) ) ;
boolean result = myUtil . writeModel ( model , " CURRENT_OPEN " , getVreName ( ) , getUsername ( ) ) ;
if ( ! result ) {
2013-03-05 15:26:22 +01:00
_log . debug ( " Could not save report, serializing failed " ) ;
2013-02-06 23:25:43 +01:00
}
else {
String templatePath = myUtil . getTemplateFolder ( getVreName ( ) , getUsername ( ) ) + " CURRENT_OPEN " ;
try {
Thread . sleep ( 1000 ) ;
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
}
2013-03-05 15:26:22 +01:00
_log . debug ( " Trying to zip folder: " + templatePath ) ;
2013-02-06 23:25:43 +01:00
String folderToZip = templatePath ;
String outZip = templatePath + " -report.zip " ;
try {
ZipUtil . zipDir ( outZip , folderToZip ) ;
} catch ( IOException e ) {
2013-03-05 15:26:22 +01:00
_log . error ( " Could not zip template, serializing failed " ) ;
2013-02-06 23:25:43 +01:00
e . printStackTrace ( ) ;
}
2013-03-05 15:26:22 +01:00
_log . info ( " Folder zipped, result: " + outZip ) ;
2013-02-06 23:25:43 +01:00
InputStream isZip = null ;
WorkspaceFolder toSaveIn = null ;
try {
isZip = new BufferedInputStream ( new FileInputStream ( outZip ) ) ;
2013-03-16 02:53:00 +01:00
toSaveIn = getRootFolder ( folderid ) ;
2013-02-06 23:25:43 +01:00
String templateName = newname ;
2013-10-29 19:17:47 +01:00
boolean isUpdate = false ;
2013-02-06 23:25:43 +01:00
if ( templateName . endsWith ( " d4sR " ) ) {
if ( toSaveIn . exists ( templateName ) ) {
2013-10-29 19:17:47 +01:00
_log . warn ( " Item exists already, updating " ) ;
//toSaveIn.removeChild(toSaveIn.find(templateName));
isUpdate = true ;
2013-02-06 23:25:43 +01:00
}
}
if ( toSaveIn . exists ( templateName + " .d4sR " ) ) {
2013-10-29 19:17:47 +01:00
_log . warn ( " Item exists already, updating " ) ;
//toSaveIn.removeChild(toSaveIn.find(templateName + ".d4sR"));
isUpdate = true ;
2013-02-06 23:25:43 +01:00
}
//remove the template extension
String templateToInsert = templateName . replace ( " .d4sT " , " " ) ;
if ( ! templateToInsert . endsWith ( " .d4sR " ) )
templateToInsert + = " .d4sR " ;
2013-10-29 19:17:47 +01:00
Report rep = null ;
if ( isUpdate ) {
rep = ( Report ) toSaveIn . find ( templateName ) ;
getWorkspaceArea ( ) . updateItem ( rep . getId ( ) , isZip ) ;
} else { //is new
Calendar dateCreated = Calendar . getInstance ( ) ;
dateCreated . setTime ( model . getDateCreated ( ) ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 19:17:47 +01:00
Calendar lastEdit = Calendar . getInstance ( ) ;
lastEdit . setTime ( model . getLastEdit ( ) ) ;
2013-02-06 23:25:43 +01:00
2013-10-29 19:17:47 +01:00
rep = toSaveIn . createReportItem ( templateToInsert , templateToInsert , dateCreated , lastEdit ,
model . getAuthor ( ) , model . getLastEditBy ( ) , templateToInsert , model . getSections ( ) . size ( ) , " no-status " , isZip ) ;
}
2013-02-06 23:25:43 +01:00
storeReportItemIDInSession ( rep . getId ( ) ) ;
2013-03-16 02:53:00 +01:00
if ( toSaveIn . getType ( ) = = WorkspaceItemType . SHARED_FOLDER ) {
sendReportUpdateNotification ( toSaveIn , rep ) ;
}
2013-02-06 23:25:43 +01:00
AccessLogger log = AccessLogger . getAccessLogger ( ) ;
CreateReportLogEntry logEntry = new CreateReportLogEntry ( model . getTemplateName ( ) , rep . getId ( ) ) ;
log . logEntry ( getASLSession ( ) . getUsername ( ) , getASLSession ( ) . getScopeName ( ) , logEntry ) ;
2013-03-16 02:53:00 +01:00
} catch ( Exception e ) {
2013-02-06 23:25:43 +01:00
e . printStackTrace ( ) ;
2013-03-16 02:53:00 +01:00
}
2013-02-06 23:25:43 +01:00
}
}
/ * *
2013-03-16 02:53:00 +01:00
* Return an instance of the root folder
* @param folderId
2013-02-06 23:25:43 +01:00
* @return
* @throws ItemNotFoundException
* /
2013-03-16 02:53:00 +01:00
private WorkspaceFolder getRootFolder ( String folderId ) throws ItemNotFoundException {
2013-02-06 23:25:43 +01:00
Workspace root = null ;
try {
root = getWorkspaceArea ( ) ;
} catch ( WorkspaceFolderNotFoundException e ) { e . printStackTrace ( ) ;
} catch ( InternalErrorException e ) { e . printStackTrace ( ) ;
} catch ( HomeNotFoundException e ) { e . printStackTrace ( ) ;
}
WorkspaceItem item = null ;
try {
2013-03-16 02:53:00 +01:00
item = root . getItem ( folderId ) ;
2013-02-06 23:25:43 +01:00
} catch ( ItemNotFoundException e ) {
2013-03-16 02:53:00 +01:00
_log . info ( " Folder : " + folderId + " NOT FOUND RETURNING DEFAULT ONE " ) ;
return ( WorkspaceFolder ) root . getItem ( getRootFolder ( ) ) ;
2013-02-06 23:25:43 +01:00
}
2013-03-16 02:53:00 +01:00
if ( item . getType ( ) = = WorkspaceItemType . FOLDER | | item . getType ( ) = = WorkspaceItemType . SHARED_FOLDER ) {
return ( WorkspaceFolder ) item ;
2013-02-06 23:25:43 +01:00
}
2013-03-16 02:53:00 +01:00
_log . error ( " The item id does not belong to a valid folder id: " + folderId ) ;
return null ;
2013-02-06 23:25:43 +01:00
}
2013-03-16 02:53:00 +01:00
/ * *
* send an update notification to all the user sharing this folder
* @param toSaveIn
* @throws InternalErrorException
* /
private void sendReportUpdateNotification ( WorkspaceFolder sharedFolder , WorkspaceItem wsItem ) throws InternalErrorException {
if ( sharedFolder . getType ( ) = = WorkspaceItemType . SHARED_FOLDER ) {
WorkspaceSharedFolder wsFolder = ( WorkspaceSharedFolder ) sharedFolder ;
List < String > usersToNotify = wsFolder . getUsers ( ) ;
Thread thread = new Thread ( new NotificationsThread ( getASLSession ( ) , usersToNotify , wsItem , sharedFolder ) ) ;
thread . start ( ) ;
}
}
2013-02-06 23:25:43 +01:00
/ * *
* return a sample of the given TS to the client
* @param sTS .
* @return .
* /
2013-03-01 19:42:50 +01:00
public Table getSampleTimeSeries ( RepTimeSeries sTS ) {
2013-02-06 23:25:43 +01:00
File csvTS = getTimeSeriesFromWorkspace ( sTS ) ;
2013-03-01 19:42:50 +01:00
Table toReturn = null ;
2013-02-06 23:25:43 +01:00
try {
toReturn = parseCSV ( csvTS , sTS ) ;
} catch ( ParseException e ) { e . printStackTrace ( ) ;
} catch ( IOException e ) { e . printStackTrace ( ) ;
} catch ( ProcessingException e ) { e . printStackTrace ( ) ;
}
return toReturn ;
}
/ * *
* retrieve the given TS csv representation and writes it into / tmp returning the File
* @param sTS serializable TS
* @return a File csv
* /
2013-03-01 19:42:50 +01:00
private File getTimeSeriesFromWorkspace ( RepTimeSeries sTS ) {
2013-02-06 23:25:43 +01:00
try {
String timeSeriesBasketID = sTS . getTsMetadata ( ) . getId ( ) ;
Workspace root = null ;
try {
root = getWorkspaceArea ( ) ;
} catch ( WorkspaceFolderNotFoundException e ) { e . printStackTrace ( ) ;
} catch ( InternalErrorException e ) { e . printStackTrace ( ) ;
} catch ( HomeNotFoundException e ) { e . printStackTrace ( ) ;
}
WorkspaceItem item = null ;
try {
item = root . getItem ( timeSeriesBasketID ) ;
} catch ( ItemNotFoundException e ) {
e . printStackTrace ( ) ;
}
2013-03-05 15:26:22 +01:00
_log . debug ( " Got Item TS From HL, Item Type: " + item . getType ( ) ) ;
2013-02-06 23:25:43 +01:00
if ( item . getType ( ) ! = WorkspaceItemType . FOLDER_ITEM ) {
2013-03-05 15:26:22 +01:00
_log . debug ( " The item id does not belong to a timeseries, id: " + timeSeriesBasketID ) ;
2013-02-06 23:25:43 +01:00
return null ;
}
FolderItem bItem = ( FolderItem ) item ;
if ( bItem . getFolderItemType ( ) ! = FolderItemType . TIME_SERIES ) {
2013-03-05 15:26:22 +01:00
_log . debug ( " The basket item does not belong to a timeseries, id: " + timeSeriesBasketID ) ;
2013-02-06 23:25:43 +01:00
return null ;
}
TimeSeries ts = ( TimeSeries ) bItem ;
return getTSFromBasket ( ts ) ;
}
catch ( NullPointerException e ) {
2013-03-05 15:26:22 +01:00
_log . error ( " No TS was dragged in the Area returning NULL " ) ;
2013-02-06 23:25:43 +01:00
return null ;
}
}
/ * *
*
* @param toParse the csv to parse
* @throws ProcessingException .
* @throws IOException .
* @throws ParseException .
* /
2013-03-01 19:42:50 +01:00
private Table parseCSV ( File toParse , final RepTimeSeries sTS ) throws ParseException , IOException , ProcessingException {
2013-02-06 23:25:43 +01:00
2013-03-01 19:42:50 +01:00
final Table toReturn ;
2013-02-06 23:25:43 +01:00
final boolean isFiltered ;
final int fromLine ;
final int toLine ;
//if there is no filter
if ( sTS . getFilter ( ) = = null ) {
2013-03-01 19:42:50 +01:00
toReturn = new Table ( sTS . getTsMetadata ( ) . getHeaderLabels ( ) . size ( ) ) ;
2013-02-06 23:25:43 +01:00
isFiltered = false ;
fromLine = 1 ;
toLine = 10 ;
}
else {
int headers = sTS . getFilter ( ) . getColsNumberToShow ( ) . size ( ) ;
2013-03-01 19:42:50 +01:00
toReturn = new Table ( headers ) ;
2013-02-06 23:25:43 +01:00
isFiltered = true ;
fromLine = sTS . getFilter ( ) . getFrom ( ) ;
toLine = fromLine + 10 ;
}
final CSVFileProcessor fp = new CSVFileProcessor ( ) ;
fp . processFile ( toParse . getAbsolutePath ( ) , new CSVLineProcessor ( ) {
boolean keepGoing = true ;
public void processHeaderLine ( final int linenumber , final List < String > fieldNames ) {
ArrayList < TableCell > toInsert = new ArrayList < TableCell > ( ) ;
for ( String field : fieldNames ) {
toInsert . add ( new TableCell ( field ) ) ;
}
if ( ! isFiltered )
toReturn . addRow ( ( ArrayList < TableCell > ) toInsert ) ;
else {
ArrayList < String > filteredHeaders = new ArrayList < String > ( ) ;
for ( Integer colNo : sTS . getFilter ( ) . getColsNumberToShow ( ) ) {
String toAdd = sTS . getTsMetadata ( ) . getHeaderLabels ( ) . get ( colNo ) ;
filteredHeaders . add ( toAdd ) ;
toInsert = new ArrayList < TableCell > ( ) ;
for ( String field : filteredHeaders ) {
toInsert . add ( new TableCell ( field ) ) ;
}
}
toReturn . addRow ( toInsert ) ;
}
}
public void processDataLine ( final int linenumber , final List < String > fieldValues ) {
if ( linenumber > toLine )
keepGoing = false ;
if ( linenumber > = fromLine & & linenumber < = toLine ) {
ArrayList < TableCell > toInsert = new ArrayList < TableCell > ( ) ;
for ( String field : fieldValues ) {
toInsert . add ( new TableCell ( field ) ) ;
}
if ( ! isFiltered )
toReturn . addRow ( toInsert ) ;
else {
ArrayList < String > filteredFields = new ArrayList < String > ( ) ;
for ( Integer colNo : sTS . getFilter ( ) . getColsNumberToShow ( ) ) {
String toAdd = fieldValues . get ( colNo ) ;
filteredFields . add ( toAdd ) ;
toInsert = new ArrayList < TableCell > ( ) ;
for ( String field : filteredFields ) {
toInsert . add ( new TableCell ( field ) ) ;
}
}
toReturn . addRow ( toInsert ) ;
}
}
}
public boolean continueProcessing ( ) {
return keepGoing ;
}
} ) ;
return toReturn ;
}
/ * *
* get the TS from the Basket
* @param ts
* @return a csv file
* /
private File getTSFromBasket ( TimeSeries ts ) {
try {
File temp = File . createTempFile ( ts . getName ( ) , " .csv " ) ;
InputStream inputStream = null ;
try {
inputStream = ts . getData ( ) ;
} catch ( InternalErrorException e ) {
e . printStackTrace ( ) ;
return null ;
}
OutputStream out = new FileOutputStream ( temp ) ;
byte buf [ ] = new byte [ 1024 ] ;
int len ;
while ( ( len = inputStream . read ( buf ) ) > 0 )
out . write ( buf , 0 , len ) ;
out . close ( ) ;
inputStream . close ( ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " Successfully got TimeSeries from Basket: \ n " + temp . getAbsolutePath ( ) ) ;
2013-02-06 23:25:43 +01:00
return temp ;
}
catch ( IOException e ) {
e . printStackTrace ( ) ;
return null ;
} catch ( InternalErrorException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
return null ;
}
/ * *
* each portlet instance runs in a scope
* each portlet instance is used by a unique username from within the portal
* @param currentHost .
* @return a SessionInfo bean containing the username the scope andis opening a workflow document or not
* /
public SessionInfo getSessionInfo ( String currentHost ) {
if ( testWorkflow ) {
getASLSession ( ) . setAttribute ( WfDocsLibrary . WORKFLOW_ID_ATTRIBUTE , " 1 " ) ;
getASLSession ( ) . setAttribute ( WfDocsLibrary . WORKFLOW_GIVEN_NAME , " TEST REPORT " ) ;
getASLSession ( ) . setAttribute ( WfDocsLibrary . WORKFLOW_READONLY_ATTRIBUTE , true ) ;
2013-12-17 19:19:51 +01:00
return new SessionInfo ( getUserBean ( ) , getVreName ( ) , true , true , false ) ;
2013-02-06 23:25:43 +01:00
}
2014-01-23 19:11:55 +01:00
2013-12-17 19:19:51 +01:00
boolean isVME = isReportsStoreGatewayAvailable ( ) ;
2014-01-23 19:11:55 +01:00
2013-02-06 23:25:43 +01:00
if ( getASLSession ( ) . getAttribute ( WfDocsLibrary . WORKFLOW_ID_ATTRIBUTE ) = = null ) {
2013-03-05 15:26:22 +01:00
_log . debug ( " WfDocsLibrary.WORKFLOW_ID_ATTRIBUTE is NULL: " ) ;
2013-12-17 19:19:51 +01:00
return new SessionInfo ( getUserBean ( ) , getVreName ( ) , false , false , isVME ) ;
2013-02-06 23:25:43 +01:00
}
else {
2013-03-05 15:26:22 +01:00
_log . debug ( " FOUND WORKFLOW_ID_ATTRIBUTE ***** " ) ;
2013-02-06 23:25:43 +01:00
//String workflowid = getASLSession().getAttribute(WfDocsLibrary.WORKFLOW_ID_ATTRIBUTE).toString();
Boolean canEdit = ! ( Boolean ) getASLSession ( ) . getAttribute ( WfDocsLibrary . WORKFLOW_READONLY_ATTRIBUTE ) ;
2013-12-17 19:19:51 +01:00
return new SessionInfo ( getUserBean ( ) , getVreName ( ) , true , canEdit , false ) ;
2013-02-06 23:25:43 +01:00
}
}
2013-03-01 19:42:50 +01:00
public Model getWorkflowDocumentFromDocumentLibrary ( ) {
2013-02-06 23:25:43 +01:00
ServiceUtil myUtil = new ServiceUtil ( getASLSession ( ) ) ;
if ( testWorkflow ) {
FileInputStream fis = null ;
ObjectInputStream in = null ;
2013-03-01 19:42:50 +01:00
Model toConvert = null ;
2013-02-06 23:25:43 +01:00
try {
fis = new FileInputStream ( " /Users/massi/portal/CURRENT_OPEN.d4st " ) ;
in = new ObjectInputStream ( fis ) ;
2013-03-01 19:42:50 +01:00
toConvert = ( Model ) in . readObject ( ) ;
2013-02-06 23:25:43 +01:00
in . close ( ) ;
} catch ( IOException ex ) {
ex . printStackTrace ( ) ;
} catch ( ClassNotFoundException ex ) {
ex . printStackTrace ( ) ;
}
toConvert . setTemplateName ( " TEST " ) ;
2013-03-05 15:26:22 +01:00
_log . info ( " Converting TEST REPORT to Serializable object, model name: \ n " + toConvert . getTemplateName ( ) ) ;
2013-02-06 23:25:43 +01:00
return toConvert ;
}
else {
String workflowid = getASLSession ( ) . getAttribute ( WfDocsLibrary . WORKFLOW_ID_ATTRIBUTE ) . toString ( ) ;
Boolean canEdit = ! ( Boolean ) getASLSession ( ) . getAttribute ( WfDocsLibrary . WORKFLOW_READONLY_ATTRIBUTE ) ;
String documentName = getASLSession ( ) . getAttribute ( WfDocsLibrary . WORKFLOW_GIVEN_NAME ) . toString ( ) ;
2013-03-05 15:26:22 +01:00
_log . info ( " getWorkflowDocumentFromDocumentLibrary() CALLED ***** ID = " + workflowid + " name: \ n " + documentName ) ;
2013-02-06 23:25:43 +01:00
//TODO: check this
//reset the values in session
2013-03-05 15:26:22 +01:00
_log . info ( " Reset the values in session ... " ) ;
2013-02-06 23:25:43 +01:00
getASLSession ( ) . setAttribute ( WfDocsLibrary . LAST_WORKFLOW_ID , workflowid ) ;
getASLSession ( ) . setAttribute ( WfDocsLibrary . WORKFLOW_ID_ATTRIBUTE , null ) ;
getASLSession ( ) . setAttribute ( WfDocsLibrary . WORKFLOW_READONLY_ATTRIBUTE , null ) ;
getASLSession ( ) . setAttribute ( WfDocsLibrary . WORKFLOW_GIVEN_NAME , null ) ;
try {
InputStream inputStream = DocLibraryUtil . getFileEntryAsStream ( getASLSession ( ) , workflowid ) ;
String templatePath = myUtil . getTemplateFolder ( getVreName ( ) , getUsername ( ) ) + " CURRENT_OPEN/ " ;
String pathToFile = templatePath ;
File toExtract = writeReportToDisk ( inputStream , pathToFile , " Workflodoc-report.zip " ) ;
File outputDir = new File ( myUtil . getTemplatePath ( " " , getVreName ( ) , getUsername ( ) ) ) ;
ZipUtil . unzipArchive ( toExtract , outputDir ) ;
toExtract . delete ( ) ;
FileInputStream fis = null ;
ObjectInputStream in = null ;
2013-03-01 19:42:50 +01:00
Model toConvert = null ;
2013-02-06 23:25:43 +01:00
try {
String path = myUtil . getTemplateFolder ( getVreName ( ) , getUsername ( ) ) ;
2013-11-13 18:56:13 +01:00
2013-11-07 19:00:34 +01:00
String reportFileName = seekModel ( templatePath , UUID . randomUUID ( ) . toString ( ) ) ; //random name just to make sure it look for the name
String pathToReport = path + " CURRENT_OPEN/ " + reportFileName + " .d4st " ;
_log . trace ( " WF PathToReport = " + pathToReport ) ;
fis = new FileInputStream ( pathToReport ) ;
2013-02-06 23:25:43 +01:00
in = new ObjectInputStream ( fis ) ;
2013-03-01 19:42:50 +01:00
toConvert = ( Model ) in . readObject ( ) ;
2013-02-06 23:25:43 +01:00
in . close ( ) ;
} catch ( IOException ex ) {
ex . printStackTrace ( ) ;
} catch ( ClassNotFoundException ex ) {
ex . printStackTrace ( ) ;
}
toConvert . setTemplateName ( documentName ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " Converting fileToRead to Serializable object, model name: \ n " + toConvert . getTemplateName ( ) ) ;
2013-03-01 19:42:50 +01:00
Model toReturn = ( toConvert ) ;
2013-02-06 23:25:43 +01:00
//saves this model as previous one in session
getASLSession ( ) . setAttribute ( PREVIOUS_REPORT_INSTANCE , toConvert ) ;
AccessLogger log = AccessLogger . getAccessLogger ( ) ;
OpenWorkflowLogEntry logEntry = new OpenWorkflowLogEntry ( toConvert . getTemplateName ( ) , toConvert . getUniqueID ( ) , toConvert . getAuthor ( ) ) ;
log . logEntry ( getASLSession ( ) . getUsername ( ) , getASLSession ( ) . getScopeName ( ) , logEntry ) ;
return toReturn ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
return null ;
}
}
/ * *
* get the report instance from the Basket
* @param repTmp .
* @param pathToFile the directory where to save the file
* @param filename the filename to give to the newly created file
* @return
* /
private File writeReportToDisk ( InputStream isData , String pathToFile , String filename ) {
try {
File dir = new File ( pathToFile ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " DIR: " + pathToFile ) ;
2013-02-06 23:25:43 +01:00
if ( ! dir . exists ( ) )
dir . mkdirs ( ) ;
File f = new File ( pathToFile + filename ) ;
OutputStream out = new FileOutputStream ( f ) ;
IOUtils . copy ( isData , out ) ;
out . close ( ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " Successfully WROTE ReportTemplate from DL: " + pathToFile ) ;
2013-02-06 23:25:43 +01:00
return f ;
}
catch ( IOException e ) {
e . printStackTrace ( ) ;
return null ;
}
}
/ * *
* update the Workflow Document in session
* /
2013-05-06 17:38:24 +02:00
public void updateWorkflowDocument ( Model toSave , boolean update ) {
2013-02-06 23:25:43 +01:00
ASLSession session = getASLSession ( ) ;
ServiceUtil myUtil = new ServiceUtil ( session ) ;
String workflowid = session . getAttribute ( WfDocsLibrary . LAST_WORKFLOW_ID ) . toString ( ) ;
2013-03-01 19:42:50 +01:00
Model model = null ;
2013-02-06 23:25:43 +01:00
String documentWorkflowOwnerId = store . getWorkflowById ( workflowid ) . getAuthor ( ) ;
String documentWorkflowName = store . getWorkflowById ( workflowid ) . getName ( ) ;
if ( update ) {
2013-03-05 15:26:22 +01:00
_log . debug ( " SAVING in WorkflowDocument Library " ) ;
2013-02-06 23:25:43 +01:00
2013-05-06 17:38:24 +02:00
model = ( Model ) toSave ;
2013-03-05 15:26:22 +01:00
_log . debug ( " Trying to convert dynamic images ... " ) ;
2013-04-29 17:27:29 +02:00
convertDynamicImagesFromHL ( model ) ;
2013-02-06 23:25:43 +01:00
boolean result = myUtil . writeModel ( model , " CURRENT_OPEN " , getVreName ( ) , getUsername ( ) ) ;
if ( ! result ) {
2013-03-05 15:26:22 +01:00
_log . error ( " Could not save report, serializing failed " ) ;
2013-02-06 23:25:43 +01:00
}
else {
String templatePath = myUtil . getTemplateFolder ( getVreName ( ) , getUsername ( ) ) + " CURRENT_OPEN " ;
try {
Thread . sleep ( 1000 ) ;
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
}
2013-03-05 15:26:22 +01:00
_log . debug ( " Trying to zip folder: " + templatePath ) ;
2013-02-06 23:25:43 +01:00
String folderToZip = templatePath ;
String outZip = templatePath + " -report.zip " ;
try {
ZipUtil . zipDir ( outZip , folderToZip ) ;
2013-03-05 15:26:22 +01:00
_log . debug ( " Folder zipped, result: " + outZip ) ;
2013-02-06 23:25:43 +01:00
InputStream isZip = new BufferedInputStream ( new FileInputStream ( outZip ) ) ;
DocLibraryUtil . updateFileIntoDocLibrary ( getASLSession ( ) , workflowid , getBytesFromInputStream ( isZip ) ) ;
2013-03-05 15:26:22 +01:00
_log . info ( " Updated in DOC LIB OK " ) ;
2013-02-06 23:25:43 +01:00
store . addWorkflowLogAction ( workflowid , getASLSession ( ) . getUsername ( ) , " Updated " ) ;
//send the notification
NotificationsManager nm = new ApplicationNotificationsManager ( session , " org.gcube.admin.portlet.wfdocviewer.server.WorkflowDocServiceImpl " ) ;
nm . notifyDocumentWorkflowUpdate ( documentWorkflowOwnerId , workflowid , documentWorkflowName ) ;
AccessLogger log = AccessLogger . getAccessLogger ( ) ;
SaveWorkflowLogEntry logEntry = new SaveWorkflowLogEntry ( model . getTemplateName ( ) , model . getUniqueID ( ) , model . getAuthor ( ) ) ;
log . logEntry ( getASLSession ( ) . getUsername ( ) , getASLSession ( ) . getScopeName ( ) , logEntry ) ;
}
catch ( Exception e ) {
2013-03-05 15:26:22 +01:00
_log . error ( " Could not zip template, serializing failed " ) ;
2013-02-06 23:25:43 +01:00
e . printStackTrace ( ) ;
}
}
} else {
store . addWorkflowLogAction ( workflowid , getASLSession ( ) . getUsername ( ) , " Viewed " ) ;
//send the notification
NotificationsManager nm = new ApplicationNotificationsManager ( session , " org.gcube.admin.portlet.wfdocviewer.server.WorkflowDocServiceImpl " ) ;
nm . notifyDocumentWorkflowView ( documentWorkflowOwnerId , workflowid , documentWorkflowName ) ;
}
//unlocks
unlock ( workflowid ) ;
getASLSession ( ) . setAttribute ( " idreport " , null ) ;
}
2013-03-05 15:26:22 +01:00
/ * *
*
* @return the info about the current user
* /
private UserBean getUserBean ( ) {
try {
String username = getASLSession ( ) . getUsername ( ) ;
String email = username + " @isti.cnr.it " ;
String fullName = username + " FULL " ;
String thumbnailURL = " images/Avatar_default.png " ;
if ( withinPortal ) {
UserModel user = UserLocalServiceUtil . getUserByScreenName ( OrganizationsUtil . getCompany ( ) . getCompanyId ( ) , username ) ;
thumbnailURL = " /image/user_male_portrait?img_id= " + user . getPortraitId ( ) ;
fullName = user . getFirstName ( ) + " " + user . getLastName ( ) ;
email = user . getEmailAddress ( ) ;
UserBean toReturn = new UserBean ( username , fullName , thumbnailURL , user . getEmailAddress ( ) ) ;
_log . info ( " Returning USER: " + toReturn ) ;
return toReturn ;
}
else {
UserBean toReturn = new UserBean ( getASLSession ( ) . getUsername ( ) , fullName , thumbnailURL , email ) ;
_log . info ( " Returning test USER: " + toReturn ) ;
return toReturn ;
}
2013-02-06 23:25:43 +01:00
2013-03-05 15:26:22 +01:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
return new UserBean ( ) ;
}
2013-02-06 23:25:43 +01:00
/ * *
* /
byte [ ] getBytesFromInputStream ( InputStream is ) {
ByteArrayOutputStream os = new ByteArrayOutputStream ( ) ;
try {
IOUtils . copy ( is , os ) ;
} catch ( IOException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
return os . toByteArray ( ) ;
}
/ * *
*
* @param workflowid .
* /
private void unlock ( String workflowid ) {
DLFileEntry fileEntry ;
try {
fileEntry = DocLibraryUtil . getFileEntry ( getASLSession ( ) , workflowid ) ;
2013-03-05 15:26:22 +01:00
_log . info ( " Log action saved, trying ot unlock document ... " ) ;
2013-02-06 23:25:43 +01:00
LockLocalServiceUtil . unlock ( DLFileEntry . class . getName ( ) , fileEntry . getFileEntryId ( ) ) ;
2013-03-05 15:26:22 +01:00
_log . info ( " UNLOCK OK! " ) ;
2013-02-06 23:25:43 +01:00
}
catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
public void renewLock ( ) {
HttpSession httpSes = this . getThreadLocalRequest ( ) . getSession ( ) ;
httpSes . setMaxInactiveInterval ( - 1 ) ; //session won't expire
String workflowid = getASLSession ( ) . getAttribute ( WfDocsLibrary . LAST_WORKFLOW_ID ) . toString ( ) ;
try {
DLFileEntry fileEntry = DocLibraryUtil . getFileEntry ( getASLSession ( ) , workflowid ) ;
2013-03-05 15:26:22 +01:00
_log . info ( " Renewing Lock ... " ) ;
2013-02-06 23:25:43 +01:00
long fifteenMin = 900000 ;
Date currTimePlus15 = new Date ( new Date ( ) . getTime ( ) + fifteenMin ) ;
LockLocalServiceUtil . getLock ( DLFileEntry . class . getName ( ) , fileEntry . getFileEntryId ( ) ) . setExpirationDate ( currTimePlus15 ) ;
2013-03-05 15:26:22 +01:00
_log . info ( " Lock Renewed, expiring: " + currTimePlus15 ) ;
2013-02-06 23:25:43 +01:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
2013-03-15 19:06:43 +01:00
@Override
public String save ( String filePath , String workspaceFolderId , String itemName , TypeExporter type , boolean overwrite ) throws SaveReportFileException , SaveReportFileExistException {
try {
File file = new File ( filePath ) ;
2013-10-09 14:21:50 +02:00
Workspace workspace = HomeLibrary . getUserWorkspace ( getASLSession ( ) . getUsername ( ) ) ;
2013-03-15 19:06:43 +01:00
_log . info ( " Saving in Workspace of " + workspace . getOwner ( ) . getPortalLogin ( ) ) ;
WorkspaceFolder folder = ( workspaceFolderId ! = null ) ?
( WorkspaceFolder ) workspace . getItem ( workspaceFolderId ) : workspace . getRoot ( ) ;
2013-03-16 02:53:00 +01:00
itemName = itemName + " . " + type . toString ( ) . toLowerCase ( ) ;
if ( workspace . exists ( itemName , folder . getId ( ) ) ) {
if ( overwrite )
workspace . remove ( itemName , folder . getId ( ) ) ;
else
throw new SaveReportFileException ( " The item " + itemName + " already exists " ) ;
}
switch ( type ) {
case PDF :
return folder . createExternalPDFFileItem ( itemName ,
" " , null , new FileInputStream ( file ) ) . getId ( ) ;
case HTML :
return folder . createExternalFileItem ( itemName ,
" " , " text/html " , new FileInputStream ( file ) ) . getId ( ) ;
case DOCX :
return folder . createExternalFileItem ( itemName ,
" " , " application/msword " , new FileInputStream ( file ) ) . getId ( ) ;
case XML :
return folder . createExternalFileItem ( itemName ,
" " , " application/xml " , new FileInputStream ( file ) ) . getId ( ) ;
}
throw new SaveReportFileException ( " Unknown file type " ) ;
2013-03-15 19:06:43 +01:00
} catch ( ItemAlreadyExistException e ) {
throw new SaveReportFileExistException ( e . getMessage ( ) ) ;
} catch ( Exception e ) {
throw new SaveReportFileException ( e . getMessage ( ) ) ;
}
}
2013-04-30 16:16:20 +02:00
@Override
public void renewHTTPSession ( ) {
HttpSession session = this . getThreadLocalRequest ( ) . getSession ( ) ;
_log . info ( " HTTP Session renewed " + new Date ( session . getLastAccessedTime ( ) ) ) ;
}
2013-11-13 18:56:13 +01:00
@Override
2013-11-25 17:12:01 +01:00
public ArrayList < VMEReportBean > listVMEReports ( ) {
2013-11-13 18:56:13 +01:00
try {
Thread . sleep ( 1000 ) ;
} catch ( InterruptedException e ) {
}
2013-11-25 17:12:01 +01:00
ArrayList < VMEReportBean > toReturn = new ArrayList < VMEReportBean > ( ) ;
2013-11-13 18:56:13 +01:00
ReportType type = new ReportType ( ) ;
type . setTypeIdentifier ( " Vme " ) ;
2013-12-17 19:19:51 +01:00
RsgService rsgClient = new RsgClient ( getRSGWSAddress ( ) ) ;
2013-12-04 23:21:12 +01:00
for ( ReportEntry re : rsgClient . listReports ( type ) ) {
2013-12-06 16:57:55 +01:00
String rfmo = re . getOwner ( ) ;
String name = re . getIdentifier ( ) ;
2013-12-04 23:21:12 +01:00
toReturn . add ( new VMEReportBean ( " " + re . getId ( ) , rfmo , name ) ) ;
2013-11-13 18:56:13 +01:00
}
return toReturn ;
}
@Override
2013-11-25 19:11:03 +01:00
public ArrayList < VMEReportBean > listVMEReportRefByType ( VMETypeIdentifier refType ) {
2013-11-13 18:56:13 +01:00
if ( refType = = VMETypeIdentifier . Vme )
throw new IllegalArgumentException ( " VME Type is not a reference " ) ;
try {
Thread . sleep ( 1000 ) ;
} catch ( InterruptedException e ) {
}
2013-11-25 19:11:03 +01:00
ArrayList < VMEReportBean > toReturn = new ArrayList < VMEReportBean > ( ) ;
2013-11-13 18:56:13 +01:00
ReportType type = new ReportType ( ) ;
type . setTypeIdentifier ( refType . toString ( ) ) ;
2013-12-17 19:19:51 +01:00
RsgService rsgClient = new RsgClient ( getRSGWSAddress ( ) ) ;
2013-12-06 18:23:49 +01:00
2013-12-04 23:21:12 +01:00
for ( ReportEntry re : rsgClient . listReports ( new ReportType ( refType . getId ( ) ) ) ) {
2013-12-06 16:57:55 +01:00
String rfmo = re . getOwner ( ) ;
String name = re . getIdentifier ( ) ;
toReturn . add ( new VMEReportBean ( " " + re . getId ( ) , rfmo , name ) ) ;
2013-11-13 18:56:13 +01:00
}
return toReturn ;
}
2013-11-26 18:37:13 +01:00
2013-11-13 18:56:13 +01:00
@Override
2013-12-04 23:21:12 +01:00
public Model importVMEReport ( String id , String name , VMETypeIdentifier refType ) {
2013-12-17 19:19:51 +01:00
RsgService rsgClient = new RsgClient ( getRSGWSAddress ( ) ) ;
2013-12-06 18:23:49 +01:00
2013-12-04 23:21:12 +01:00
CompiledReport cr = null ;
2013-11-26 18:37:13 +01:00
if ( refType = = VMETypeIdentifier . Vme ) {
2013-12-04 23:21:12 +01:00
_log . info ( " Importing VME id= " + id + " name= " + name ) ;
cr = rsgClient . getReportById ( new ReportType ( refType . getId ( ) ) , id ) ;
}
2013-11-26 18:37:13 +01:00
else {
2013-12-04 23:21:12 +01:00
_log . info ( " Importing Ref type= " + refType . getId ( ) + " id= " + id + " name= " + name ) ;
cr = rsgClient . getReferenceReportById ( new ReportType ( refType . getId ( ) ) , id ) ;
2013-12-06 18:23:49 +01:00
2013-11-26 18:37:13 +01:00
}
2013-12-04 23:21:12 +01:00
Model model = null ;
2013-11-13 18:56:13 +01:00
try {
2013-12-12 16:39:48 +01:00
model = new ReportManagerReportBuilder ( ) . buildReport ( cr ) ;
model . setTemplateName ( name ) ;
2013-12-18 16:30:32 +01:00
//ReportsReader.readReportStructure(model);
2013-12-04 23:21:12 +01:00
} catch ( ReportBuilderException e ) {
e . printStackTrace ( ) ;
2013-11-13 18:56:13 +01:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2014-01-22 18:31:02 +01:00
ModelReader reader = new ModelReader ( model ) ;
System . out . println ( reader ) ;
2013-12-04 23:21:12 +01:00
return model ;
2013-11-13 18:56:13 +01:00
}
2013-12-06 18:23:49 +01:00
2013-11-26 18:37:13 +01:00
@Override
public Model getVMEReportRef2Associate ( String id , VMETypeIdentifier refType ) {
2013-12-17 19:19:51 +01:00
RsgService rsgClient = new RsgClient ( getRSGWSAddress ( ) ) ;
2013-12-04 23:21:12 +01:00
_log . info ( " Importing Ref type= " + refType . getId ( ) + " id= " + id ) ;
CompiledReport cr = rsgClient . getReferenceReportById ( new ReportType ( refType . getId ( ) ) , id ) ;
Model model = null ;
2013-11-14 16:26:26 +01:00
try {
2013-12-12 16:39:48 +01:00
model = new ReportManagerReportBuilder ( ) . buildReferenceReport ( cr ) ;
2013-12-04 23:21:12 +01:00
} catch ( ReportBuilderException e ) {
e . printStackTrace ( ) ;
2013-11-14 16:26:26 +01:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2013-12-04 23:21:12 +01:00
return model ;
2013-11-14 16:26:26 +01:00
}
2014-01-23 19:11:55 +01:00
2013-12-18 16:30:32 +01:00
@Override
public Model importVMETemplate ( VMETypeIdentifier refType ) {
RsgService rsgClient = new RsgClient ( getRSGWSAddress ( ) ) ;
CompiledReport cr = null ;
if ( refType = = VMETypeIdentifier . Vme ) {
_log . info ( " Importing VME Template " ) ;
cr = rsgClient . getTemplate ( new ReportType ( refType . getId ( ) ) ) ;
}
else {
_log . info ( " Importing Ref type= " + refType . getId ( ) ) ;
cr = rsgClient . getTemplate ( new ReportType ( refType . getId ( ) ) ) ;
}
Model model = null ;
try {
model = new ReportManagerReportBuilder ( ) . buildReport ( cr ) ;
model . setTemplateName ( " New " + refType . getDisplayName ( ) ) ;
} catch ( ReportBuilderException e ) {
e . printStackTrace ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
return model ;
}
2014-01-23 19:11:55 +01:00
@Override
2014-01-28 17:31:13 +01:00
public VmeExportResponse exportReportToRSG ( VMETypeIdentifier refType , Model model ) {
2014-01-24 12:50:50 +01:00
System . out . println ( new ModelReader ( model ) . toString ( ) ) ;
2014-01-28 17:31:13 +01:00
2014-01-23 19:11:55 +01:00
RsgService rsgClient = new RsgClient ( getRSGWSAddress ( ) ) ;
//Use the RSG client to get a template for the report whose type is the last token (i.e. the corresponding class' 'simple name')
//appearing in the VME model class name as stored in the 'type' metadata
2014-01-29 17:34:57 +01:00
CompiledReport template = null ;
if ( refType = = VMETypeIdentifier . Vme ) {
_log . info ( " getTemplate for VME Report " ) ;
template = rsgClient . getTemplate ( new ReportType ( refType . getId ( ) ) ) ;
}
else {
_log . info ( " getTemplate for Ref Report " ) ;
template = rsgClient . getRefTemplate ( new ReportType ( refType . getId ( ) ) ) ;
}
2014-01-23 19:11:55 +01:00
CompiledReport toSend = null ;
try {
2014-01-24 16:25:54 +01:00
toSend = new ReportManagerReportBuilder ( ) . extract ( template , model ) ;
} catch ( Exception e ) {
2014-01-23 19:11:55 +01:00
e . printStackTrace ( ) ;
}
2014-01-28 17:31:13 +01:00
try {
//Actual RSG client interface exposes different methods (publish / publishDelta etc.) that need to be updated
Response res = null ;
if ( refType = = VMETypeIdentifier . Vme ) {
_log . info ( " Exporting VME Report " ) ;
res = rsgClient . update ( toSend ) ;
}
else {
_log . info ( " Exporting Ref type= " + refType . getId ( ) ) ;
res = rsgClient . updateRef ( toSend ) ;
}
return getClientResponse ( res ) ;
} catch ( RuntimeException ex ) {
ex . printStackTrace ( ) ;
return new VmeExportResponse ( new VmeResponseEntry ( " RUNTIME_EXCEPTION " , " Sorry, there was an error on the Server, please try again in few minutes or report an Issue. " ) ) ;
}
2014-01-23 19:11:55 +01:00
}
2014-01-28 17:31:13 +01:00
@Override
public VmeExportResponse deleteReportFromRSG ( VMETypeIdentifier refType , String idToDelete ) {
RsgService rsgClient = new RsgClient ( getRSGWSAddress ( ) ) ;
try {
Response res = null ;
if ( refType = = VMETypeIdentifier . Vme ) {
_log . info ( " Deleting VME Report id = " + idToDelete ) ;
res = rsgClient . deleteById ( new ReportType ( refType . getId ( ) ) , idToDelete ) ;
}
else {
_log . info ( " Deleting VME Reference Report of Type " + refType + " having id = " + idToDelete ) ;
res = rsgClient . deleteReferenceById ( new ReportType ( refType . getId ( ) ) , idToDelete ) ;
}
return getClientResponse ( res ) ;
} catch ( RuntimeException ex ) {
ex . printStackTrace ( ) ;
return new VmeExportResponse ( new VmeResponseEntry ( " RUNTIME_EXCEPTION " , " Sorry, there was an error on the Server, we could not delete. Please try again in few minutes or report an Issue. " ) ) ;
}
}
2013-12-17 19:19:51 +01:00
/ * *
2013-12-18 16:30:32 +01:00
* this method look for a ReportsStoreGateway WS available in the infrastructure
2013-12-17 19:19:51 +01:00
* @return true if an instance of the ReportsStoreGateway is available in the infrastructure
* /
private boolean isReportsStoreGatewayAvailable ( ) {
String scope = getASLSession ( ) . getScope ( ) ;
_log . info ( " Looking for a running ReportsStoreGateway WS in " + scope ) ;
String previousScope = ScopeProvider . instance . get ( ) ;
ScopeProvider . instance . set ( scope ) ;
SimpleQuery query = queryFor ( ServiceEndpoint . class ) ;
query . addCondition ( " $resource/Profile/Category/string() eq 'Service' " ) ;
query . addCondition ( " $resource/Profile/Name/string() eq 'ReportsStoreGateway' " ) ;
DiscoveryClient < ServiceEndpoint > client = clientFor ( ServiceEndpoint . class ) ;
List < ServiceEndpoint > list = client . submit ( query ) ;
ScopeProvider . instance . set ( previousScope ) ;
if ( list . size ( ) > 1 ) {
_log . warn ( " Multiple ReportsStoreGateway Service Endpoints available in the scope, should be only one. " ) ;
return false ;
}
else if ( list . size ( ) = = 1 ) {
ServiceEndpoint se = list . get ( 0 ) ;
String host = se . profile ( ) . runtime ( ) . hostedOn ( ) ;
String accessPoint = se . profile ( ) . accessPoints ( ) . iterator ( ) . next ( ) . address ( ) ;
setRSGWSAddress ( host + accessPoint ) ;
return true ;
}
else return false ;
}
2014-01-23 19:11:55 +01:00
2014-01-28 17:31:13 +01:00
private VmeExportResponse getClientResponse ( Response rsgResponse ) {
VmeExportResponse toReturn = new VmeExportResponse ( ) ;
for ( ResponseEntry entry : rsgResponse . getResponseMessageList ( ) ) {
String entryCode = entry . getResponseCode ( ) = = null ? " no-code " : entry . getResponseCode ( ) . toString ( ) ;
String entryMessage = entry . getResponseMessage ( ) = = null ? " no response message " : entry . getResponseMessage ( ) ;
toReturn . getResponseMessageList ( ) . add ( new VmeResponseEntry ( entryCode , entryMessage ) ) ;
}
toReturn . setGloballySucceded ( rsgResponse . isGloballySucceeded ( ) ) ;
return toReturn ;
}
2014-01-23 19:11:55 +01:00
2013-02-06 23:25:43 +01:00
}