Commit 84ef1ae6 authored by Arnaud Saval's avatar Arnaud Saval
Browse files

WEBLAB-958 merge with portlet from incubation

git-svn-id: svn://svn.forge.objectweb.org/svnroot/weblab/trunk/WebLabServices/Portlets/metadata-portlet@3389 a803c184-d8de-4a0e-a52b-d3eef88212b6
parent 1f7d7e01
......@@ -5,22 +5,16 @@
<groupId>org.ow2.weblab.portlets</groupId>
<artifactId>parent</artifactId>
<version>1.2.5</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<groupId>org.ow2.weblab.portlets</groupId>
<artifactId>weblab-metaview</artifactId>
<version>1.2.0</version>
<packaging>war</packaging>
<version>2.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>WebLab Metaview</name>
<description>this portlet allows you to display chosen metadata to the user for a given Document</description>
<dependencies>
<dependency>
<groupId>org.ow2.weblab.portlets</groupId>
<artifactId>weblab-portlet</artifactId>
</dependency>
<dependency>
<groupId>org.ow2.weblab.core.helpers</groupId>
<artifactId>rdf-helper-jena</artifactId>
......@@ -29,16 +23,37 @@
<groupId>org.ow2.weblab.core.helpers</groupId>
<artifactId>rdf-helper-jena-selection</artifactId>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.ow2.weblab.components</groupId>
<artifactId>weblab-client</artifactId>
<version>0.2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.ow2.weblab.portlets</groupId>
<artifactId>weblab-portlet-filters</artifactId>
<version>0.2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</dependency>
</dependencies>
<description>This portlet displays WebLab resources metadata.</description>
<build>
<finalName>${project.artifactId}</finalName>
</build>
......
/**
* WEBLAB: Service oriented integration platform for media mining and intelligence applications
*
* Copyright (C) 2004 - 2012 Cassidian, an EADS Company
*
* This library is free software; you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation; either version
* 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
package org.ow2.weblab.portlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ow2.weblab.components.client.WebLabClient;
import org.ow2.weblab.core.extended.exception.WebLabCheckedException;
import org.ow2.weblab.core.extended.ontologies.WebLabRetrieval;
import org.ow2.weblab.core.extended.util.ResourceUtil;
import org.ow2.weblab.core.helper.impl.AdvancedSelector;
import org.ow2.weblab.core.helper.impl.RDFSelectorFactory;
import org.ow2.weblab.core.helper.impl.Statements;
import org.ow2.weblab.core.helper.impl.WTriple;
import org.ow2.weblab.core.model.Document;
import org.ow2.weblab.core.model.MediaUnit;
import org.ow2.weblab.core.model.PieceOfKnowledge;
import org.ow2.weblab.core.model.Resource;
import org.ow2.weblab.core.services.ResourceContainer;
import org.ow2.weblab.core.services.resourcecontainer.LoadResourceArgs;
import org.ow2.weblab.portlet.bean.AnnotationsDescBean;
import org.ow2.weblab.portlet.bean.MetaConfBean;
import org.ow2.weblab.portlet.bean.RepoServiceConfigBean;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
* A portlet to display WebLab resources metadata.
*
* @author Emilien
*/
public class MetaDataPortlet extends WebLabPortlet {
/**
* Repository Service URI
*/
private static final String REPO_SERVICE_URI = "repo_service_uri";
/**
* Namespace property used in PieceOkKnowledge to get the Document to
* display URI
*/
private static final String POK_PROPERTY_DOC_URI = WebLabRetrieval.IS_LINKED_TO;
/**
* User's media unit identifier in session
*/
private final static String USER_MEDIA_UNIT = "user_media_unit";
private static final String USER_MEDIA_UNIT_WTMAP = "user_mediaunit_triplemap";
/**
* Attribute name of the user's current display configuration bean in
* session
*/
private static final String META_CONF_BEAN_ID = "meta_conf_bean";
/**
* Attribute name used to send hit description to jsp
*/
public static final String META_MAP_DESC = "resource_desc";
/**
* Default meta configuration bean
*/
public static MetaConfBean DEFAULT_META_CONF_BEAN;
/**
* Attribute name used to send display configuration bean to jsp
*/
public static final String USER_META_CONF = "meta_conf_bean";
private static final String USER_RES_ANNOTS_LOADED = "user_wtriple_map_isloaded";
private static final String USER_MEDIA_UNIT_URI = "resource_uri";
/**
* Attribute name used to send repository service configuration bean to jsp
*/
public static final String USER_REPO_CONF = "user_repo_service_conf";
/**
* Attribute name used to send a error message to jsp
*/
public static final String ERROR = "message_error";
/**
* Variable identifier in session to know if the user's current document is
* loaded or not
*/
private final static String CURRENT_RESSOURCE_IS_LOADED = "user_resource_is_loaded";
/**
* Default AnnotationsDescBean
*/
private AnnotationsDescBean rdfProperties;
protected final Log logger;
public MetaDataPortlet() {
super();
this.logger = LogFactory.getLog(this.getClass());
this.logger.info("MetaDataPortlet created.");
}
@Override
public void init() throws PortletException {
super.init();
/*
* creating map of repo service
*/
//this.repoMap = new HashMap<URL, ResourceContainer>();
/*
* getting meta-data description list from a XML bean, using Spring
*/
final ClassPathResource resource = new ClassPathResource("conf_meta.xml");
final BeanFactory factory = new XmlBeanFactory(resource);
this.rdfProperties = factory.getBean("metaConf", AnnotationsDescBean.class);
/*
* initializing meta conf factory
*/
MetaDataPortlet.DEFAULT_META_CONF_BEAN = factory.getBean(MetaDataPortlet.META_CONF_BEAN_ID, MetaConfBean.class);
this.logger.info("MetaDataPortlet initialised.");
}
@Override
public void destroy() {
this.rdfProperties = null;
super.destroy();
}
/**
* Render HTML corresponding to the view mode
*/
@Override
public void doView(final RenderRequest req, final RenderResponse res) throws IOException, PortletException {
final long start = System.currentTimeMillis();
boolean sessionExpired = false;
/*
* changing portlet title
*/
res.setTitle(ResourceBundle.getBundle("meta_portlet", req.getLocale()).getString("portlet.title"));
/*
* checking for session expiration
*/
if ((req.getPortletSession().getLastAccessedTime() + (req.getPortletSession().getMaxInactiveInterval() * 1000)) < System.currentTimeMillis()) {
sessionExpired = true;
req.setAttribute("message_warning", ResourceBundle.getBundle("meta_portlet", req.getLocale()).getString("portlet.warning.session_expired"));
}
if (!sessionExpired) {
final String errorParam = req.getParameter(MetaDataPortlet.ERROR);
if ((errorParam != null) && !errorParam.equals("")) {
/*
* dispatch to error jsp
*/
this.logger.error(MetaDataPortlet.ERROR + " " + errorParam);
req.setAttribute(MetaDataPortlet.ERROR, errorParam);
final PortletRequestDispatcher rd = this.getPortletContext().getRequestDispatcher(this.getInitParameter("error_page_url"));
rd.include(req, res);
}
/*
* injecting configuration bean
*/
req.setAttribute(MetaDataPortlet.USER_MEDIA_UNIT,
req.getPortletSession().getAttribute(MetaDataPortlet.USER_MEDIA_UNIT, PortletSession.APPLICATION_SCOPE));
/*
* dispatch to jsp
*/
final PortletRequestDispatcher rd = this.getPortletContext().getRequestDispatcher(this.getInitParameter("view_page_url"));
rd.include(req, res);
} else {
/*
* Dispatch warning to JSP page
*/
final PortletRequestDispatcher rd = this.getPortletContext().getRequestDispatcher(this.getInitParameter("warning_page_url"));
rd.include(req, res);
}
this.logger.info("doView reponse time:" + Long.toString(System.currentTimeMillis() - start));
}
@Override
public void doEdit(final RenderRequest req, final RenderResponse res) throws PortletException, IOException {
final long start = System.currentTimeMillis();
/*
* meta conf bean
*/
final MetaConfBean metaConfbean;
if (req.getPortletSession().getAttribute(MetaDataPortlet.USER_META_CONF, PortletSession.APPLICATION_SCOPE) == null) {
metaConfbean = MetaDataPortlet.DEFAULT_META_CONF_BEAN;
} else {
metaConfbean = (MetaConfBean) req.getPortletSession().getAttribute(MetaDataPortlet.USER_META_CONF, PortletSession.APPLICATION_SCOPE);
}
/*
* injecting configuration bean
*/
req.setAttribute(MetaDataPortlet.USER_META_CONF, metaConfbean);
/*
* repository conf bean
*/
final RepoServiceConfigBean repoConfBean;
if (req.getPortletSession().getAttribute(MetaDataPortlet.USER_REPO_CONF, PortletSession.APPLICATION_SCOPE) == null) {
repoConfBean = new RepoServiceConfigBean();
} else {
repoConfBean = (RepoServiceConfigBean) req.getPortletSession().getAttribute(MetaDataPortlet.USER_REPO_CONF, PortletSession.APPLICATION_SCOPE);
}
/*
* injecting configuration bean
*/
req.setAttribute(MetaDataPortlet.USER_REPO_CONF, repoConfBean);
/*
* dispatch to jsp
*/
final PortletRequestDispatcher rd = this.getPortletContext().getRequestDispatcher(this.getInitParameter("edit_page_url"));
rd.include(req, res);
this.logger.info("doEdit reponse time :" + Long.toString(System.currentTimeMillis() - start));
}
@Override
public void processEvent(final EventRequest req, final EventResponse resp) throws PortletException, IOException {
/*
* long to get duration
*/
final long start = System.currentTimeMillis();
/*
* document received
*/
Document doc_evt = null;
/*
* checking action according portlet.xml
*/
if (this.getReaction(req.getEvent().getQName()).getLocalPart().equals("displayMeta")) {
// receive a event containing a document
/*
* Setting IS_NOT_LOADED to true because the document is already
* loaded
*/
req.getPortletSession().setAttribute(MetaDataPortlet.CURRENT_RESSOURCE_IS_LOADED, Boolean.TRUE, PortletSession.APPLICATION_SCOPE);
doc_evt = (Document) req.getEvent().getValue();
this.logger.info("receive a loaded document");
} else if (this.getReaction(req.getEvent().getQName()).getLocalPart().equals("loadAndDisplayMeta")) {
// receive a event containg a PieceOfKnowledge about a document
/*
* Setting IS_NOT_LOADED to false because the document is not loaded
* yet
*/
req.getPortletSession().setAttribute(MetaDataPortlet.CURRENT_RESSOURCE_IS_LOADED, Boolean.FALSE, PortletSession.APPLICATION_SCOPE);
final PieceOfKnowledge pok = (PieceOfKnowledge) req.getEvent().getValue();
/*
* getting the document URI using RDFSelector
*/
final String docURI = this.getURIFromPoK(pok);
if (docURI != null) {
/*
* creating the document using the retrieve URI
*/
doc_evt = new Document();
doc_evt.setUri(docURI);
this.logger.info("receive an unloaded document with URI : " + doc_evt.getUri());
} else {
// no URI found in pok
try {
this.logger.warn("receive an unloaded document with unavailable URI (null), corresponding PoK :" + ResourceUtil.saveToXMLString(pok));
} catch (final WebLabCheckedException e) {
this.logger.error("Unable to get PoK XML", e);
}
}
} else {
this.logger.info("Unsupported event :" + req.getEvent().getQName());
}
if (doc_evt != null) {
/*
* Setting media unit in PortletSession
*/
req.getPortletSession().setAttribute(MetaDataPortlet.USER_MEDIA_UNIT, doc_evt, PortletSession.APPLICATION_SCOPE);
req.getPortletSession().setAttribute(MetaDataPortlet.USER_RES_ANNOTS_LOADED, Boolean.FALSE, PortletSession.APPLICATION_SCOPE);
}
this.logger.info("process event " + req.getEvent().getQName() + "reponse time :" + Long.toString(System.currentTimeMillis() - start));
}
@SuppressWarnings("unchecked")
@Override
public void serveResource(final ResourceRequest request, final ResourceResponse response) throws PortletException, IOException {
/*
* long to get duration
*/
final long start = System.currentTimeMillis();
/*
* user current document
*/
MediaUnit unit = null;
if ((request.getPortletSession().getAttribute(MetaDataPortlet.USER_MEDIA_UNIT, PortletSession.APPLICATION_SCOPE) != null)
&& (request.getPortletSession().getAttribute(MetaDataPortlet.CURRENT_RESSOURCE_IS_LOADED, PortletSession.APPLICATION_SCOPE) != null)) {
/*
* get document from session
*/
unit = (MediaUnit) request.getPortletSession().getAttribute(MetaDataPortlet.USER_MEDIA_UNIT, PortletSession.APPLICATION_SCOPE);
/*
* checking the document is load
*/
if (!((Boolean) request.getPortletSession().getAttribute(MetaDataPortlet.CURRENT_RESSOURCE_IS_LOADED, PortletSession.APPLICATION_SCOPE))
.booleanValue()) {
/*
* Document need to be loaded from repository
*/
final String uri_to_load = unit.getUri();
if (uri_to_load != null) {
/*
* load a resource
*/
boolean loaded = false;
this.logger.info("new resource to load: " + uri_to_load);
/*
* getting Document from repository
*/
final LoadResourceArgs args = new LoadResourceArgs();
args.setResourceId(uri_to_load);
Resource loadedResource = null;
/*
* setting user repo url
*/
if (request.getPortletSession().getAttribute(MetaDataPortlet.USER_REPO_CONF, PortletSession.APPLICATION_SCOPE) == null) {
final RepoServiceConfigBean repo_conf = new RepoServiceConfigBean();
repo_conf.setServiceURI(this.getInitParameter(REPO_SERVICE_URI));
request.getPortletSession().setAttribute(MetaDataPortlet.USER_REPO_CONF, repo_conf, PortletSession.APPLICATION_SCOPE);
}
final String repoURL = ((RepoServiceConfigBean) request.getPortletSession().getAttribute(MetaDataPortlet.USER_REPO_CONF,
PortletSession.APPLICATION_SCOPE)).getServiceURI();
this.logger.info("use repository: " + repoURL.toString());
try {
/*
* call web service
*/
ResourceContainer repo = this.getRepoService(repoURL);
loadedResource = repo.loadResource(args).getResource();
loaded = true;
} catch (final Exception e) {
this.logger.error("An error occurs retrieving resource from repo.", e);
request.setAttribute(MetaDataPortlet.ERROR, "Unable to get resource on repository service");
loaded = false;
}
if (loaded && (loadedResource instanceof MediaUnit)) {
unit = (MediaUnit) loadedResource;
/*
* Setting media unit in PortletSession and
* CURRENT_RESOURCE_IS_LOADED
*/
request.getPortletSession().setAttribute(MetaDataPortlet.USER_MEDIA_UNIT, unit, PortletSession.APPLICATION_SCOPE);
request.getPortletSession().setAttribute(MetaDataPortlet.CURRENT_RESSOURCE_IS_LOADED, Boolean.TRUE, PortletSession.APPLICATION_SCOPE);
}
}
}
/*
* resource loaded, checking for problem when loading
*/
if (request.getAttribute(MetaDataPortlet.ERROR) != null) {
/*
* Dispatch view to JSP page
*/
response.setContentType("text/html");
final PortletRequestDispatcher rd = this.getPortletContext().getRequestDispatcher(this.getInitParameter("error_page_url"));
rd.include(request, response);
} else {
/*
* no error during loading resource : continuing serving
* resource
*/
Statements mediaUnitWTMap = null;
final Document doc = (Document) request.getPortletSession().getAttribute(MetaDataPortlet.USER_MEDIA_UNIT, PortletSession.APPLICATION_SCOPE);
if (request.getPortletSession().getAttribute(MetaDataPortlet.USER_RES_ANNOTS_LOADED, PortletSession.APPLICATION_SCOPE) != null) {
if (!((Boolean) request.getPortletSession().getAttribute(MetaDataPortlet.USER_RES_ANNOTS_LOADED, PortletSession.APPLICATION_SCOPE))
.booleanValue()) {
/*
* getting rdf map from rs
*/
final AdvancedSelector rdfSelector = RDFSelectorFactory.getSelector(true);
rdfSelector.limitToFirstLevelAnnotation(true);
mediaUnitWTMap = rdfSelector.searchFor(doc, this.rdfProperties.getDisctinctAnnotationsNS());
request.getPortletSession().setAttribute(MetaDataPortlet.USER_MEDIA_UNIT_WTMAP, mediaUnitWTMap, PortletSession.APPLICATION_SCOPE);
request.getPortletSession().setAttribute(MetaDataPortlet.USER_RES_ANNOTS_LOADED, Boolean.TRUE, PortletSession.APPLICATION_SCOPE);
}
}
if (request.getPortletSession().getAttribute(MetaDataPortlet.USER_MEDIA_UNIT_WTMAP, PortletSession.APPLICATION_SCOPE) != null) {
mediaUnitWTMap = (Statements) request.getPortletSession().getAttribute(MetaDataPortlet.USER_MEDIA_UNIT_WTMAP,
PortletSession.APPLICATION_SCOPE);
final Map<String, List<String>> res_desc = new HashMap<String, List<String>>();
for (final WTriple statement : mediaUnitWTMap.get(doc.getUri()).getStatements()) {
final LinkedList<String> l = new LinkedList<String>();
if (mediaUnitWTMap.get(doc.getUri()).getValue(statement.getPredicate()) instanceof List<?>) {
l.addAll((List<String>) mediaUnitWTMap.get(doc.getUri()).getValue(statement.getPredicate()));
} else {
l.add((String) mediaUnitWTMap.get(doc.getUri()).getValue(statement.getPredicate()));
}
res_desc.put(statement.getPredicate(), l);
}
/*
* injecting meta-data
*/
request.setAttribute(MetaDataPortlet.META_MAP_DESC, res_desc);
/*
* injecting resource uri
*/
request.setAttribute(MetaDataPortlet.USER_MEDIA_UNIT_URI, doc.getUri());
/*
* injecting rdf bean
*/
request.setAttribute("rdfProperties", this.rdfProperties.getProperties());
}
/*
* getting description map from splitter
*/
this.logger.debug("resource map " + mediaUnitWTMap);
/*
* getting meta configuration from session
*/