org.semanticwb.portal.resources.CollectionResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of SWBPortal Show documentation
Show all versions of SWBPortal Show documentation
SemanticWebBuilder Portal API components and utilities
The newest version!
/*
* SemanticWebBuilder es una plataforma para el desarrollo de portales y aplicaciones de integración,
* colaboración y conocimiento, que gracias al uso de tecnología semántica puede generar contextos de
* información alrededor de algún tema de interés o bien integrar información y aplicaciones de diferentes
* fuentes, donde a la información se le asigna un significado, de forma que pueda ser interpretada y
* procesada por personas y/o sistemas, es una creación original del Fondo de Información y Documentación
* para la Industria INFOTEC, cuyo registro se encuentra actualmente en trámite.
*
* INFOTEC pone a su disposición la herramienta SemanticWebBuilder a través de su licenciamiento abierto al público (‘open source’),
* en virtud del cual, usted podrá usarlo en las mismas condiciones con que INFOTEC lo ha diseñado y puesto a su disposición;
* aprender de él; distribuirlo a terceros; acceder a su código fuente y modificarlo, y combinarlo o enlazarlo con otro software,
* todo ello de conformidad con los términos y condiciones de la LICENCIA ABIERTA AL PÚBLICO que otorga INFOTEC para la utilización
* del SemanticWebBuilder 4.0.
*
* INFOTEC no otorga garantía sobre SemanticWebBuilder, de ninguna especie y naturaleza, ni implícita ni explícita,
* siendo usted completamente responsable de la utilización que le dé y asumiendo la totalidad de los riesgos que puedan derivar
* de la misma.
*
* Si usted tiene cualquier duda o comentario sobre SemanticWebBuilder, INFOTEC pone a su disposición la siguiente
* dirección electrónica:
* http://www.semanticwebbuilder.org
*/
package org.semanticwb.portal.resources;
import org.semanticwb.model.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.semanticwb.Logger;
import org.semanticwb.SWBPlatform;
import org.semanticwb.SWBUtils;
import org.semanticwb.model.Collection;
import org.semanticwb.model.FormElement;
import org.semanticwb.model.FormValidateException;
import org.semanticwb.model.GenericObject;
import org.semanticwb.model.PropertyGroup;
import org.semanticwb.model.Resource;
import org.semanticwb.model.SWBComparator;
import org.semanticwb.model.SWBModel;
import org.semanticwb.model.Traceable;
import org.semanticwb.platform.SemanticClass;
import org.semanticwb.platform.SemanticModel;
import org.semanticwb.platform.SemanticObject;
import org.semanticwb.platform.SemanticOntology;
import org.semanticwb.platform.SemanticProperty;
import org.semanticwb.platform.SemanticVocabulary;
import org.semanticwb.portal.SWBFormButton;
import org.semanticwb.portal.SWBFormMgr;
import org.semanticwb.portal.api.GenericResource;
import org.semanticwb.portal.api.SWBActionResponse;
import org.semanticwb.portal.api.SWBParamRequest;
import org.semanticwb.portal.api.SWBResourceException;
import org.semanticwb.portal.api.SWBResourceURL;
/**
*
* @author juan.fernandez
*/
public class CollectionResource extends GenericResource {
/** The log. */
private Logger log = SWBUtils.getLogger(CollectionResource.class);
/** The MOD e_ form. */
private String MODE_FORM = "FORM";
/** The hm form ele. */
private HashMap hmFormEle = null;
private Resource base = null;
private static String COLL_ID = "collid";
@Override
public void doAdmin(HttpServletRequest request, HttpServletResponse response, SWBParamRequest paramRequest) throws SWBResourceException, IOException {
base = getResourceBase();
response.setContentType("text/html; charset=ISO-8859-1");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
PrintWriter out = response.getWriter();
User user = paramRequest.getUser();
String id = request.getParameter("suri");
String coluri = base.getAttribute(COLL_ID, "");
SWBResourceURL urlact = paramRequest.getActionUrl();
urlact.setAction("updcfg");
out.println("");
out.println("");
out.println("");
}
@Override
public void processAction(HttpServletRequest request, SWBActionResponse response) throws SWBResourceException, IOException {
String id = request.getParameter("suri");
String sval = request.getParameter("sval");
//String ract = request.getParameter("ract");
String action = response.getAction();
if (action == null) {
action = "";
}
if (action.equals("")) {
action = request.getParameter("act");
}
if (action == null) {
action = "";
}
//System.out.println("Accion:"+action);
SemanticOntology ont = SWBPlatform.getSemanticMgr().getOntology();
GenericObject obj = ont.getGenericObject(id);
if (action.equals("updcfg")) {
try {
String bhtype = request.getParameter(COLL_ID);
//System.out.println("Config resource:"+bhtype);
if (bhtype != null) {
base.setAttribute(COLL_ID, bhtype);
base.updateAttributesToDB();
}
} catch (Exception e) {
log.error(e);
}
response.setMode(SWBActionResponse.Mode_ADMIN);
}
else if ("new".equals(action)) {
log.debug("ProcessAction(new) ");
id = request.getParameter("suri");
String clsuri = request.getParameter("clsuri");
SemanticObject so = ont.getSemanticObject(id);
SemanticClass sclass = ont.getSemanticObject(clsuri).transformToSemanticClass();
log.debug("ProcessAction(new): sobj: " + id);
SWBFormMgr fmgr = new SWBFormMgr(sclass, so.getModel().getModelObject(), SWBFormMgr.MODE_CREATE);
try {
SemanticObject nso = fmgr.processForm(request);
if (clsuri != null) {
response.setRenderParameter("clsuri", clsuri);
}
response.setRenderParameter("nsuri", nso.getURI());
} catch (FormValidateException e) {
throw new SWBResourceException("Error to process form...", e);
}
//response.setRenderParameter("statmsg", response.getLocaleString("statmsg1"));
response.setMode(SWBActionResponse.Mode_VIEW);
}
else if ("updateform".equals(action)) {
log.debug("ProcessAction(new) ");
id = request.getParameter("suri");
String clsuri = request.getParameter("clsuri");
sval = request.getParameter("sval");
SemanticObject so = ont.getSemanticObject(sval);
//SemanticClass sclass = ont.getSemanticObject(clsuri).transformToSemanticClass();
log.debug("ProcessAction(updateform): sobj: " + sval);
SWBFormMgr fmgr = new SWBFormMgr(so, null, SWBFormMgr.MODE_EDIT);
try {
SemanticObject nso = fmgr.processForm(request);
if (clsuri != null) {
response.setRenderParameter("clsuri", clsuri);
}
response.setRenderParameter("nsuri", nso.getURI());
} catch (FormValidateException e) {
throw new SWBResourceException("Error to process form...", e);
}
//response.setRenderParameter("statmsg", response.getLocaleString("statmsg1"));
response.setMode(SWBActionResponse.Mode_VIEW);
} else if ("remove".equals(action))
{
log.debug("processAction(remove)");
//System.out.println("sval:"+sval);
if (obj instanceof Collection) {
Collection col = (Collection) obj;
String prop = request.getParameter("prop");
//System.out.println("Eliminando ... "+prop);
if(prop!=null&&prop.equals("display"))
{
//System.out.println("quitando display");
col.removeListProperties(sval);
} else if(prop!=null&&prop.equals("search"))
{
//System.out.println("quitando search");
col.removeSearchProperties(sval);
}
}
}
if(id!=null) response.setRenderParameter("suri", id);
if(sval!=null) response.setRenderParameter("sval", sval);
}
/* (non-Javadoc)
* @see org.semanticwb.portal.api.GenericResource#doEdit(HttpServletRequest, HttpServletResponse, SWBParamRequest)
*/
@Override
public void doView(HttpServletRequest request, HttpServletResponse response, SWBParamRequest paramsRequest) throws SWBResourceException, IOException {
base = getResourceBase();
response.setContentType("text/html; charset=ISO-8859-1");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
PrintWriter out = response.getWriter();
User user = paramsRequest.getUser();
String id = null;//request.getParameter("suri");
id = base.getAttribute(COLL_ID);
//System.out.println("Collection ID:"+id);
String page = request.getParameter("page");
String collectiontype = "display";
SemanticOntology ont = SWBPlatform.getSemanticMgr().getOntology();
GenericObject gobj = ont.getGenericObject(id);
if (hmFormEle == null) {
//System.out.println("Cargando HM - Form Element");
hmFormEle = new HashMap();
SemanticOntology sont = SWBPlatform.getSemanticMgr().getSchema();
SemanticVocabulary sv = SWBPlatform.getSemanticMgr().getVocabulary();
Iterator itfe = sv.getSemanticClass(sv.SWB_FORMELEMENT).listInstances(false);//sont.listInstancesOfClass(sv.getSemanticClass(sv.SWB_FORMELEMENT));
while (itfe.hasNext()) {
SemanticObject sofe = itfe.next();
hmFormEle.put(sofe.getURI(), sofe);
//System.out.println("formElement: "+sofe.getId());
}
}
String action = request.getParameter("act");
if (action == null) {
action = "";
}
Collection col = null;
if (collectiontype.equals("display")) { //("stpBusqueda".equals(action) || "".equals(action)) {
//System.out.println("ID: "+id);
String busqueda = request.getParameter("search");
if (null == busqueda) {
busqueda = "";
}
busqueda = busqueda.trim();
HashMap hmhasprop = new HashMap(); // uri property, uri form element asociado
HashMap hmhasproporder = new HashMap(); // indice filtro, PROPIEDAD
HashMap hmConfcol = new HashMap();
HashMap hmConfcolFE = new HashMap();
HashMap hmConfbus = new HashMap();
HashMap hmfiltro = new HashMap();
HashMap hmSearchParam = new HashMap();
if (gobj instanceof Collection) {
col = (Collection) gobj;
SemanticClass sccol = col.getCollectionClass().transformToSemanticClass();
//System.out.println("Coleccion: "+col.getTitle());
SWBModel colmodel = col.getCollectionModel();
if (colmodel == null) {
colmodel = col.getWebSite().getSubModel();
}
SemanticModel semmodel = colmodel.getSemanticObject().getModel();
SemanticProperty semProphm = null;
Iterator its = col.listListPropertieses();
while (its.hasNext()) {
String spropname = its.next();
StringTokenizer stoken = new StringTokenizer(spropname, "|");
//System.out.println("spropname: "+spropname);
if (stoken != null) {
String suri = stoken.nextToken();
String surife = stoken.nextToken();
String suriorder = stoken.nextToken();
semProphm = ont.getSemanticProperty(suri);
if (semProphm != null) {
hmConfcol.put(suriorder, semProphm);
hmConfcolFE.put(suriorder, surife);
}
} else {
semProphm = ont.getSemanticProperty(spropname);
if (semProphm != null) {
hmConfcol.put(spropname, semProphm);
}
}
}
ArrayList list = new ArrayList(hmConfcol.keySet());
Collections.sort(list);
semProphm = null;
its = col.listSearchPropertieses();
while (its.hasNext()) {
String spropname = its.next();
StringTokenizer stoken = new StringTokenizer(spropname, "|");
//System.out.println("spropname: "+spropname);
if (stoken != null) {
String suri = stoken.nextToken();
String surife = stoken.nextToken();
String suriorder = stoken.nextToken();
semProphm = ont.getSemanticProperty(suri);
//System.out.println("spropnameSearch: "+suri);
if (semProphm != null) {
hmConfbus.put(suri, semProphm);
}
if (semProphm.isObjectProperty()) {
hmhasprop.put(suri, surife);
hmhasproporder.put(suriorder, semProphm);
}
}
}
if (hmConfcol.size() > 0) {
//Armado de tabla
out.println(""); //class=\"swbform\"
out.println("");
SWBResourceURL urls = paramsRequest.getRenderUrl();
urls.setParameter("act", "stpBusqueda");
urls.setParameter("act2", "ssearch");
out.println("");
out.println(" ");
out.println("");
out.println("");
if (p > 0 || x < l) //Requiere paginacion
{
out.println("");
}
SWBResourceURL url = paramsRequest.getRenderUrl();
url.setParameter("act", "stpBusqueda");
url.setParameter("suri", id);
url.setParameter("clsuri", sccol.getURI());
url.setMode(MODE_FORM);
// SWBResourceURL urlback = paramsRequest.getRenderUrl();
// urlback.setParameter("act", "config");
// urlback.setParameter("suri", id);
out.println("");
out.println("");
} else {
out.println("");
out.println("");
SWBResourceURL urlreload = paramsRequest.getRenderUrl();
urlreload.setParameter("act", "config");
urlreload.setParameter("suri", id);
out.println("");
out.println("");
}
}
}
}
/**
* Gets the fE select.
*
* @param hmFE the hm fe
* @param FEsel the f esel
* @param user the user
* @return the fE select
*/
public String getFESelect(HashMap hmFE, String FEsel, User user) {
StringBuilder ret = new StringBuilder();
ret.append("");
Iterator itfe = hmFE.values().iterator();
while (itfe.hasNext()) {
SemanticObject sofe = itfe.next();
ret.append("");
}
return ret.toString();
}
/**
* Review sem prop.
*
* @param prop the prop
* @param obj the obj
* @param paramsRequest the params request
* @return the string
*/
public String reviewSemProp(SemanticProperty prop, SemanticObject obj, SWBParamRequest paramsRequest) {
String ret = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss", new Locale(paramsRequest.getUser().getLanguage()));
if (prop.isDataTypeProperty()) {
if (prop.isBoolean()) {
boolean bvalue = obj.getBooleanProperty(prop);
if (bvalue) {
ret = paramsRequest.getLocaleString("booleanYes");
} else {
ret = paramsRequest.getLocaleString("booleanNo");
}
}
if (prop.isInt() || prop.isDouble() || prop.isLong()) {
ret = Long.toString(obj.getLongProperty(prop));
}
if (prop.isString()) {
ret = obj.getProperty(prop);
}
if (prop.isFloat()) {
ret = Float.toString(obj.getFloatProperty(prop));
}
if (prop.isDate() || prop.isDateTime()) {
ret = sdf.format(obj.getDateTimeProperty(prop));
}
} else if (prop.isObjectProperty()) {
SemanticObject so = obj.getObjectProperty(prop);
if (null != so) {
ret = so.getDisplayName(paramsRequest.getUser().getLanguage());
}
}
if (null == ret || (ret != null && ret.trim().equals("null"))) {
ret = "";
}
} catch (Exception e) {
}
if (ret == null || (ret != null && ret.trim().equals("null"))) {
ret = "";
}
return ret;
}
/**
* Do form.
*
* @param request the request
* @param response the response
* @param paramRequest the param request
* @throws SWBResourceException the sWB resource exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public void doForm(HttpServletRequest request, HttpServletResponse response, SWBParamRequest paramRequest) throws SWBResourceException, IOException {
response.setContentType("text/html; charset=ISO-8859-1");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
//System.out.println("doForm...");
PrintWriter out = response.getWriter();
User user = paramRequest.getUser();
String id = request.getParameter("suri");
String cid = request.getParameter("clsuri");
String act = request.getParameter("act");
String sval = request.getParameter("sval");
SemanticOntology ont = SWBPlatform.getSemanticMgr().getOntology();
SemanticObject obj = ont.getSemanticObject(sval);
GenericObject go = ont.getGenericObject(sval);
Collection col = null;
if(go instanceof Collection)
col = (Collection)go;
SemanticClass sclass = null;
if(col!=null) sclass = col.getCollectionClass().transformToSemanticClass();
else sclass = ont.getSemanticObject(cid).transformToSemanticClass();
SWBResourceURL urlPA = paramRequest.getActionUrl();
String modeform = null;
if(act!=null&&act.equals("new"))
{
modeform = SWBFormMgr.MODE_CREATE;
urlPA.setAction("new");
}
else if(act!=null&&act.equals("edit"))
{
modeform = SWBFormMgr.MODE_EDIT;
urlPA.setAction("updateform");
}
//System.out.println("Modo: "+modeform);
SWBFormMgr fmgr = new SWBFormMgr(obj,null,modeform);
fmgr.setLang(user.getLanguage());
fmgr.setAction(urlPA.toString());
fmgr.setSubmitByAjax(false);
fmgr.addButton(SWBFormButton.newSaveButton());
SWBResourceURL urlback = paramRequest.getRenderUrl();
urlback.setMode(SWBResourceURL.Mode_VIEW);
urlback.setParameter("suri", id);
if (null != cid) {
urlback.setParameter("clsuri", cid);
}
if (null != act) {
urlback.setParameter("act", act);
}
fmgr.addButton(SWBFormButton.newCancelButton().setAttribute("onclick", "window.location='" + urlback + "';"));
fmgr.setType(SWBFormMgr.TYPE_DOJO);
log.debug("new: suri: " + id);
log.debug("new: clsuri: " + cid);
log.debug("new: act: " + act);
fmgr.addHiddenParameter("suri", id);
fmgr.addHiddenParameter("clsuri", cid);
fmgr.addHiddenParameter("ract", act);
fmgr.addHiddenParameter("act", act!=null&&act.equals("new")?"new":"updateform");
fmgr.addHiddenParameter("sval", sval);
out.println(fmgr.renderForm(request));
// Iterator it = fmgr.getProperties().iterator();
// while (it.hasNext()) {
// SemanticProperty semProp = it.next();
// if(semProp.isDataTypeProperty())
// {
// out.println(fmgr.getFormElement(semProp).renderElement(request, obj, semProp, act, modeform, null));
// out.println("
");
// out.println(fmgr.renderLabel(request, semProp, modeform)+": "+fmgr.renderElement(request, semProp.getName()));
// out.println("
");
// }
// else if(semProp.isObjectProperty())
// {
// //
//
// }
//
// }
// HashMap hmpg = fmgr.getGroups();
// out.println("");
// Iterator itpg = hmpg.keySet().iterator();
// while (itpg.hasNext()) {
// PropertyGroup pGroup = itpg.next();
// //out.println("groupid:"+pGroup.getId());
// //out.println("
");
// //out.println("indice"+pGroup.getIndex());
// //out.println("
");
//
// //out.println("label:"+pGroup.getSemanticObject().getDisplayName(user.getLanguage()));
// //out.println("
");
// out.println("");
// }
// out.println(" ");
}
/**
* Process the mode request by the session user.
*
* @param request , this holds the parameters
* @param response , an answer to the user request
* @param paramRequest , a list of objects like user, webpage, Resource, ...
* @throws SWBResourceException, a Resource Exception
* @throws IOException, an In Out Exception
* @throws SWBResourceException the sWB resource exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@Override
public void processRequest(HttpServletRequest request, HttpServletResponse response, SWBParamRequest paramRequest) throws SWBResourceException, IOException {
if (paramRequest.getMode().equals(MODE_FORM)) {
doForm(request, response, paramRequest);
} else {
super.processRequest(request, response, paramRequest);
}
}
}