package org.gcube.portlets.user.dataminermanager.server; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpSession; import org.gcube.application.framework.accesslogger.library.impl.AccessLogger; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.common.homelibrary.home.HomeLibrary; 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.exceptions.ItemAlreadyExistException; import org.gcube.common.homelibrary.util.WorkspaceUtil; import org.gcube.portlets.user.dataminermanager.client.bean.ComputationStatus; import org.gcube.portlets.user.dataminermanager.client.bean.Operator; import org.gcube.portlets.user.dataminermanager.client.bean.OperatorsClassification; import org.gcube.portlets.user.dataminermanager.client.bean.output.ImagesResource; import org.gcube.portlets.user.dataminermanager.client.bean.output.MapResource; import org.gcube.portlets.user.dataminermanager.client.bean.output.Resource; import org.gcube.portlets.user.dataminermanager.client.bean.parameters.Parameter; import org.gcube.portlets.user.dataminermanager.client.rpc.DataMinerPortletService; import org.gcube.portlets.user.dataminermanager.server.smservice.SClient; import org.gcube.portlets.user.dataminermanager.server.util.DataMinerWorkAreaManager; import org.gcube.portlets.user.dataminermanager.server.util.SessionUtil; import org.gcube.portlets.user.dataminermanager.server.util.StorageUtil; import org.gcube.portlets.user.dataminermanager.server.util.TableReader; import org.gcube.portlets.user.dataminermanager.shared.data.ComputationId; import org.gcube.portlets.user.dataminermanager.shared.data.TableItemSimple; import org.gcube.portlets.user.dataminermanager.shared.exception.ServiceException; import org.gcube.portlets.user.dataminermanager.shared.session.UserInfo; import org.gcube.portlets.user.dataminermanager.shared.workspace.DataMinerWorkArea; import org.gcube.portlets.user.dataminermanager.shared.workspace.ItemDescription; //import org.gcube.portlets.user.tdw.server.datasource.DataSourceFactoryRegistry; //import org.gcube.portlets.widgets.file_dw_import_wizard.server.file.TargetRegistry; import org.gcube.portlets.widgets.wsexplorer.shared.Item; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.allen_sauer.gwt.log.client.Log; import com.google.gwt.user.server.rpc.RemoteServiceServlet; /** * The server side implementation of the RPC service. */ @SuppressWarnings("serial") public class DataMinerManagerServiceImpl extends RemoteServiceServlet implements DataMinerPortletService { public static AccessLogger accountinglog = AccessLogger.getAccessLogger(); // private static final long CACHE_REFRESH_TIME = 30 * 60 * 1000; // 30 // minutes // private final static String SEPARATOR = AlgorithmConfiguration // .getListSeparator(); private static Logger logger = LoggerFactory .getLogger(DataMinerManagerServiceImpl.class); /** * {@inheritDoc} */ @Override public void init() throws ServletException { super.init(); logger.info("DataMinerManager started!"); } /** * * {@inheritDoc} * */ @Override public UserInfo hello() throws ServiceException { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); UserInfo userInfo = new UserInfo(aslSession.getUsername(), aslSession.getGroupId(), aslSession.getGroupName(), aslSession.getScope(), aslSession.getScopeName(), aslSession.getUserEmailAddress(), aslSession.getUserFullName()); logger.debug("UserInfo: " + userInfo); return userInfo; } catch (ServiceException e) { e.printStackTrace(); throw e; } catch (Throwable e) { e.printStackTrace(); logger.error("Hello(): " + e.getLocalizedMessage(), e); throw new ServiceException(e.getLocalizedMessage()); } } @Override public List getOperatorsClassifications() throws Exception { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); SClient smClient = SessionUtil.getSClient(session, aslSession); return smClient.getOperatorsClassifications(); } catch (Throwable e) { logger.error( "An error occurred getting the OperatorsClassifications list", e); System.out .println("An error occurred getting the OperatorsClassifications list"); e.printStackTrace(); // return null; throw new Exception(e.getLocalizedMessage()); } } @Override public List getParameters(Operator operator) throws Exception { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); SClient smClient = SessionUtil.getSClient(session, aslSession); return smClient.getInputParameters(operator); } catch (Throwable e) { logger.error("Error retrieving parameters: " + e.getLocalizedMessage()); e.printStackTrace(); throw new Exception(e.getLocalizedMessage()); } } @Override public ComputationId startComputation(Operator operator, String computationTitle, String computationDescription) throws Exception { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); logger.debug("StartComputation(): [computationTitle=" + computationTitle + ", computationDescription=" + computationDescription + ", operator=" + operator + "]"); SClient smClient = SessionUtil.getSClient(session, aslSession); return smClient.startComputation(operator, computationTitle, computationDescription); } catch (Throwable e) { logger.error("Error in start computation: " + e.getLocalizedMessage()); e.printStackTrace(); throw new Exception(e.getLocalizedMessage()); } } @Override public ComputationId resubmit(ComputationId computationId) throws ServiceException { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); logger.debug("Resubmit(): " + computationId); SClient smClient = SessionUtil.getSClient(session, aslSession); return smClient.resubmitComputation(computationId); } catch (Throwable e) { logger.error("Error in resubmit computation: " + e.getLocalizedMessage()); e.printStackTrace(); throw new ServiceException(e.getLocalizedMessage()); } } @Override public ComputationStatus getComputationStatus(ComputationId computationId) throws Exception { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); SClient smClient = SessionUtil.getSClient(session, aslSession); return smClient.getComputationStatus(computationId); } catch (Throwable e) { // logger.error("Error in getComputationStatus: " // + e.getLocalizedMessage()); // e.printStackTrace(); throw new Exception(e.getLocalizedMessage()); } } /* @Override public List getTableItems(List templates) throws Exception { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); List list = new ArrayList(); return list; } catch (Throwable e) { logger.error("Error in getTableItems:" + e.getLocalizedMessage()); e.printStackTrace(); throw new Exception(e.getLocalizedMessage()); } }*/ @Override public List getFileItems(List templates) throws Exception { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); List list = new ArrayList(); return list; } catch (Throwable e) { logger.error("Error in getFileItems:" + e.getLocalizedMessage()); e.printStackTrace(); throw new Exception(e.getLocalizedMessage()); } } @Override public Resource getResourceByComputationId(ComputationId computationId) { try { Log.debug("getResourceByComputationId: " + computationId); HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); SClient smClient = SessionUtil.getSClient(session, aslSession); Resource resource = smClient .getResourceByComputationId(computationId); Log.debug("Resoure: " + resource); return resource; } catch (Exception e) { logger.error("Error in getResourceByComputationId: " + e.getLocalizedMessage()); e.printStackTrace(); return null; } } @Override public Map getParametersMapByComputationId( ComputationId jobId) throws ServiceException { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); SClient smClient = SessionUtil.getSClient(session, aslSession); // SMComputation computation = wpsClient.getComputation(jobId); Map parametersMap = new LinkedHashMap(); // for (SMEntry entry : computation.parameters()) { // String value = entry.value(); // if (value != null) // value = value.replaceAll("#", ", "); // parametersMap.put(entry.key(), value); // } return parametersMap; } catch (Throwable e) { logger.error(e.getLocalizedMessage(), e); e.printStackTrace(); throw new ServiceException(e.getLocalizedMessage(), e); } } @Override public Map getMapFromMapResource(MapResource mapRes) throws Exception { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); return mapRes.getMap(); } catch (Exception e) { logger.error(e.getLocalizedMessage(), e); // e.printStackTrace(); throw e; } } @Override public String saveImage(ComputationId computationId, ImagesResource imagesResource) throws Exception { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); //SClient smClient = SessionUtil.getSClient(session, aslSession); // SMComputation computation = // factory.gegetComputation(computationId); // String computationName = computation.title() == null ? // computationId // : computation.title(); // computationName = // FileSystemNameUtil.cleanFileName(computationName); Workspace workspace = HomeLibrary.getUserWorkspace(aslSession .getUsername()); WorkspaceFolder rootFolder = workspace.getRoot(); // computationName = WorkspaceUtil.getUniqueName(computationName, // rootFolder); // create the folder WorkspaceFolder folder; try { folder = rootFolder .createFolder(computationId.getId(), "This folder contains images obtained from Data Miner Manager"); } catch (ItemAlreadyExistException e) { folder = (WorkspaceFolder) rootFolder.find(computationId .getId()); } InputStream inputStream = new URL(imagesResource.getLink()) .openStream(); WorkspaceItem wi = WorkspaceUtil.createExternalFile(folder, imagesResource.getResourceId(), imagesResource.getDescription(), imagesResource.getMimeType(), inputStream); return wi.getId(); } catch (Throwable e) { logger.error("Error in save images: " + e.getLocalizedMessage()); e.printStackTrace(); throw (e); } } @Override public void removeResource(String id) throws Exception { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); } catch (Exception e) { logger.error(e.getLocalizedMessage(), e); throw (e); } } @Override public void removeImport(String id) throws Exception { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); } catch (Exception e) { logger.error(e.getLocalizedMessage(), e); e.printStackTrace(); throw (e); } } @Override public TableItemSimple retrieveTableInformation(Item item) throws ServiceException { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); logger.debug("retrieveTableInformation(): " + item); TableReader tableReader = new TableReader(aslSession, item); TableItemSimple tableItemSimple = tableReader.getTableItemSimple(); return tableItemSimple; } catch (ServiceException e) { throw e; } catch (Throwable e) { logger.error(e.getLocalizedMessage(), e); e.printStackTrace(); throw new ServiceException(e.getLocalizedMessage()); } } @Override public DataMinerWorkArea getDataMinerWorkArea() throws ServiceException { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); logger.debug("getDataMinerWorkArea()"); DataMinerWorkAreaManager dataMinerWorkAreaManager = new DataMinerWorkAreaManager( aslSession); return dataMinerWorkAreaManager.getDataMinerWorkArea(); } catch (ServiceException e) { throw e; } catch (Throwable e) { logger.error(e.getLocalizedMessage(), e); e.printStackTrace(); throw new ServiceException(e.getLocalizedMessage()); } } @Override public String getPublicLink(ItemDescription itemDescription) throws ServiceException { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); logger.debug("GetPublicLink(): " + itemDescription); String link = StorageUtil.getPublicLink(aslSession.getUsername(), itemDescription.getId()); return link; } catch (ServiceException e) { throw e; } catch (Throwable e) { logger.error(e.getLocalizedMessage(), e); e.printStackTrace(); throw new ServiceException(e.getLocalizedMessage()); } } @Override public String cancelComputation(ComputationId computationId) throws ServiceException { try { HttpSession session = this.getThreadLocalRequest().getSession(); ASLSession aslSession = SessionUtil.getASLSession(session); logger.debug("CancelComputation(): " + computationId); SClient smClient = SessionUtil.getSClient(session, aslSession); String result = smClient.cancelComputation(computationId); return result; } catch (ServiceException e) { throw e; } catch (Throwable e) { logger.error(e.getLocalizedMessage(), e); e.printStackTrace(); throw new ServiceException(e.getLocalizedMessage()); } } }