0001: /* Copyright 2001, 2002 The JA-SIG Collaborative. All rights reserved.
0002: * See license distributed with this file and
0003: * available online at http://www.uportal.org/license.html
0004: */
0005:
0006: package org.jasig.portal.layout.simple;
0007:
0008: import java.sql.Connection;
0009: import java.sql.PreparedStatement;
0010: import java.sql.ResultSet;
0011: import java.sql.SQLException;
0012: import java.sql.Statement;
0013: import java.util.Enumeration;
0014: import java.util.HashMap;
0015: import java.util.HashSet;
0016: import java.util.Hashtable;
0017: import java.util.Iterator;
0018: import java.util.Map;
0019: import java.util.Vector;
0020:
0021: import javax.xml.transform.Transformer;
0022: import javax.xml.transform.TransformerFactory;
0023: import javax.xml.transform.dom.DOMResult;
0024: import javax.xml.transform.sax.SAXSource;
0025:
0026: import org.apache.commons.logging.Log;
0027: import org.apache.commons.logging.LogFactory;
0028: import org.jasig.portal.ChannelDefinition;
0029: import org.jasig.portal.ChannelRegistryStoreFactory;
0030: import org.jasig.portal.CoreStylesheetDescription;
0031: import org.jasig.portal.CoreXSLTStylesheetDescription;
0032: import org.jasig.portal.EntityIdentifier;
0033: import org.jasig.portal.IChannelRegistryStore;
0034: import org.jasig.portal.PortalException;
0035: import org.jasig.portal.RDBMServices;
0036: import org.jasig.portal.StructureStylesheetDescription;
0037: import org.jasig.portal.StructureStylesheetUserPreferences;
0038: import org.jasig.portal.ThemeStylesheetDescription;
0039: import org.jasig.portal.ThemeStylesheetUserPreferences;
0040: import org.jasig.portal.UserPreferences;
0041: import org.jasig.portal.UserProfile;
0042: import org.jasig.portal.channels.error.ErrorCode;
0043: import org.jasig.portal.i18n.LocaleManager;
0044: import org.jasig.portal.layout.IUserLayoutStore;
0045: import org.jasig.portal.layout.LayoutStructure;
0046: import org.jasig.portal.layout.StructureParameter;
0047: import org.jasig.portal.rdbm.DatabaseMetaDataImpl;
0048: import org.jasig.portal.security.IPerson;
0049: import org.jasig.portal.security.ISecurityContext;
0050: import org.jasig.portal.utils.CounterStoreFactory;
0051: import org.jasig.portal.utils.DocumentFactory;
0052: import org.jasig.portal.utils.ICounterStore;
0053:
0054: import org.jasig.portal.utils.ResourceLoader;
0055: import org.w3c.dom.Document;
0056: import org.w3c.dom.Element;
0057: import org.w3c.dom.NamedNodeMap;
0058: import org.w3c.dom.Node;
0059: import org.w3c.dom.NodeList;
0060: import org.xml.sax.InputSource;
0061:
0062: /**
0063: * SQL implementation for the 2.x relational database model.
0064: *
0065: * Prior to uPortal 2.5, this class existed in the org.jasig.portal package. It was
0066: * moved to its present package to express that it is part of the
0067: * Simple Layout Manager implementation.
0068: *
0069: * @author George Lindholm
0070: * @version $Revision: 36679 $ $Date: 2006-08-22 14:29:30 -0700 (Tue, 22 Aug 2006) $
0071: */
0072: public class RDBMUserLayoutStore implements IUserLayoutStore {
0073:
0074: protected final Log log = LogFactory.getLog(getClass());
0075:
0076: //This class is instantiated ONCE so NO class variables can be used to keep state between calls
0077: protected static int DEBUG = 0;
0078: protected static final String channelPrefix = "n";
0079: protected static final String folderPrefix = "s";
0080: protected IChannelRegistryStore crs;
0081: protected ICounterStore csdb;
0082: // I18n property
0083: protected static final boolean localeAware = LocaleManager
0084: .isLocaleAware();
0085:
0086: public RDBMUserLayoutStore() throws Exception {
0087: crs = ChannelRegistryStoreFactory.getChannelRegistryStoreImpl();
0088: csdb = CounterStoreFactory.getCounterStoreImpl();
0089: if (RDBMServices.getDbMetaData().supportsOuterJoins()) {
0090: if (RDBMServices.getDbMetaData().getJoinQuery() instanceof DatabaseMetaDataImpl.JdbcDb) {
0091: RDBMServices
0092: .getDbMetaData()
0093: .getJoinQuery()
0094: .addQuery(
0095: "layout",
0096: "{oj UP_LAYOUT_STRUCT ULS LEFT OUTER JOIN UP_LAYOUT_PARAM USP ON ULS.USER_ID = USP.USER_ID AND ULS.STRUCT_ID = USP.STRUCT_ID} WHERE");
0097: RDBMServices
0098: .getDbMetaData()
0099: .getJoinQuery()
0100: .addQuery(
0101: "ss_struct",
0102: "{oj UP_SS_STRUCT USS LEFT OUTER JOIN UP_SS_STRUCT_PAR USP ON USS.SS_ID=USP.SS_ID} WHERE");
0103: RDBMServices
0104: .getDbMetaData()
0105: .getJoinQuery()
0106: .addQuery(
0107: "ss_theme",
0108: "{oj UP_SS_THEME UTS LEFT OUTER JOIN UP_SS_THEME_PARM UTP ON UTS.SS_ID=UTP.SS_ID} WHERE");
0109: } else if (RDBMServices.getDbMetaData().getJoinQuery() instanceof DatabaseMetaDataImpl.PostgreSQLDb) {
0110: RDBMServices
0111: .getDbMetaData()
0112: .getJoinQuery()
0113: .addQuery(
0114: "layout",
0115: "UP_LAYOUT_STRUCT ULS LEFT OUTER JOIN UP_LAYOUT_PARAM USP ON ULS.USER_ID = USP.USER_ID AND ULS.STRUCT_ID = USP.STRUCT_ID WHERE");
0116: RDBMServices
0117: .getDbMetaData()
0118: .getJoinQuery()
0119: .addQuery(
0120: "ss_struct",
0121: "UP_SS_STRUCT USS LEFT OUTER JOIN UP_SS_STRUCT_PAR USP ON USS.SS_ID=USP.SS_ID WHERE");
0122: RDBMServices
0123: .getDbMetaData()
0124: .getJoinQuery()
0125: .addQuery(
0126: "ss_theme",
0127: "UP_SS_THEME UTS LEFT OUTER JOIN UP_SS_THEME_PARM UTP ON UTS.SS_ID=UTP.SS_ID WHERE");
0128: } else if (RDBMServices.getDbMetaData().getJoinQuery() instanceof DatabaseMetaDataImpl.OracleDb) {
0129: RDBMServices
0130: .getDbMetaData()
0131: .getJoinQuery()
0132: .addQuery(
0133: "layout",
0134: "UP_LAYOUT_STRUCT ULS, UP_LAYOUT_PARAM USP WHERE ULS.STRUCT_ID = USP.STRUCT_ID(+) AND ULS.USER_ID = USP.USER_ID(+) AND");
0135: RDBMServices
0136: .getDbMetaData()
0137: .getJoinQuery()
0138: .addQuery("ss_struct",
0139: "UP_SS_STRUCT USS, UP_SS_STRUCT_PAR USP WHERE USS.SS_ID=USP.SS_ID(+) AND");
0140: RDBMServices
0141: .getDbMetaData()
0142: .getJoinQuery()
0143: .addQuery("ss_theme",
0144: "UP_SS_THEME UTS, UP_SS_THEME_PARM UTP WHERE UTS.SS_ID=UTP.SS_ID(+) AND");
0145: } else {
0146: throw new Exception("Unknown database driver");
0147: }
0148: }
0149: }
0150:
0151: /**
0152: * Registers a NEW structure stylesheet with the database.
0153: * @param ssd the Stylesheet description object
0154: * @return an <code>Integer</code> id for the registered Stylesheet description object
0155: */
0156: public Integer addStructureStylesheetDescription(
0157: StructureStylesheetDescription ssd) throws Exception {
0158: Connection con = RDBMServices.getConnection();
0159: try {
0160: // Set autocommit false for the connection
0161: RDBMServices.setAutoCommit(con, false);
0162: Statement stmt = con.createStatement();
0163: try {
0164: // we assume that this is a new stylesheet.
0165: int id = csdb.getIncrementIntegerId("UP_SS_STRUCT");
0166: ssd.setId(id);
0167: String sQuery = "INSERT INTO UP_SS_STRUCT (SS_ID,SS_NAME,SS_URI,SS_DESCRIPTION_URI,SS_DESCRIPTION_TEXT) VALUES ("
0168: + id
0169: + ",'"
0170: + ssd.getStylesheetName()
0171: + "','"
0172: + ssd.getStylesheetURI()
0173: + "','"
0174: + ssd.getStylesheetDescriptionURI()
0175: + "','"
0176: + ssd.getStylesheetWordDescription() + "')";
0177: if (log.isDebugEnabled())
0178: log
0179: .debug("RDBMUserLayoutStore::addStructureStylesheetDescription(): "
0180: + sQuery);
0181: stmt.executeUpdate(sQuery);
0182: // insert all stylesheet params
0183: for (Enumeration e = ssd.getStylesheetParameterNames(); e
0184: .hasMoreElements();) {
0185: String pName = (String) e.nextElement();
0186: sQuery = "INSERT INTO UP_SS_STRUCT_PAR (SS_ID,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE) VALUES ("
0187: + id
0188: + ",'"
0189: + pName
0190: + "','"
0191: + ssd
0192: .getStylesheetParameterDefaultValue(pName)
0193: + "','"
0194: + ssd
0195: .getStylesheetParameterWordDescription(pName)
0196: + "',1)";
0197: if (log.isDebugEnabled())
0198: log
0199: .debug("RDBMUserLayoutStore::addStructureStylesheetDescription(): "
0200: + sQuery);
0201: stmt.executeUpdate(sQuery);
0202: }
0203: // insert all folder attributes
0204: for (Enumeration e = ssd.getFolderAttributeNames(); e
0205: .hasMoreElements();) {
0206: String pName = (String) e.nextElement();
0207: sQuery = "INSERT INTO UP_SS_STRUCT_PAR (SS_ID,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE) VALUES ("
0208: + id
0209: + ",'"
0210: + pName
0211: + "','"
0212: + ssd.getFolderAttributeDefaultValue(pName)
0213: + "','"
0214: + ssd
0215: .getFolderAttributeWordDescription(pName)
0216: + "',2)";
0217: if (log.isDebugEnabled())
0218: log
0219: .debug("RDBMUserLayoutStore::addStructureStylesheetDescription(): "
0220: + sQuery);
0221: stmt.executeUpdate(sQuery);
0222: }
0223: // insert all channel attributes
0224: for (Enumeration e = ssd.getChannelAttributeNames(); e
0225: .hasMoreElements();) {
0226: String pName = (String) e.nextElement();
0227: sQuery = "INSERT INTO UP_SS_STRUCT_PAR (SS_ID,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE) VALUES ("
0228: + id
0229: + ",'"
0230: + pName
0231: + "','"
0232: + ssd
0233: .getChannelAttributeDefaultValue(pName)
0234: + "','"
0235: + ssd
0236: .getChannelAttributeWordDescription(pName)
0237: + "',3)";
0238: if (log.isDebugEnabled())
0239: log
0240: .debug("RDBMUserLayoutStore::addStructureStylesheetDescription(): "
0241: + sQuery);
0242: stmt.executeUpdate(sQuery);
0243: }
0244: // Commit the transaction
0245: RDBMServices.commit(con);
0246: return new Integer(id);
0247: } catch (Exception e) {
0248: // Roll back the transaction
0249: RDBMServices.rollback(con);
0250: throw e;
0251: } finally {
0252: stmt.close();
0253: }
0254: } finally {
0255: RDBMServices.releaseConnection(con);
0256: }
0257: }
0258:
0259: /**
0260: * Registers a NEW theme stylesheet with the database.
0261: * @param tsd Stylesheet description object
0262: * @return an <code>Integer</code> id of the registered Theme Stylesheet if successful;
0263: * <code>null</code> otherwise.
0264: */
0265: public Integer addThemeStylesheetDescription(
0266: ThemeStylesheetDescription tsd) throws Exception {
0267: Connection con = RDBMServices.getConnection();
0268: try {
0269: // Set autocommit false for the connection
0270: RDBMServices.setAutoCommit(con, false);
0271: Statement stmt = con.createStatement();
0272: try {
0273: // we assume that this is a new stylesheet.
0274: int id = csdb.getIncrementIntegerId("UP_SS_THEME");
0275: tsd.setId(id);
0276: String sQuery = "INSERT INTO UP_SS_THEME (SS_ID,SS_NAME,SS_URI,SS_DESCRIPTION_URI,SS_DESCRIPTION_TEXT,STRUCT_SS_ID,SAMPLE_URI,SAMPLE_ICON_URI,MIME_TYPE,DEVICE_TYPE,SERIALIZER_NAME,UP_MODULE_CLASS) VALUES ("
0277: + id
0278: + ",'"
0279: + tsd.getStylesheetName()
0280: + "','"
0281: + tsd.getStylesheetURI()
0282: + "','"
0283: + tsd.getStylesheetDescriptionURI()
0284: + "','"
0285: + tsd.getStylesheetWordDescription()
0286: + "',"
0287: + tsd.getStructureStylesheetId()
0288: + ",'"
0289: + tsd.getSamplePictureURI()
0290: + "','"
0291: + tsd.getSampleIconURI()
0292: + "','"
0293: + tsd.getMimeType()
0294: + "','"
0295: + tsd.getDeviceType()
0296: + "','"
0297: + tsd.getSerializerName()
0298: + "','"
0299: + tsd.getCustomUserPreferencesManagerClass()
0300: + "')";
0301: if (log.isDebugEnabled())
0302: log
0303: .debug("RDBMUserLayoutStore::addThemeStylesheetDescription(): "
0304: + sQuery);
0305: stmt.executeUpdate(sQuery);
0306: // insert all stylesheet params
0307: for (Enumeration e = tsd.getStylesheetParameterNames(); e
0308: .hasMoreElements();) {
0309: String pName = (String) e.nextElement();
0310: sQuery = "INSERT INTO UP_SS_THEME_PARM (SS_ID,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE) VALUES ("
0311: + id
0312: + ",'"
0313: + pName
0314: + "','"
0315: + tsd
0316: .getStylesheetParameterDefaultValue(pName)
0317: + "','"
0318: + tsd
0319: .getStylesheetParameterWordDescription(pName)
0320: + "',1)";
0321: if (log.isDebugEnabled())
0322: log
0323: .debug("RDBMUserLayoutStore::addThemeStylesheetDescription(): "
0324: + sQuery);
0325: stmt.executeUpdate(sQuery);
0326: }
0327: // insert all channel attributes
0328: for (Enumeration e = tsd.getChannelAttributeNames(); e
0329: .hasMoreElements();) {
0330: String pName = (String) e.nextElement();
0331: sQuery = "INSERT INTO UP_SS_THEME_PARM (SS_ID,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE) VALUES ("
0332: + id
0333: + ",'"
0334: + pName
0335: + "','"
0336: + tsd
0337: .getChannelAttributeDefaultValue(pName)
0338: + "','"
0339: + tsd
0340: .getChannelAttributeWordDescription(pName)
0341: + "',3)";
0342: if (log.isDebugEnabled())
0343: log
0344: .debug("RDBMUserLayoutStore::addThemeStylesheetDescription(): "
0345: + sQuery);
0346: stmt.executeUpdate(sQuery);
0347: }
0348: // Commit the transaction
0349: RDBMServices.commit(con);
0350: return new Integer(id);
0351: } catch (Exception e) {
0352: // Roll back the transaction
0353: RDBMServices.rollback(con);
0354: throw e;
0355: } finally {
0356: stmt.close();
0357: }
0358: } finally {
0359: RDBMServices.releaseConnection(con);
0360: }
0361: }
0362:
0363: /**
0364: * Update the theme stylesheet description.
0365: * @param stylesheetDescriptionURI
0366: * @param stylesheetURI
0367: * @param stylesheetId
0368: * @return true if update succeeded, otherwise false
0369: */
0370: public boolean updateThemeStylesheetDescription(
0371: String stylesheetDescriptionURI, String stylesheetURI,
0372: int stylesheetId) {
0373: try {
0374: Document stylesheetDescriptionXML = getDOM(stylesheetDescriptionURI);
0375: String ssName = this .getRootElementTextValue(
0376: stylesheetDescriptionXML,
0377: "parentStructureStylesheet");
0378: // should thrown an exception
0379: if (ssName == null)
0380: return false;
0381: // determine id of the parent structure stylesheet
0382: Integer ssId = getStructureStylesheetId(ssName);
0383: // stylesheet not found, should thrown an exception here
0384: if (ssId == null)
0385: return false;
0386: ThemeStylesheetDescription sssd = new ThemeStylesheetDescription();
0387: sssd.setId(stylesheetId);
0388: sssd.setStructureStylesheetId(ssId.intValue());
0389: String xmlStylesheetName = this
0390: .getName(stylesheetDescriptionXML);
0391: String xmlStylesheetDescriptionText = this
0392: .getDescription(stylesheetDescriptionXML);
0393: sssd.setStylesheetName(xmlStylesheetName);
0394: sssd.setStylesheetURI(stylesheetURI);
0395: sssd.setStylesheetDescriptionURI(stylesheetDescriptionURI);
0396: sssd
0397: .setStylesheetWordDescription(xmlStylesheetDescriptionText);
0398: sssd.setMimeType(this .getRootElementTextValue(
0399: stylesheetDescriptionXML, "mimeType"));
0400: if (log.isDebugEnabled())
0401: log
0402: .debug("RDBMUserLayoutStore::updateThemeStylesheetDescription() : setting mimetype=\""
0403: + sssd.getMimeType() + "\"");
0404: sssd.setSerializerName(this .getRootElementTextValue(
0405: stylesheetDescriptionXML, "serializer"));
0406: if (log.isDebugEnabled())
0407: log
0408: .debug("RDBMUserLayoutStore::updateThemeStylesheetDescription() : setting serializerName=\""
0409: + sssd.getSerializerName() + "\"");
0410: sssd.setCustomUserPreferencesManagerClass(this
0411: .getRootElementTextValue(stylesheetDescriptionXML,
0412: "userPreferencesModuleClass"));
0413: sssd.setSamplePictureURI(this .getRootElementTextValue(
0414: stylesheetDescriptionXML, "samplePictureURI"));
0415: sssd.setSampleIconURI(this .getRootElementTextValue(
0416: stylesheetDescriptionXML, "sampleIconURI"));
0417: sssd.setDeviceType(this .getRootElementTextValue(
0418: stylesheetDescriptionXML, "deviceType"));
0419: // populate parameter and attriute tables
0420: this .populateParameterTable(stylesheetDescriptionXML, sssd);
0421: this .populateChannelAttributeTable(
0422: stylesheetDescriptionXML, sssd);
0423: updateThemeStylesheetDescription(sssd);
0424: } catch (Exception e) {
0425: if (log.isDebugEnabled())
0426: log.debug(
0427: "Exception updating theme stylesheet description="
0428: + "[" + stylesheetDescriptionURI
0429: + "] stylesheetUri=[" + stylesheetURI
0430: + "] stylesheetId=[" + stylesheetId
0431: + "]", e);
0432: return false;
0433: }
0434: return true;
0435: }
0436:
0437: /**
0438: * Update the structure stylesheet description
0439: * @param stylesheetDescriptionURI
0440: * @param stylesheetURI
0441: * @param stylesheetId
0442: * @return true if update succeeded, otherwise false
0443: */
0444: public boolean updateStructureStylesheetDescription(
0445: String stylesheetDescriptionURI, String stylesheetURI,
0446: int stylesheetId) {
0447: try {
0448: Document stylesheetDescriptionXML = getDOM(stylesheetDescriptionURI);
0449: StructureStylesheetDescription fssd = new StructureStylesheetDescription();
0450: String xmlStylesheetName = this
0451: .getName(stylesheetDescriptionXML);
0452: String xmlStylesheetDescriptionText = this
0453: .getDescription(stylesheetDescriptionXML);
0454: fssd.setId(stylesheetId);
0455: fssd.setStylesheetName(xmlStylesheetName);
0456: fssd.setStylesheetURI(stylesheetURI);
0457: fssd.setStylesheetDescriptionURI(stylesheetDescriptionURI);
0458: fssd
0459: .setStylesheetWordDescription(xmlStylesheetDescriptionText);
0460:
0461: // populate parameter and attriute tables
0462: this .populateParameterTable(stylesheetDescriptionXML, fssd);
0463: this .populateFolderAttributeTable(stylesheetDescriptionXML,
0464: fssd);
0465: this .populateChannelAttributeTable(
0466: stylesheetDescriptionXML, fssd);
0467:
0468: // now write out the database record
0469: updateStructureStylesheetDescription(fssd);
0470:
0471: } catch (Exception e) {
0472: if (log.isDebugEnabled())
0473: log.debug(
0474: "Exception updating structure stylesheet description "
0475: + "stylesheetDescriptionUri=["
0476: + stylesheetDescriptionURI + "]"
0477: + " stylesheetUri=[" + stylesheetURI
0478: + "] stylesheetId=" + stylesheetId, e);
0479: return false;
0480: }
0481: return true;
0482: }
0483:
0484: /**
0485: * Add a structure stylesheet description
0486: * @param stylesheetDescriptionURI
0487: * @param stylesheetURI
0488: * @return an <code>Integer</code> id of the registered Structure Stylesheet description object if successful;
0489: * <code>null</code> otherwise.
0490: */
0491: public Integer addStructureStylesheetDescription(
0492: String stylesheetDescriptionURI, String stylesheetURI) {
0493: // need to read in the description file to obtain information such as name, word description and media list
0494: try {
0495: Document stylesheetDescriptionXML = getDOM(stylesheetDescriptionURI);
0496: StructureStylesheetDescription fssd = new StructureStylesheetDescription();
0497: String xmlStylesheetName = this
0498: .getName(stylesheetDescriptionXML);
0499: String xmlStylesheetDescriptionText = this
0500: .getDescription(stylesheetDescriptionXML);
0501: fssd.setStylesheetName(xmlStylesheetName);
0502: fssd.setStylesheetURI(stylesheetURI);
0503: fssd.setStylesheetDescriptionURI(stylesheetDescriptionURI);
0504: fssd
0505: .setStylesheetWordDescription(xmlStylesheetDescriptionText);
0506:
0507: // populate parameter and attriute tables
0508: this .populateParameterTable(stylesheetDescriptionXML, fssd);
0509: this .populateFolderAttributeTable(stylesheetDescriptionXML,
0510: fssd);
0511: this .populateChannelAttributeTable(
0512: stylesheetDescriptionXML, fssd);
0513:
0514: // now write out the database record
0515: // first the basic record
0516: //UserLayoutStoreFactory.getUserLayoutStoreImpl().addStructureStylesheetDescription(xmlStylesheetName, stylesheetURI, stylesheetDescriptionURI, xmlStylesheetDescriptionText);
0517: return addStructureStylesheetDescription(fssd);
0518:
0519: } catch (Exception e) {
0520: if (log.isDebugEnabled())
0521: log.debug("Error adding stylesheet: "
0522: + "description Uri=["
0523: + stylesheetDescriptionURI + "] "
0524: + "stylesheetUri=[" + stylesheetURI + "]", e);
0525: }
0526: return null;
0527: }
0528:
0529: /**
0530: * Add theme stylesheet description
0531: * @param stylesheetDescriptionURI
0532: * @param stylesheetURI
0533: * @return an <code>Integer</code> id of the registered Theme Stylesheet if successful;
0534: * <code>null</code> otherwise.
0535: */
0536: public Integer addThemeStylesheetDescription(
0537: String stylesheetDescriptionURI, String stylesheetURI) {
0538: // need to read iN the description file to obtain information such as name, word description and mime type list
0539: try {
0540: Document stylesheetDescriptionXML = getDOM(stylesheetDescriptionURI);
0541: if (log.isDebugEnabled()) {
0542: log
0543: .debug("RDBMUserLayoutStore::addThemeStylesheetDescription() : stylesheet name = "
0544: + this
0545: .getName(stylesheetDescriptionXML));
0546: log
0547: .debug("RDBMUserLayoutStore::addThemeStylesheetDescription() : stylesheet description = "
0548: + this
0549: .getDescription(stylesheetDescriptionXML));
0550: }
0551: String ssName = this .getRootElementTextValue(
0552: stylesheetDescriptionXML,
0553: "parentStructureStylesheet");
0554: // should thrown an exception
0555: if (ssName == null)
0556: return null;
0557: // determine id of the parent structure stylesheet
0558: Integer ssId = getStructureStylesheetId(ssName);
0559: // stylesheet not found, should thrown an exception here
0560: if (ssId == null)
0561: return null;
0562: ThemeStylesheetDescription sssd = new ThemeStylesheetDescription();
0563: sssd.setStructureStylesheetId(ssId.intValue());
0564: String xmlStylesheetName = this
0565: .getName(stylesheetDescriptionXML);
0566: String xmlStylesheetDescriptionText = this
0567: .getDescription(stylesheetDescriptionXML);
0568: sssd.setStylesheetName(xmlStylesheetName);
0569: sssd.setStylesheetURI(stylesheetURI);
0570: sssd.setStylesheetDescriptionURI(stylesheetDescriptionURI);
0571: sssd
0572: .setStylesheetWordDescription(xmlStylesheetDescriptionText);
0573: sssd.setMimeType(this .getRootElementTextValue(
0574: stylesheetDescriptionXML, "mimeType"));
0575: if (log.isDebugEnabled())
0576: log
0577: .debug("RDBMUserLayoutStore::addThemeStylesheetDescription() : setting mimetype=\""
0578: + sssd.getMimeType() + "\"");
0579: sssd.setSerializerName(this .getRootElementTextValue(
0580: stylesheetDescriptionXML, "serializer"));
0581: if (log.isDebugEnabled())
0582: log
0583: .debug("RDBMUserLayoutStore::addThemeStylesheetDescription() : setting serializerName=\""
0584: + sssd.getSerializerName() + "\"");
0585: sssd.setCustomUserPreferencesManagerClass(this
0586: .getRootElementTextValue(stylesheetDescriptionXML,
0587: "userPreferencesModuleClass"));
0588: sssd.setSamplePictureURI(this .getRootElementTextValue(
0589: stylesheetDescriptionXML, "samplePictureURI"));
0590: sssd.setSampleIconURI(this .getRootElementTextValue(
0591: stylesheetDescriptionXML, "sampleIconURI"));
0592: sssd.setDeviceType(this .getRootElementTextValue(
0593: stylesheetDescriptionXML, "deviceType"));
0594:
0595: // populate parameter and attriute tables
0596: this .populateParameterTable(stylesheetDescriptionXML, sssd);
0597: this .populateChannelAttributeTable(
0598: stylesheetDescriptionXML, sssd);
0599:
0600: return addThemeStylesheetDescription(sssd);
0601:
0602: } catch (Exception e) {
0603: if (log.isDebugEnabled())
0604: log.debug(
0605: "Exception adding theme stylesheet description "
0606: + "description uri=["
0607: + stylesheetDescriptionURI + "] "
0608: + "stylesheet uri=[" + stylesheetURI
0609: + "]", e);
0610: }
0611: return null;
0612: }
0613:
0614: /**
0615: * Add a user profile
0616: * @param person
0617: * @param profile
0618: * @return userProfile
0619: * @exception Exception
0620: */
0621: public UserProfile addUserProfile(IPerson person,
0622: UserProfile profile) throws Exception {
0623: int userId = person.getID();
0624: // generate an id for this profile
0625: Connection con = RDBMServices.getConnection();
0626: try {
0627: int id = csdb.getIncrementIntegerId("UP_USER_PROFILE");
0628: profile.setProfileId(id);
0629: Statement stmt = con.createStatement();
0630: try {
0631: String sQuery = "INSERT INTO UP_USER_PROFILE (USER_ID,PROFILE_ID,PROFILE_NAME,STRUCTURE_SS_ID,THEME_SS_ID,DESCRIPTION, LAYOUT_ID) VALUES ("
0632: + userId
0633: + ","
0634: + profile.getProfileId()
0635: + ",'"
0636: + profile.getProfileName()
0637: + "',"
0638: + profile.getStructureStylesheetId()
0639: + ","
0640: + profile.getThemeStylesheetId()
0641: + ",'"
0642: + profile.getProfileDescription()
0643: + "', "
0644: + profile.getLayoutId() + ")";
0645: if (log.isDebugEnabled())
0646: log.debug("RDBMUserLayoutStore::addUserProfile(): "
0647: + sQuery);
0648: stmt.executeUpdate(sQuery);
0649: } finally {
0650: stmt.close();
0651: }
0652: } finally {
0653: RDBMServices.releaseConnection(con);
0654: }
0655: return profile;
0656: }
0657:
0658: /**
0659: * Checks if a channel has been approved
0660: * @param approvedDate
0661: * @return boolean Channel is approved
0662: */
0663: protected static boolean channelApproved(java.util.Date approvedDate) {
0664: java.util.Date rightNow = new java.util.Date();
0665: return (approvedDate != null && rightNow.after(approvedDate));
0666: }
0667:
0668: /**
0669: * Create a layout
0670: * @param layoutStructure
0671: * @param doc
0672: * @param root
0673: * @param structId
0674: * @exception java.sql.SQLException
0675: */
0676: protected final void createLayout(HashMap layoutStructure,
0677: Document doc, Element root, int structId)
0678: throws java.sql.SQLException, Exception {
0679: while (structId != 0) {
0680: if (DEBUG > 1) {
0681: System.err.println("CreateLayout(" + structId + ")");
0682: }
0683: LayoutStructure ls = (LayoutStructure) layoutStructure
0684: .get(new Integer(structId));
0685: // replaced with call to method in containing class to allow overriding
0686: // by subclasses of RDBMUserLayoutStore.
0687: // Element structure = ls.getStructureDocument(doc);
0688: Element structure = getStructure(doc, ls);
0689: root.appendChild(structure);
0690:
0691: String id = structure.getAttribute("ID");
0692: if (id != null && !id.equals("")) {
0693: structure.setIdAttribute("ID", true);
0694: }
0695:
0696: if (!ls.isChannel()) { // Folder
0697: createLayout(layoutStructure, doc, structure, ls
0698: .getChildId());
0699: }
0700: structId = ls.getNextId();
0701: }
0702: }
0703:
0704: /**
0705: * convert true/false into Y/N for database
0706: * @param value to check
0707: * @result boolean
0708: */
0709: protected static final boolean xmlBool(String value) {
0710: return (value != null && value.equals("true") ? true : false);
0711: }
0712:
0713: public void deleteUserProfile(IPerson person, int profileId)
0714: throws Exception {
0715: int userId = person.getID();
0716: deleteUserProfile(userId, profileId);
0717: }
0718:
0719: private Document getDOM(String uri) throws Exception {
0720: DOMResult result = new DOMResult();
0721: SAXSource source = new SAXSource(new InputSource(ResourceLoader
0722: .getResourceAsStream(this .getClass(), uri)));
0723: TransformerFactory tFactory = TransformerFactory.newInstance();
0724: Transformer emptytr = tFactory.newTransformer();
0725: emptytr.transform(source, result);
0726:
0727: // need to return a Document
0728: Node node = result.getNode();
0729: if (node instanceof Document) {
0730: return (Document) node;
0731: }
0732:
0733: Document dom = DocumentFactory.getNewDocument();
0734: dom.appendChild(dom.importNode(node, true));
0735: return dom;
0736: }
0737:
0738: private void deleteUserProfile(int userId, int profileId)
0739: throws Exception {
0740: Connection con = RDBMServices.getConnection();
0741: try {
0742: Statement stmt = con.createStatement();
0743: try {
0744: String sQuery = "DELETE FROM UP_USER_PROFILE WHERE USER_ID="
0745: + userId
0746: + " AND PROFILE_ID="
0747: + Integer.toString(profileId);
0748: if (log.isDebugEnabled())
0749: log
0750: .debug("RDBMUserLayoutStore::deleteUserProfile() : "
0751: + sQuery);
0752: stmt.executeUpdate(sQuery);
0753:
0754: // remove profile mappings
0755: sQuery = "DELETE FROM UP_USER_UA_MAP WHERE USER_ID="
0756: + userId + " AND PROFILE_ID="
0757: + Integer.toString(profileId);
0758: if (log.isDebugEnabled())
0759: log
0760: .debug("RDBMUserLayoutStore::deleteUserProfile() : "
0761: + sQuery);
0762: stmt.executeUpdate(sQuery);
0763:
0764: // remove parameter information
0765: sQuery = "DELETE FROM UP_SS_USER_PARM WHERE USER_ID="
0766: + userId + " AND PROFILE_ID="
0767: + Integer.toString(profileId);
0768: if (log.isDebugEnabled())
0769: log
0770: .debug("RDBMUserLayoutStore::deleteUserProfile() : "
0771: + sQuery);
0772: stmt.executeUpdate(sQuery);
0773:
0774: sQuery = "DELETE FROM UP_SS_USER_ATTS WHERE USER_ID="
0775: + userId + " AND PROFILE_ID="
0776: + Integer.toString(profileId);
0777: if (log.isDebugEnabled())
0778: log
0779: .debug("RDBMUserLayoutStore::deleteUserProfile() : "
0780: + sQuery);
0781: stmt.executeUpdate(sQuery);
0782:
0783: } finally {
0784: stmt.close();
0785: }
0786: } finally {
0787: RDBMServices.releaseConnection(con);
0788: }
0789: }
0790:
0791: /**
0792: * Dump a document tree structure on stdout
0793: * @param node
0794: * @param indent
0795: */
0796: public static final void dumpDoc(Node node, String indent) {
0797: if (node == null) {
0798: return;
0799: }
0800: if (node instanceof Element) {
0801: System.err.print(indent + "element: tag="
0802: + ((Element) node).getTagName() + " ");
0803: } else if (node instanceof Document) {
0804: System.err.print("document:");
0805: } else {
0806: System.err.print(indent + "node:");
0807: }
0808: System.err.println("name=" + node.getNodeName() + " value="
0809: + node.getNodeValue());
0810: NamedNodeMap nm = node.getAttributes();
0811: if (nm != null) {
0812: for (int i = 0; i < nm.getLength(); i++) {
0813: System.err.println(indent + " "
0814: + nm.item(i).getNodeName() + ": '"
0815: + nm.item(i).getNodeValue() + "'");
0816: }
0817: System.err.println(indent + "--");
0818: }
0819: if (node.hasChildNodes()) {
0820: dumpDoc(node.getFirstChild(), indent + " ");
0821: }
0822: dumpDoc(node.getNextSibling(), indent);
0823: }
0824:
0825: /**
0826: *
0827: * CoreStyleSheet
0828: *
0829: */
0830: public Hashtable getMimeTypeList() throws Exception {
0831: Connection con = RDBMServices.getConnection();
0832: try {
0833: Statement stmt = con.createStatement();
0834: try {
0835: String sQuery = "SELECT A.MIME_TYPE, A.MIME_TYPE_DESCRIPTION FROM UP_MIME_TYPE A, UP_SS_MAP B WHERE B.MIME_TYPE=A.MIME_TYPE";
0836: if (log.isDebugEnabled())
0837: log
0838: .debug("RDBMUserLayoutStore::getMimeTypeList() : "
0839: + sQuery);
0840: ResultSet rs = stmt.executeQuery(sQuery);
0841: try {
0842: Hashtable list = new Hashtable();
0843: while (rs.next()) {
0844: list.put(rs.getString("MIME_TYPE"), rs
0845: .getString("MIME_TYPE_DESCRIPTION"));
0846: }
0847: return list;
0848: } finally {
0849: rs.close();
0850: }
0851: } finally {
0852: stmt.close();
0853: }
0854: } finally {
0855: RDBMServices.releaseConnection(con);
0856: }
0857: }
0858:
0859: /**
0860: * Return the next available channel structure id for a user
0861: * @param person
0862: * @return the next available channel structure id
0863: */
0864: public String generateNewChannelSubscribeId(IPerson person)
0865: throws Exception {
0866: return getNextStructId(person, channelPrefix);
0867: }
0868:
0869: /**
0870: * Return the next available folder structure id for a user
0871: * @param person
0872: * @return a <code>String</code> that is the next free structure ID
0873: * @exception Exception
0874: */
0875: public String generateNewFolderId(IPerson person) throws Exception {
0876: return getNextStructId(person, folderPrefix);
0877: }
0878:
0879: /**
0880: * Return the next available structure id for a user
0881: * @param person
0882: * @param prefix
0883: * @return next free structure ID
0884: * @exception Exception
0885: */
0886: protected synchronized String getNextStructId(IPerson person,
0887: String prefix) throws Exception {
0888: int userId = person.getID();
0889: Connection con = RDBMServices.getConnection();
0890: try {
0891: RDBMServices.setAutoCommit(con, false);
0892: Statement stmt = con.createStatement();
0893: try {
0894: String sQuery = "SELECT NEXT_STRUCT_ID FROM UP_USER WHERE USER_ID="
0895: + userId;
0896: for (int i = 0; i < 25; i++) {
0897: if (log.isDebugEnabled())
0898: log
0899: .debug("RDBMUserLayoutStore::getNextStructId(): "
0900: + sQuery);
0901: ResultSet rs = stmt.executeQuery(sQuery);
0902: int currentStructId;
0903: try {
0904: if (rs.next()) {
0905: currentStructId = rs.getInt(1);
0906: } else {
0907: throw new SQLException(
0908: "no rows returned for query ["
0909: + sQuery + "]");
0910: }
0911: } finally {
0912: rs.close();
0913: }
0914: int nextStructId = currentStructId + 1;
0915: try {
0916: String sUpdate = "UPDATE UP_USER SET NEXT_STRUCT_ID="
0917: + nextStructId
0918: + " WHERE USER_ID="
0919: + userId
0920: + " AND NEXT_STRUCT_ID="
0921: + currentStructId;
0922: if (log.isDebugEnabled())
0923: log
0924: .debug("RDBMUserLayoutStore::getNextStructId(): "
0925: + sUpdate);
0926: stmt.executeUpdate(sUpdate);
0927: RDBMServices.commit(con);
0928: return prefix + nextStructId;
0929: } catch (SQLException sqle) {
0930: RDBMServices.rollback(con);
0931: // Assume a concurrent update. Try again after some random amount of milliseconds.
0932: Thread
0933: .sleep(java.lang.Math
0934: .round(java.lang.Math.random() * 3 * 1000)); // Retry in up to 3 seconds
0935: }
0936: }
0937: } finally {
0938: stmt.close();
0939: }
0940: } finally {
0941: RDBMServices.releaseConnection(con);
0942: }
0943: throw new SQLException(
0944: "Unable to generate a new structure id for user "
0945: + userId);
0946: }
0947:
0948: /**
0949: * Return the Structure ID tag
0950: * @param structId
0951: * @param chanId
0952: * @return ID tag
0953: */
0954: protected String getStructId(int structId, int chanId) {
0955: if (chanId == 0) {
0956: return folderPrefix + structId;
0957: } else {
0958: return channelPrefix + structId;
0959: }
0960: }
0961:
0962: /**
0963: * Obtain structure stylesheet description object for a given structure stylesheet id.
0964: * @param stylesheetId the id of the structure stylesheet
0965: * @return structure stylesheet description
0966: */
0967: public StructureStylesheetDescription getStructureStylesheetDescription(
0968: int stylesheetId) throws Exception {
0969: StructureStylesheetDescription ssd = null;
0970: Connection con = null;
0971: try {
0972: con = RDBMServices.getConnection();
0973: Statement stmt = con.createStatement();
0974: int dbOffset = 0;
0975: String sQuery = "SELECT SS_NAME,SS_URI,SS_DESCRIPTION_URI,SS_DESCRIPTION_TEXT";
0976: if (RDBMServices.getDbMetaData().supportsOuterJoins()) {
0977: sQuery += ",TYPE,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT FROM "
0978: + RDBMServices.getDbMetaData().getJoinQuery()
0979: .getQuery("ss_struct");
0980: dbOffset = 4;
0981: } else {
0982: sQuery += " FROM UP_SS_STRUCT USS WHERE";
0983: }
0984: sQuery += " USS.SS_ID=" + stylesheetId;
0985:
0986: if (log.isDebugEnabled())
0987: log
0988: .debug("RDBMUserLayoutStore::getStructureStylesheetDescription(): "
0989: + sQuery);
0990: ResultSet rs = stmt.executeQuery(sQuery);
0991: try {
0992: if (rs.next()) {
0993: ssd = new StructureStylesheetDescription();
0994: ssd.setId(stylesheetId);
0995: ssd.setStylesheetName(rs.getString(1));
0996: ssd.setStylesheetURI(rs.getString(2));
0997: ssd.setStylesheetDescriptionURI(rs.getString(3));
0998: ssd.setStylesheetWordDescription(rs.getString(4));
0999: }
1000:
1001: if (!RDBMServices.getDbMetaData().supportsOuterJoins()) {
1002: rs.close();
1003: // retrieve stylesheet params and attributes
1004: sQuery = "SELECT TYPE,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT FROM UP_SS_STRUCT_PAR WHERE SS_ID="
1005: + stylesheetId;
1006: if (log.isDebugEnabled())
1007: log
1008: .debug("RDBMUserLayoutStore::getStructureStylesheetDescription(): "
1009: + sQuery);
1010: rs = stmt.executeQuery(sQuery);
1011: }
1012:
1013: while (true) {
1014: if (!RDBMServices.getDbMetaData()
1015: .supportsOuterJoins()
1016: && !rs.next()) {
1017: break;
1018: }
1019:
1020: int type = rs.getInt(dbOffset + 1);
1021: if (rs.wasNull()) {
1022: break;
1023: }
1024: if (type == 1) {
1025: // param
1026: ssd.addStylesheetParameter(rs
1027: .getString(dbOffset + 2), rs
1028: .getString(dbOffset + 3), rs
1029: .getString(dbOffset + 4));
1030: } else if (type == 2) {
1031: // folder attribute
1032: ssd.addFolderAttribute(rs
1033: .getString(dbOffset + 2), rs
1034: .getString(dbOffset + 3), rs
1035: .getString(dbOffset + 4));
1036: } else if (type == 3) {
1037: // channel attribute
1038: ssd.addChannelAttribute(rs
1039: .getString(dbOffset + 2), rs
1040: .getString(dbOffset + 3), rs
1041: .getString(dbOffset + 4));
1042: } else {
1043: if (log.isDebugEnabled())
1044: log
1045: .debug("RDBMUserLayoutStore::getStructureStylesheetDescription() : encountered param of unknown type! (stylesheetId="
1046: + stylesheetId
1047: + " param_name=\""
1048: + rs
1049: .getString(dbOffset + 2)
1050: + "\" type=" + type + ").");
1051: }
1052: if (RDBMServices.getDbMetaData()
1053: .supportsOuterJoins()
1054: && !rs.next()) {
1055: break;
1056: }
1057: }
1058: } finally {
1059: try {
1060: rs.close();
1061: } catch (Exception e) {
1062: }
1063: try {
1064: stmt.close();
1065: } catch (Exception e) {
1066: }
1067: }
1068: } finally {
1069: RDBMServices.releaseConnection(con);
1070: }
1071: return ssd;
1072: }
1073:
1074: /**
1075: * Obtain ID for known structure stylesheet name
1076: * @param ssName name of the structure stylesheet
1077: * @return id or null if no stylesheet matches the name given.
1078: */
1079: public Integer getStructureStylesheetId(String ssName)
1080: throws Exception {
1081: Connection con = null;
1082: Statement stmt = null;
1083: ResultSet rs = null;
1084:
1085: try {
1086: con = RDBMServices.getConnection();
1087: RDBMServices.setAutoCommit(con, false);
1088: stmt = con.createStatement();
1089: String sQuery = "SELECT SS_ID FROM UP_SS_STRUCT WHERE SS_NAME='"
1090: + ssName + "'";
1091: rs = stmt.executeQuery(sQuery);
1092: if (rs.next()) {
1093: int id = rs.getInt("SS_ID");
1094: if (rs.wasNull()) {
1095: id = 0;
1096: }
1097: return new Integer(id);
1098: }
1099: } finally {
1100: RDBMServices.closeResultSet(rs);
1101: RDBMServices.closeStatement(stmt);
1102: RDBMServices.releaseConnection(con);
1103: }
1104: return null;
1105: }
1106:
1107: /**
1108: * Obtain a list of structure stylesheet descriptions that have stylesheets for a given
1109: * mime type.
1110: * @param mimeType
1111: * @return a mapping from stylesheet names to structure stylesheet description objects
1112: */
1113: public Hashtable getStructureStylesheetList(String mimeType)
1114: throws Exception {
1115: Connection con = RDBMServices.getConnection();
1116: Hashtable list = new Hashtable();
1117: try {
1118: Statement stmt = con.createStatement();
1119: try {
1120: String sQuery = "SELECT A.SS_ID FROM UP_SS_STRUCT A, UP_SS_THEME B WHERE B.MIME_TYPE='"
1121: + mimeType + "' AND B.STRUCT_SS_ID=A.SS_ID";
1122: if (log.isDebugEnabled())
1123: log
1124: .debug("RDBMUserLayoutStore::getStructureStylesheetList() : "
1125: + sQuery);
1126: ResultSet rs = stmt.executeQuery(sQuery);
1127: try {
1128: while (rs.next()) {
1129: StructureStylesheetDescription ssd = getStructureStylesheetDescription(rs
1130: .getInt("SS_ID"));
1131: if (ssd != null)
1132: list.put(new Integer(ssd.getId()), ssd);
1133: }
1134: } finally {
1135: rs.close();
1136: }
1137: } finally {
1138: stmt.close();
1139: }
1140: } finally {
1141: RDBMServices.releaseConnection(con);
1142: }
1143: return list;
1144: }
1145:
1146: /**
1147: * Obtain a list of strcture stylesheet descriptions registered on the system
1148: * @return a <code>Hashtable</code> mapping stylesheet id (<code>Integer</code> objects) to {@link StructureStylesheetDescription} objects
1149: * @exception Exception
1150: */
1151: public Hashtable getStructureStylesheetList() throws Exception {
1152: Connection con = RDBMServices.getConnection();
1153: Hashtable list = new Hashtable();
1154: try {
1155: Statement stmt = con.createStatement();
1156: try {
1157: String sQuery = "SELECT SS_ID FROM UP_SS_STRUCT";
1158: if (log.isDebugEnabled())
1159: log
1160: .debug("RDBMUserLayoutStore::getStructureStylesheetList() : "
1161: + sQuery);
1162: ResultSet rs = stmt.executeQuery(sQuery);
1163: try {
1164: while (rs.next()) {
1165: StructureStylesheetDescription ssd = getStructureStylesheetDescription(rs
1166: .getInt("SS_ID"));
1167: if (ssd != null)
1168: list.put(new Integer(ssd.getId()), ssd);
1169: }
1170: } finally {
1171: rs.close();
1172: }
1173: } finally {
1174: stmt.close();
1175: }
1176: } finally {
1177: RDBMServices.releaseConnection(con);
1178: }
1179: return list;
1180: }
1181:
1182: public StructureStylesheetUserPreferences getStructureStylesheetUserPreferences(
1183: IPerson person, int profileId, int stylesheetId)
1184: throws Exception {
1185: int userId = person.getID();
1186: StructureStylesheetUserPreferences ssup;
1187: Connection con = RDBMServices.getConnection();
1188: try {
1189: Statement stmt = con.createStatement();
1190: try {
1191: ResultSet rs = null;
1192:
1193: // get stylesheet description
1194: StructureStylesheetDescription ssd = getStructureStylesheetDescription(stylesheetId);
1195:
1196: // get user defined defaults
1197: int layoutId = this .getLayoutID(userId, profileId);
1198:
1199: if (layoutId == 0) { // First time, grab the default layout for this user
1200: String sQuery = "SELECT USER_DFLT_USR_ID FROM UP_USER WHERE USER_ID="
1201: + userId;
1202: if (log.isDebugEnabled())
1203: log
1204: .debug("RDBMUserLayoutStore::getStructureStylesheetUserPreferences(): "
1205: + sQuery);
1206: rs = stmt.executeQuery(sQuery);
1207: try {
1208: rs.next();
1209: userId = rs.getInt(1);
1210: } finally {
1211: rs.close();
1212: }
1213: }
1214:
1215: String sQuery = "SELECT PARAM_NAME, PARAM_VAL FROM UP_SS_USER_PARM WHERE USER_ID="
1216: + userId
1217: + " AND PROFILE_ID="
1218: + profileId
1219: + " AND SS_ID="
1220: + stylesheetId
1221: + " AND SS_TYPE=1";
1222: if (log.isDebugEnabled())
1223: log
1224: .debug("RDBMUserLayoutStore::getStructureStylesheetUserPreferences(): "
1225: + sQuery);
1226: rs = stmt.executeQuery(sQuery);
1227: try {
1228: while (rs.next()) {
1229: // stylesheet param
1230: ssd.setStylesheetParameterDefaultValue(rs
1231: .getString(1), rs.getString(2));
1232: }
1233: } finally {
1234: rs.close();
1235: }
1236: ssup = new StructureStylesheetUserPreferences();
1237: ssup.setStylesheetId(stylesheetId);
1238: // fill stylesheet description with defaults
1239: for (Enumeration e = ssd.getStylesheetParameterNames(); e
1240: .hasMoreElements();) {
1241: String pName = (String) e.nextElement();
1242: ssup.putParameterValue(pName, ssd
1243: .getStylesheetParameterDefaultValue(pName));
1244: }
1245: for (Enumeration e = ssd.getChannelAttributeNames(); e
1246: .hasMoreElements();) {
1247: String pName = (String) e.nextElement();
1248: ssup.addChannelAttribute(pName, ssd
1249: .getChannelAttributeDefaultValue(pName));
1250: }
1251: for (Enumeration e = ssd.getFolderAttributeNames(); e
1252: .hasMoreElements();) {
1253: String pName = (String) e.nextElement();
1254: ssup.addFolderAttribute(pName, ssd
1255: .getFolderAttributeDefaultValue(pName));
1256: }
1257: // get user preferences
1258: sQuery = "SELECT PARAM_NAME, PARAM_VAL, PARAM_TYPE, ULS.STRUCT_ID, CHAN_ID FROM UP_SS_USER_ATTS UUSA, UP_LAYOUT_STRUCT ULS WHERE UUSA.USER_ID="
1259: + userId
1260: + " AND PROFILE_ID="
1261: + profileId
1262: + " AND SS_ID="
1263: + stylesheetId
1264: + " AND SS_TYPE=1 AND UUSA.STRUCT_ID = ULS.STRUCT_ID AND UUSA.USER_ID = ULS.USER_ID";
1265: if (log.isDebugEnabled())
1266: log
1267: .debug("RDBMUserLayoutStore::getStructureStylesheetUserPreferences(): "
1268: + sQuery);
1269: rs = stmt.executeQuery(sQuery);
1270: try {
1271: while (rs.next()) {
1272: String temp1 = rs.getString(1); // Access columns left to right
1273: String temp2 = rs.getString(2);
1274: int param_type = rs.getInt(3);
1275: int structId = rs.getInt(4);
1276: if (rs.wasNull()) {
1277: structId = 0;
1278: }
1279: int chanId = rs.getInt(5);
1280: if (rs.wasNull()) {
1281: chanId = 0;
1282: }
1283:
1284: if (param_type == 1) {
1285: // stylesheet param
1286: log
1287: .error("RDBMUserLayoutStore::getStructureStylesheetUserPreferences() : stylesheet global params should be specified in the user defaults table ! UP_SS_USER_ATTS is corrupt. (userId="
1288: + Integer.toString(userId)
1289: + ", profileId="
1290: + Integer
1291: .toString(profileId)
1292: + ", stylesheetId="
1293: + Integer
1294: .toString(stylesheetId)
1295: + ", param_name=\""
1296: + temp1
1297: + "\", param_type="
1298: + Integer
1299: .toString(param_type));
1300: } else if (param_type == 2) {
1301: // folder attribute
1302: ssup.setFolderAttributeValue(getStructId(
1303: structId, chanId), temp1, temp2);
1304: } else if (param_type == 3) {
1305: // channel attribute
1306: ssup.setChannelAttributeValue(getStructId(
1307: structId, chanId), temp1, temp2);
1308: } else {
1309: // unknown param type
1310: log
1311: .error("RDBMUserLayoutStore::getStructureStylesheetUserPreferences() : unknown param type encountered! DB corrupt. (userId="
1312: + Integer.toString(userId)
1313: + ", profileId="
1314: + Integer
1315: .toString(profileId)
1316: + ", stylesheetId="
1317: + Integer
1318: .toString(stylesheetId)
1319: + ", param_name=\""
1320: + temp1
1321: + "\", param_type="
1322: + Integer
1323: .toString(param_type));
1324: }
1325: }
1326: } finally {
1327: rs.close();
1328: }
1329: } finally {
1330: stmt.close();
1331: }
1332: } finally {
1333: RDBMServices.releaseConnection(con);
1334: }
1335: return ssup;
1336: }
1337:
1338: /**
1339: * Obtain theme stylesheet description object for a given theme stylesheet id.
1340: * @param stylesheetId the id of the theme stylesheet
1341: * @return theme stylesheet description
1342: */
1343: public ThemeStylesheetDescription getThemeStylesheetDescription(
1344: int stylesheetId) throws Exception {
1345: ThemeStylesheetDescription tsd = null;
1346: Connection con = null;
1347: try {
1348: con = RDBMServices.getConnection();
1349: Statement stmt = con.createStatement();
1350: int dbOffset = 0;
1351: String sQuery = "SELECT SS_NAME,SS_URI,SS_DESCRIPTION_URI,SS_DESCRIPTION_TEXT,STRUCT_SS_ID,SAMPLE_ICON_URI,SAMPLE_URI,MIME_TYPE,DEVICE_TYPE,SERIALIZER_NAME,UP_MODULE_CLASS";
1352: if (RDBMServices.getDbMetaData().supportsOuterJoins()) {
1353: sQuery += ",TYPE,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT FROM "
1354: + RDBMServices.getDbMetaData().getJoinQuery()
1355: .getQuery("ss_theme");
1356: dbOffset = 11;
1357: } else {
1358: sQuery += " FROM UP_SS_THEME UTS WHERE";
1359: }
1360: sQuery += " UTS.SS_ID=" + stylesheetId;
1361: if (log.isDebugEnabled())
1362: log
1363: .debug("RDBMUserLayoutStore::getThemeStylesheetDescription(): "
1364: + sQuery);
1365: ResultSet rs = stmt.executeQuery(sQuery);
1366: try {
1367: if (rs.next()) {
1368: tsd = new ThemeStylesheetDescription();
1369: tsd.setId(stylesheetId);
1370: tsd.setStylesheetName(rs.getString(1));
1371: tsd.setStylesheetURI(rs.getString(2));
1372: tsd.setStylesheetDescriptionURI(rs.getString(3));
1373: tsd.setStylesheetWordDescription(rs.getString(4));
1374: int ssId = rs.getInt(5);
1375: if (rs.wasNull()) {
1376: ssId = 0;
1377: }
1378: tsd.setStructureStylesheetId(ssId);
1379: tsd.setSampleIconURI(rs.getString(6));
1380: tsd.setSamplePictureURI(rs.getString(7));
1381: tsd.setMimeType(rs.getString(8));
1382: tsd.setDeviceType(rs.getString(9));
1383: tsd.setSerializerName(rs.getString(10));
1384: tsd.setCustomUserPreferencesManagerClass(rs
1385: .getString(11));
1386: }
1387:
1388: if (!RDBMServices.getDbMetaData().supportsOuterJoins()) {
1389: rs.close();
1390: // retrieve stylesheet params and attributes
1391: sQuery = "SELECT TYPE,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT FROM UP_SS_THEME_PARM WHERE SS_ID="
1392: + stylesheetId;
1393: if (log.isDebugEnabled())
1394: log
1395: .debug("RDBMUserLayoutStore::getThemeStylesheetDescription(): "
1396: + sQuery);
1397: rs = stmt.executeQuery(sQuery);
1398: }
1399: while (true) {
1400: if (!RDBMServices.getDbMetaData()
1401: .supportsOuterJoins()
1402: && !rs.next()) {
1403: break;
1404: }
1405: int type = rs.getInt(dbOffset + 1);
1406: if (rs.wasNull()) {
1407: break;
1408: }
1409: if (type == 1) {
1410: // param
1411: tsd.addStylesheetParameter(rs
1412: .getString(dbOffset + 2), rs
1413: .getString(dbOffset + 3), rs
1414: .getString(dbOffset + 4));
1415: } else if (type == 3) {
1416: // channel attribute
1417: tsd.addChannelAttribute(rs
1418: .getString(dbOffset + 2), rs
1419: .getString(dbOffset + 3), rs
1420: .getString(dbOffset + 4));
1421: } else if (type == 2) {
1422: // folder attributes are not allowed here
1423: log
1424: .error("RDBMUserLayoutStore::getThemeStylesheetDescription() : encountered a folder attribute specified for a theme stylesheet ! Corrupted DB entry. (stylesheetId="
1425: + stylesheetId
1426: + " param_name=\""
1427: + rs.getString(dbOffset + 2)
1428: + "\" type=" + type + ").");
1429: } else {
1430: log
1431: .error("RDBMUserLayoutStore::getThemeStylesheetDescription() : encountered param of unknown type! (stylesheetId="
1432: + stylesheetId
1433: + " param_name=\""
1434: + rs.getString(dbOffset + 2)
1435: + "\" type=" + type + ").");
1436: }
1437: if (RDBMServices.getDbMetaData()
1438: .supportsOuterJoins()
1439: && !rs.next()) {
1440: break;
1441: }
1442: }
1443: } finally {
1444: try {
1445: rs.close();
1446: } catch (Exception e) {
1447: }
1448: try {
1449: stmt.close();
1450: } catch (Exception e) {
1451: }
1452: }
1453: } finally {
1454: RDBMServices.releaseConnection(con);
1455: }
1456: return tsd;
1457: }
1458:
1459: /**
1460: * Obtain ID for known theme stylesheet name
1461: * @param tsName name of the theme stylesheet
1462: * @return id or null if no theme matches the name given.
1463: */
1464: public Integer getThemeStylesheetId(String tsName) throws Exception {
1465: Integer id = null;
1466: Connection con = null;
1467: Statement stmt = null;
1468: ResultSet rs = null;
1469: try {
1470: con = RDBMServices.getConnection();
1471: stmt = con.createStatement();
1472: String sQuery = "SELECT SS_ID FROM UP_SS_THEME WHERE SS_NAME='"
1473: + tsName + "'";
1474: rs = stmt.executeQuery(sQuery);
1475: if (rs.next()) {
1476: id = new Integer(rs.getInt("SS_ID"));
1477: }
1478: } finally {
1479: RDBMServices.closeResultSet(rs);
1480: RDBMServices.closeStatement(stmt);
1481: RDBMServices.releaseConnection(con);
1482: }
1483: return id;
1484: }
1485:
1486: /**
1487: * Obtain a list of theme stylesheet descriptions for a given structure stylesheet
1488: * @param structureStylesheetId
1489: * @return a map of stylesheet names to theme stylesheet description objects
1490: * @exception Exception
1491: */
1492: public Hashtable getThemeStylesheetList(int structureStylesheetId)
1493: throws Exception {
1494: Connection con = RDBMServices.getConnection();
1495: Hashtable list = new Hashtable();
1496: try {
1497: Statement stmt = con.createStatement();
1498: try {
1499: String sQuery = "SELECT SS_ID FROM UP_SS_THEME WHERE STRUCT_SS_ID="
1500: + structureStylesheetId;
1501: if (log.isDebugEnabled())
1502: log
1503: .debug("RDBMUserLayoutStore::getThemeStylesheetList() : "
1504: + sQuery);
1505: ResultSet rs = stmt.executeQuery(sQuery);
1506: try {
1507: while (rs.next()) {
1508: ThemeStylesheetDescription tsd = getThemeStylesheetDescription(rs
1509: .getInt("SS_ID"));
1510: if (tsd != null)
1511: list.put(new Integer(tsd.getId()), tsd);
1512: }
1513: } finally {
1514: rs.close();
1515: }
1516: } finally {
1517: stmt.close();
1518: }
1519: } finally {
1520: RDBMServices.releaseConnection(con);
1521: }
1522: return list;
1523: }
1524:
1525: /**
1526: * Obtain a list of theme stylesheet descriptions registered on the system
1527: * @return a <code>Hashtable</code> mapping stylesheet id (<code>Integer</code> objects) to {@link ThemeStylesheetDescription} objects
1528: * @exception Exception
1529: */
1530: public Hashtable getThemeStylesheetList() throws Exception {
1531: Connection con = RDBMServices.getConnection();
1532: Hashtable list = new Hashtable();
1533: try {
1534: Statement stmt = con.createStatement();
1535: try {
1536: String sQuery = "SELECT SS_ID FROM UP_SS_THEME";
1537: if (log.isDebugEnabled())
1538: log
1539: .debug("RDBMUserLayoutStore::getThemeStylesheetList() : "
1540: + sQuery);
1541: ResultSet rs = stmt.executeQuery(sQuery);
1542: try {
1543: while (rs.next()) {
1544: ThemeStylesheetDescription tsd = getThemeStylesheetDescription(rs
1545: .getInt("SS_ID"));
1546: if (tsd != null)
1547: list.put(new Integer(tsd.getId()), tsd);
1548: }
1549: } finally {
1550: rs.close();
1551: }
1552: } finally {
1553: stmt.close();
1554: }
1555: } finally {
1556: RDBMServices.releaseConnection(con);
1557: }
1558: return list;
1559: }
1560:
1561: public ThemeStylesheetUserPreferences getThemeStylesheetUserPreferences(
1562: IPerson person, int profileId, int stylesheetId)
1563: throws Exception {
1564: int userId = person.getID();
1565: ThemeStylesheetUserPreferences tsup;
1566: Connection con = RDBMServices.getConnection();
1567: try {
1568: Statement stmt = con.createStatement();
1569: try {
1570: // get stylesheet description
1571: ThemeStylesheetDescription tsd = getThemeStylesheetDescription(stylesheetId);
1572: // get user defined defaults
1573:
1574: int layoutId = this .getLayoutID(userId, profileId);
1575: ResultSet rs;
1576:
1577: if (layoutId == 0) { // First time, grab the default layout for this user
1578: String sQuery = "SELECT USER_DFLT_USR_ID FROM UP_USER WHERE USER_ID="
1579: + userId;
1580: if (log.isDebugEnabled())
1581: log
1582: .debug("RDBMUserLayoutStore::getThemeStylesheetUserPreferences(): "
1583: + sQuery);
1584: rs = stmt.executeQuery(sQuery);
1585: try {
1586: rs.next();
1587: userId = rs.getInt(1);
1588: } finally {
1589: rs.close();
1590: }
1591: }
1592:
1593: String sQuery = "SELECT PARAM_NAME, PARAM_VAL FROM UP_SS_USER_PARM WHERE USER_ID="
1594: + userId
1595: + " AND PROFILE_ID="
1596: + profileId
1597: + " AND SS_ID="
1598: + stylesheetId
1599: + " AND SS_TYPE=2";
1600: if (log.isDebugEnabled())
1601: log
1602: .debug("RDBMUserLayoutStore::getThemeStylesheetUserPreferences(): "
1603: + sQuery);
1604: rs = stmt.executeQuery(sQuery);
1605: try {
1606: while (rs.next()) {
1607: // stylesheet param
1608: tsd.setStylesheetParameterDefaultValue(rs
1609: .getString(1), rs.getString(2));
1610: }
1611: } finally {
1612: rs.close();
1613: }
1614: tsup = new ThemeStylesheetUserPreferences();
1615: tsup.setStylesheetId(stylesheetId);
1616: // fill stylesheet description with defaults
1617: for (Enumeration e = tsd.getStylesheetParameterNames(); e
1618: .hasMoreElements();) {
1619: String pName = (String) e.nextElement();
1620: tsup.putParameterValue(pName, tsd
1621: .getStylesheetParameterDefaultValue(pName));
1622: }
1623: for (Enumeration e = tsd.getChannelAttributeNames(); e
1624: .hasMoreElements();) {
1625: String pName = (String) e.nextElement();
1626: tsup.addChannelAttribute(pName, tsd
1627: .getChannelAttributeDefaultValue(pName));
1628: }
1629: // get user preferences
1630: sQuery = "SELECT PARAM_TYPE, PARAM_NAME, PARAM_VAL, ULS.STRUCT_ID, CHAN_ID FROM UP_SS_USER_ATTS UUSA, UP_LAYOUT_STRUCT ULS WHERE UUSA.USER_ID="
1631: + userId
1632: + " AND PROFILE_ID="
1633: + profileId
1634: + " AND SS_ID="
1635: + stylesheetId
1636: + " AND SS_TYPE=2 AND UUSA.STRUCT_ID = ULS.STRUCT_ID AND UUSA.USER_ID = ULS.USER_ID";
1637: if (log.isDebugEnabled())
1638: log
1639: .debug("RDBMUserLayoutStore::getThemeStylesheetUserPreferences(): "
1640: + sQuery);
1641: rs = stmt.executeQuery(sQuery);
1642: try {
1643: while (rs.next()) {
1644: int param_type = rs.getInt(1);
1645: if (rs.wasNull()) {
1646: param_type = 0;
1647: }
1648: int structId = rs.getInt(4);
1649: if (rs.wasNull()) {
1650: structId = 0;
1651: }
1652: int chanId = rs.getInt(5);
1653: if (rs.wasNull()) {
1654: chanId = 0;
1655: }
1656: if (param_type == 1) {
1657: // stylesheet param
1658: log
1659: .error("RDBMUserLayoutStore::getThemeStylesheetUserPreferences() : stylesheet global params should be specified in the user defaults table ! UP_SS_USER_ATTS is corrupt. (userId="
1660: + Integer.toString(userId)
1661: + ", profileId="
1662: + Integer
1663: .toString(profileId)
1664: + ", stylesheetId="
1665: + Integer
1666: .toString(stylesheetId)
1667: + ", param_name=\""
1668: + rs.getString(2)
1669: + "\", param_type="
1670: + Integer
1671: .toString(param_type));
1672: } else if (param_type == 2) {
1673: // folder attribute
1674: log
1675: .error("RDBMUserLayoutStore::getThemeStylesheetUserPreferences() : folder attribute specified for the theme stylesheet! UP_SS_USER_ATTS corrupt. (userId="
1676: + Integer.toString(userId)
1677: + ", profileId="
1678: + Integer
1679: .toString(profileId)
1680: + ", stylesheetId="
1681: + Integer
1682: .toString(stylesheetId)
1683: + ", param_name=\""
1684: + rs.getString(2)
1685: + "\", param_type="
1686: + Integer
1687: .toString(param_type));
1688: } else if (param_type == 3) {
1689: // channel attribute
1690: tsup.setChannelAttributeValue(getStructId(
1691: structId, chanId), rs.getString(2),
1692: rs.getString(3));
1693: } else {
1694: // unknown param type
1695: log
1696: .error("RDBMUserLayoutStore::getThemeStylesheetUserPreferences() : unknown param type encountered! DB corrupt. (userId="
1697: + Integer.toString(userId)
1698: + ", profileId="
1699: + Integer
1700: .toString(profileId)
1701: + ", stylesheetId="
1702: + Integer
1703: .toString(stylesheetId)
1704: + ", param_name=\""
1705: + rs.getString(2)
1706: + "\", param_type="
1707: + Integer
1708: .toString(param_type));
1709: }
1710: }
1711: } finally {
1712: rs.close();
1713: }
1714: } finally {
1715: stmt.close();
1716: }
1717: } finally {
1718: RDBMServices.releaseConnection(con);
1719: }
1720: return tsup;
1721: }
1722:
1723: // private helper modules that retreive information from the DOM structure of the description files
1724: private String getName(Document descr) {
1725: NodeList names = descr.getElementsByTagName("name");
1726: Node name = null;
1727: for (int i = names.getLength() - 1; i >= 0; i--) {
1728: name = names.item(i);
1729: if (name.getParentNode().getNodeName().equals(
1730: "stylesheetdescription"))
1731: break;
1732: else
1733: name = null;
1734: }
1735: if (name != null) {
1736: return this .getTextChildNodeValue(name);
1737: } else {
1738: if (log.isDebugEnabled())
1739: log
1740: .debug("RDBMUserLayoutStore::getName() : no \"name\" element was found under the \"stylesheetdescription\" node!");
1741: return null;
1742: }
1743: }
1744:
1745: private String getRootElementTextValue(Document descr,
1746: String elementName) {
1747: NodeList names = descr.getElementsByTagName(elementName);
1748: Node name = null;
1749: for (int i = names.getLength() - 1; i >= 0; i--) {
1750: name = names.item(i);
1751:
1752: if (name.getParentNode().getNodeName().equals(
1753: "stylesheetdescription"))
1754: break;
1755: else
1756: name = null;
1757: }
1758: if (name != null) {
1759: return this .getTextChildNodeValue(name);
1760: } else {
1761: if (log.isDebugEnabled())
1762: log
1763: .debug("RDBMUserLayoutStore::getRootElementTextValue() : no \""
1764: + elementName
1765: + "\" element was found under the \"stylesheetdescription\" node!");
1766: return null;
1767: }
1768: }
1769:
1770: private String getDescription(Document descr) {
1771: NodeList descriptions = descr
1772: .getElementsByTagName("description");
1773: Node description = null;
1774: for (int i = descriptions.getLength() - 1; i >= 0; i--) {
1775: description = descriptions.item(i);
1776: if (description.getParentNode().getNodeName().equals(
1777: "stylesheetdescription"))
1778: break;
1779: else
1780: description = null;
1781: }
1782: if (description != null) {
1783: return this .getTextChildNodeValue(description);
1784: } else {
1785: if (log.isDebugEnabled())
1786: log
1787: .debug("RDBMUserLayoutStore::getDescription() : no \"description\" element was found under the \"stylesheetdescription\" node!");
1788: return null;
1789: }
1790: }
1791:
1792: private void populateParameterTable(Document descr,
1793: CoreStylesheetDescription csd) {
1794: NodeList parametersNodes = descr
1795: .getElementsByTagName("parameters");
1796: Node parametersNode = null;
1797: for (int i = parametersNodes.getLength() - 1; i >= 0; i--) {
1798: parametersNode = parametersNodes.item(i);
1799: if (parametersNode.getParentNode().getNodeName().equals(
1800: "stylesheetdescription"))
1801: break;
1802: else
1803: parametersNode = null;
1804: }
1805: if (parametersNode != null) {
1806: NodeList children = parametersNode.getChildNodes();
1807: for (int i = children.getLength() - 1; i >= 0; i--) {
1808: Node child = children.item(i);
1809: if (child.getNodeType() == Node.ELEMENT_NODE
1810: && child.getNodeName().equals("parameter")) {
1811: Element parameter = (Element) children.item(i);
1812: // process a <parameter> node
1813: String name = parameter.getAttribute("name");
1814: String description = null;
1815: String defaultvalue = null;
1816: NodeList pchildren = parameter.getChildNodes();
1817: for (int j = pchildren.getLength() - 1; j >= 0; j--) {
1818: Node pchild = pchildren.item(j);
1819: if (pchild.getNodeType() == Node.ELEMENT_NODE) {
1820: if (pchild.getNodeName().equals(
1821: "defaultvalue")) {
1822: defaultvalue = this
1823: .getTextChildNodeValue(pchild);
1824: } else if (pchild.getNodeName().equals(
1825: "description")) {
1826: description = this
1827: .getTextChildNodeValue(pchild);
1828: }
1829: }
1830: }
1831: if (log.isDebugEnabled())
1832: log
1833: .debug("RDBMUserLayoutStore::populateParameterTable() : adding a stylesheet parameter : (\""
1834: + name
1835: + "\",\""
1836: + defaultvalue
1837: + "\",\"" + description + "\")");
1838: csd.addStylesheetParameter(name, defaultvalue,
1839: description);
1840: }
1841: }
1842: }
1843: }
1844:
1845: private void populateFolderAttributeTable(Document descr,
1846: StructureStylesheetDescription cxsd) {
1847: NodeList parametersNodes = descr
1848: .getElementsByTagName("parameters");
1849: NodeList folderattributesNodes = descr
1850: .getElementsByTagName("folderattributes");
1851: Node folderattributesNode = null;
1852: for (int i = folderattributesNodes.getLength() - 1; i >= 0; i--) {
1853: folderattributesNode = folderattributesNodes.item(i);
1854: if (folderattributesNode.getParentNode().getNodeName()
1855: .equals("stylesheetdescription"))
1856: break;
1857: else
1858: folderattributesNode = null;
1859: }
1860: if (folderattributesNode != null) {
1861: NodeList children = folderattributesNode.getChildNodes();
1862: for (int i = children.getLength() - 1; i >= 0; i--) {
1863: Node child = children.item(i);
1864: if (child.getNodeType() == Node.ELEMENT_NODE
1865: && child.getNodeName().equals("attribute")) {
1866: Element attribute = (Element) children.item(i);
1867: // process a <attribute> node
1868: String name = attribute.getAttribute("name");
1869: String description = null;
1870: String defaultvalue = null;
1871: NodeList pchildren = attribute.getChildNodes();
1872: for (int j = pchildren.getLength() - 1; j >= 0; j--) {
1873: Node pchild = pchildren.item(j);
1874: if (pchild.getNodeType() == Node.ELEMENT_NODE) {
1875: if (pchild.getNodeName().equals(
1876: "defaultvalue")) {
1877: defaultvalue = this
1878: .getTextChildNodeValue(pchild);
1879: } else if (pchild.getNodeName().equals(
1880: "description")) {
1881: description = this
1882: .getTextChildNodeValue(pchild);
1883: }
1884: }
1885: }
1886: if (log.isDebugEnabled())
1887: log
1888: .debug("RDBMUserLayoutStore::populateFolderAttributeTable() : adding a stylesheet folder attribute : (\""
1889: + name
1890: + "\",\""
1891: + defaultvalue
1892: + "\",\"" + description + "\")");
1893: cxsd.addFolderAttribute(name, defaultvalue,
1894: description);
1895: }
1896: }
1897: }
1898: }
1899:
1900: private void populateChannelAttributeTable(Document descr,
1901: CoreXSLTStylesheetDescription cxsd) {
1902: NodeList channelattributesNodes = descr
1903: .getElementsByTagName("channelattributes");
1904: Node channelattributesNode = null;
1905: for (int i = channelattributesNodes.getLength() - 1; i >= 0; i--) {
1906: channelattributesNode = channelattributesNodes.item(i);
1907: if (channelattributesNode.getParentNode().getNodeName()
1908: .equals("stylesheetdescription"))
1909: break;
1910: else
1911: channelattributesNode = null;
1912: }
1913: if (channelattributesNode != null) {
1914: NodeList children = channelattributesNode.getChildNodes();
1915: for (int i = children.getLength() - 1; i >= 0; i--) {
1916: Node child = children.item(i);
1917: if (child.getNodeType() == Node.ELEMENT_NODE
1918: && child.getNodeName().equals("attribute")) {
1919: Element attribute = (Element) children.item(i);
1920: // process a <attribute> node
1921: String name = attribute.getAttribute("name");
1922: String description = null;
1923: String defaultvalue = null;
1924: NodeList pchildren = attribute.getChildNodes();
1925: for (int j = pchildren.getLength() - 1; j >= 0; j--) {
1926: Node pchild = pchildren.item(j);
1927: if (pchild.getNodeType() == Node.ELEMENT_NODE) {
1928: if (pchild.getNodeName().equals(
1929: "defaultvalue")) {
1930: defaultvalue = this
1931: .getTextChildNodeValue(pchild);
1932: } else if (pchild.getNodeName().equals(
1933: "description")) {
1934: description = this
1935: .getTextChildNodeValue(pchild);
1936: }
1937: }
1938: }
1939: if (log.isDebugEnabled())
1940: log
1941: .debug("RDBMUserLayoutStore::populateChannelAttributeTable() : adding a stylesheet channel attribute : (\""
1942: + name
1943: + "\",\""
1944: + defaultvalue
1945: + "\",\"" + description + "\")");
1946: cxsd.addChannelAttribute(name, defaultvalue,
1947: description);
1948: }
1949: }
1950: }
1951: }
1952:
1953: private Vector getVectorOfSimpleTextElementValues(Document descr,
1954: String elementName) {
1955: Vector v = new Vector();
1956: // find "stylesheetdescription" node, take the first one
1957: Element stylesheetdescriptionElement = (Element) (descr
1958: .getElementsByTagName("stylesheetdescription")).item(0);
1959: if (stylesheetdescriptionElement == null) {
1960: log
1961: .error("Could not obtain <stylesheetdescription> element");
1962: return null;
1963: }
1964: NodeList elements = stylesheetdescriptionElement
1965: .getElementsByTagName(elementName);
1966: for (int i = elements.getLength() - 1; i >= 0; i--) {
1967: v.add(this .getTextChildNodeValue(elements.item(i)));
1968: }
1969: return v;
1970: }
1971:
1972: private String getTextChildNodeValue(Node node) {
1973: if (node == null)
1974: return null;
1975: NodeList children = node.getChildNodes();
1976: for (int i = children.getLength() - 1; i >= 0; i--) {
1977: Node child = children.item(i);
1978: if (child.getNodeType() == Node.TEXT_NODE)
1979: return child.getNodeValue();
1980: }
1981: return null;
1982: }
1983:
1984: /**
1985: * UserPreferences
1986: */
1987: private int getUserBrowserMapping(IPerson person, String userAgent)
1988: throws Exception {
1989: if (userAgent.length() > 255) {
1990: userAgent = userAgent.substring(0, 254);
1991: log
1992: .debug("userAgent trimmed to 255 characters. userAgent: "
1993: + userAgent);
1994: }
1995: int userId = person.getID();
1996: int profileId = 0;
1997: Connection con = RDBMServices.getConnection();
1998: try {
1999: String sQuery = "SELECT PROFILE_ID, USER_ID "
2000: + "FROM UP_USER_UA_MAP WHERE USER_ID=? AND USER_AGENT=?";
2001: PreparedStatement pstmt = con.prepareStatement(sQuery);
2002:
2003: try {
2004: pstmt.setInt(1, userId);
2005: pstmt.setString(2, userAgent);
2006:
2007: if (log.isDebugEnabled())
2008: log
2009: .debug("RDBMUserLayoutStore::getUserBrowserMapping(): "
2010: + sQuery);
2011: ResultSet rs = pstmt.executeQuery();
2012: try {
2013: if (rs.next()) {
2014: profileId = rs.getInt("PROFILE_ID");
2015: if (rs.wasNull()) {
2016: profileId = 0;
2017: }
2018: } else {
2019: return 0;
2020: }
2021: } finally {
2022: rs.close();
2023: }
2024: } finally {
2025: pstmt.close();
2026: }
2027: } finally {
2028: RDBMServices.releaseConnection(con);
2029: }
2030: return profileId;
2031: }
2032:
2033: public Document getUserLayout(IPerson person, UserProfile profile)
2034: throws Exception {
2035: int userId = person.getID();
2036: int realUserId = userId;
2037: ResultSet rs;
2038: Connection con = RDBMServices.getConnection();
2039: LocaleManager localeManager = profile.getLocaleManager();
2040:
2041: RDBMServices.setAutoCommit(con, false); // May speed things up, can't hurt
2042:
2043: try {
2044: Document doc = DocumentFactory.getNewDocument();
2045: Element root = doc.createElement("layout");
2046: Statement stmt = con.createStatement();
2047: // A separate statement is needed so as not to interfere with ResultSet
2048: // of statements used for queries
2049: Statement insertStmt = con.createStatement();
2050: try {
2051: long startTime = System.currentTimeMillis();
2052: // eventually, we need to fix template layout implementations so you can just do this:
2053: // int layoutId=profile.getLayoutId();
2054: // but for now:
2055: int layoutId = this .getLayoutID(userId, profile
2056: .getProfileId());
2057:
2058: if (layoutId == 0) { // First time, grab the default layout for this user
2059: String sQuery = "SELECT USER_DFLT_USR_ID, USER_DFLT_LAY_ID FROM UP_USER WHERE USER_ID="
2060: + userId;
2061: if (log.isDebugEnabled())
2062: log
2063: .debug("RDBMUserLayoutStore::getUserLayout(): "
2064: + sQuery);
2065: rs = stmt.executeQuery(sQuery);
2066: try {
2067: rs.next();
2068: userId = rs.getInt(1);
2069: layoutId = rs.getInt(2);
2070: } finally {
2071: rs.close();
2072: }
2073:
2074: // Make sure the next struct id is set in case the user adds a channel
2075: sQuery = "SELECT NEXT_STRUCT_ID FROM UP_USER WHERE USER_ID="
2076: + userId;
2077: if (log.isDebugEnabled())
2078: log
2079: .debug("RDBMUserLayoutStore::getUserLayout(): "
2080: + sQuery);
2081: int nextStructId;
2082: rs = stmt.executeQuery(sQuery);
2083: try {
2084: rs.next();
2085: nextStructId = rs.getInt(1);
2086: } finally {
2087: rs.close();
2088: }
2089:
2090: int realNextStructId = 0;
2091:
2092: if (realUserId != userId) {
2093: // But never make the existing value SMALLER, change it only to make it LARGER
2094: // (so, get existing value)
2095: sQuery = "SELECT NEXT_STRUCT_ID FROM UP_USER WHERE USER_ID="
2096: + realUserId;
2097: if (log.isDebugEnabled())
2098: log
2099: .debug("RDBMUserLayoutStore::getUserLayout(): "
2100: + sQuery);
2101: rs = stmt.executeQuery(sQuery);
2102: try {
2103: rs.next();
2104: realNextStructId = rs.getInt(1);
2105: } finally {
2106: rs.close();
2107: }
2108: }
2109:
2110: if (nextStructId > realNextStructId) {
2111: sQuery = "UPDATE UP_USER SET NEXT_STRUCT_ID="
2112: + nextStructId + " WHERE USER_ID="
2113: + realUserId;
2114: if (log.isDebugEnabled())
2115: log
2116: .debug("RDBMUserLayoutStore::getUserLayout(): "
2117: + sQuery);
2118: stmt.executeUpdate(sQuery);
2119: }
2120:
2121: RDBMServices.commit(con); // Make sure it appears in the store
2122: }
2123:
2124: int firstStructId = -1;
2125:
2126: //Flags to enable a default layout lookup if it's needed
2127: boolean foundLayout = false;
2128: boolean triedDefault = false;
2129:
2130: //This loop is used to ensure a layout is found for a user. It tries
2131: //looking up the layout for the current userID. If one isn't found
2132: //the userID is replaced with the template user ID for this user and
2133: //the layout is searched for again. This loop should only ever loop once.
2134: do {
2135: String sQuery = "SELECT INIT_STRUCT_ID FROM UP_USER_LAYOUT WHERE USER_ID="
2136: + userId + " AND LAYOUT_ID = " + layoutId;
2137: if (log.isDebugEnabled())
2138: log
2139: .debug("RDBMUserLayoutStore::getUserLayout(): "
2140: + sQuery);
2141: rs = stmt.executeQuery(sQuery);
2142: try {
2143: if (rs.next()) {
2144: firstStructId = rs.getInt(1);
2145: } else {
2146: throw new Exception(
2147: "RDBMUserLayoutStore::getUserLayout(): No INIT_STRUCT_ID in UP_USER_LAYOUT for "
2148: + userId
2149: + " and LAYOUT_ID "
2150: + layoutId);
2151: }
2152: } finally {
2153: rs.close();
2154: }
2155:
2156: String sql;
2157: if (localeAware) {
2158: // This needs to be changed to get the localized strings
2159: sql = "SELECT ULS.STRUCT_ID,ULS.NEXT_STRUCT_ID,ULS.CHLD_STRUCT_ID,ULS.CHAN_ID,ULS.NAME,ULS.TYPE,ULS.HIDDEN,"
2160: + "ULS.UNREMOVABLE,ULS.IMMUTABLE";
2161: } else {
2162: sql = "SELECT ULS.STRUCT_ID,ULS.NEXT_STRUCT_ID,ULS.CHLD_STRUCT_ID,ULS.CHAN_ID,ULS.NAME,ULS.TYPE,ULS.HIDDEN,"
2163: + "ULS.UNREMOVABLE,ULS.IMMUTABLE";
2164: }
2165: if (RDBMServices.getDbMetaData()
2166: .supportsOuterJoins()) {
2167: sql += ",USP.STRUCT_PARM_NM,USP.STRUCT_PARM_VAL FROM "
2168: + RDBMServices.getDbMetaData()
2169: .getJoinQuery().getQuery(
2170: "layout");
2171: } else {
2172: sql += " FROM UP_LAYOUT_STRUCT ULS WHERE ";
2173: }
2174: sql += " ULS.USER_ID=" + userId
2175: + " AND ULS.LAYOUT_ID=" + layoutId
2176: + " ORDER BY ULS.STRUCT_ID";
2177: if (log.isDebugEnabled())
2178: log
2179: .debug("RDBMUserLayoutStore::getUserLayout(): "
2180: + sql);
2181: rs = stmt.executeQuery(sql);
2182:
2183: //check for rows in the result set
2184: foundLayout = rs.next();
2185:
2186: if (!foundLayout && !triedDefault
2187: && userId == realUserId) {
2188: //If we didn't find any rows and we haven't tried the default user yet
2189: triedDefault = true;
2190: rs.close();
2191:
2192: //Get the default user ID and layout ID
2193: sQuery = "SELECT USER_DFLT_USR_ID, USER_DFLT_LAY_ID FROM UP_USER WHERE USER_ID="
2194: + userId;
2195: if (log.isDebugEnabled())
2196: log
2197: .debug("RDBMUserLayoutStore::getUserLayout(): "
2198: + sQuery);
2199: rs = stmt.executeQuery(sQuery);
2200: try {
2201: rs.next();
2202: userId = rs.getInt(1);
2203: layoutId = rs.getInt(2);
2204: } finally {
2205: rs.close();
2206: }
2207: } else {
2208: //We tried the default or actually found a layout
2209: break;
2210: }
2211: } while (!foundLayout);
2212:
2213: HashMap layoutStructure = new HashMap();
2214: StringBuffer structChanIds = new StringBuffer();
2215:
2216: try {
2217: int lastStructId = 0;
2218: LayoutStructure ls = null;
2219: String sepChar = "";
2220: if (foundLayout) {
2221: int structId = rs.getInt(1);
2222: // Result Set returns 0 by default if structId was null
2223: // Except if you are using poolman 2.0.4 in which case you get -1 back
2224: if (rs.wasNull()) {
2225: structId = 0;
2226: }
2227: readLayout: while (true) {
2228: if (DEBUG > 1)
2229: System.err
2230: .println("Found layout structureID "
2231: + structId);
2232:
2233: int nextId = rs.getInt(2);
2234: if (rs.wasNull()) {
2235: nextId = 0;
2236: }
2237: int childId = rs.getInt(3);
2238: if (rs.wasNull()) {
2239: childId = 0;
2240: }
2241: int chanId = rs.getInt(4);
2242: if (rs.wasNull()) {
2243: chanId = 0;
2244: }
2245: String temp5 = rs.getString(5); // Some JDBC drivers require columns accessed in order
2246: String temp6 = rs.getString(6); // Access 5 and 6 now, save till needed.
2247:
2248: // uPortal i18n
2249: int name_index, value_index;
2250: if (localeAware) {
2251: ls = new LayoutStructure(structId,
2252: nextId, childId, chanId, rs
2253: .getString(7), rs
2254: .getString(8), rs
2255: .getString(9),
2256: localeManager.getLocales()[0]
2257: .toString());
2258: name_index = 10;
2259: value_index = 11;
2260: } else {
2261: ls = new LayoutStructure(structId,
2262: nextId, childId, chanId, rs
2263: .getString(7), rs
2264: .getString(8), rs
2265: .getString(9));
2266: name_index = 10;
2267: value_index = 11;
2268: }
2269: layoutStructure.put(new Integer(structId),
2270: ls);
2271: lastStructId = structId;
2272: if (!ls.isChannel()) {
2273: ls.addFolderData(temp5, temp6); // Plug in saved column values
2274: }
2275: if (RDBMServices.getDbMetaData()
2276: .supportsOuterJoins()) {
2277: do {
2278: String name = rs
2279: .getString(name_index);
2280: String value = rs
2281: .getString(value_index); // Oracle JDBC requires us to do this for longs
2282: if (name != null) { // may not be there because of the join
2283: ls.addParameter(name, value);
2284: }
2285: if (!rs.next()) {
2286: break readLayout;
2287: }
2288: structId = rs.getInt(1);
2289: if (rs.wasNull()) {
2290: structId = 0;
2291: }
2292: } while (structId == lastStructId);
2293: } else { // Do second SELECT later on for structure parameters
2294: if (ls.isChannel()) {
2295: structChanIds.append(sepChar
2296: + ls.getChanId());
2297: sepChar = ",";
2298: }
2299: if (rs.next()) {
2300: structId = rs.getInt(1);
2301: if (rs.wasNull()) {
2302: structId = 0;
2303: }
2304: } else {
2305: break readLayout;
2306: }
2307: }
2308: } // while
2309: }
2310: } finally {
2311: rs.close();
2312: }
2313:
2314: if (!RDBMServices.getDbMetaData().supportsOuterJoins()) { // Pick up structure parameters
2315: // first, get the struct ids for the channels
2316: String sql = "SELECT STRUCT_ID FROM UP_LAYOUT_STRUCT WHERE USER_ID="
2317: + userId
2318: + " AND LAYOUT_ID="
2319: + layoutId
2320: + " AND CHAN_ID IN ("
2321: + structChanIds.toString()
2322: + ") ORDER BY STRUCT_ID";
2323:
2324: if (log.isDebugEnabled())
2325: log
2326: .debug("RDBMUserLayoutStore::getUserLayout(): "
2327: + sql);
2328: StringBuffer structIdsSB = new StringBuffer("");
2329: String sep = "";
2330: rs = stmt.executeQuery(sql);
2331: try {
2332: // use the results to build a correct list of struct ids to look for
2333: while (rs.next()) {
2334: structIdsSB.append(sep + rs.getString(1));
2335: sep = ",";
2336: }// while
2337: } finally {
2338: rs.close();
2339: } // be a good doobie
2340:
2341: sql = "SELECT STRUCT_ID, STRUCT_PARM_NM,STRUCT_PARM_VAL FROM UP_LAYOUT_PARAM WHERE USER_ID="
2342: + userId
2343: + " AND LAYOUT_ID="
2344: + layoutId
2345: + " AND STRUCT_ID IN ("
2346: + structIdsSB.toString()
2347: + ") ORDER BY STRUCT_ID";
2348: if (log.isDebugEnabled())
2349: log
2350: .debug("RDBMUserLayoutStore::getUserLayout(): "
2351: + sql);
2352: rs = stmt.executeQuery(sql);
2353: try {
2354: if (rs.next()) {
2355: int structId = rs.getInt(1);
2356: readParm: while (true) {
2357: LayoutStructure ls = (LayoutStructure) layoutStructure
2358: .get(new Integer(structId));
2359: int lastStructId = structId;
2360: do {
2361: ls.addParameter(rs.getString(2), rs
2362: .getString(3));
2363: if (!rs.next()) {
2364: break readParm;
2365: }
2366: } while ((structId = rs.getInt(1)) == lastStructId);
2367: }
2368: }
2369: } finally {
2370: rs.close();
2371: }
2372: }
2373:
2374: if (layoutStructure.size() > 0) { // We have a layout to work with
2375: createLayout(layoutStructure, doc, root,
2376: firstStructId);
2377: layoutStructure.clear();
2378:
2379: if (log.isDebugEnabled()) {
2380: long stopTime = System.currentTimeMillis();
2381: log
2382: .debug("RDBMUserLayoutStore::getUserLayout(): Layout document for user "
2383: + userId
2384: + " took "
2385: + (stopTime - startTime)
2386: + " milliseconds to create");
2387: }
2388:
2389: doc.appendChild(root);
2390:
2391: if (DEBUG > 1) {
2392: System.err.println("--> created document");
2393: dumpDoc(doc, "");
2394: System.err.println("<--");
2395: }
2396: }
2397: } finally {
2398: stmt.close();
2399: insertStmt.close();
2400: }
2401: return doc;
2402: } finally {
2403: RDBMServices.releaseConnection(con);
2404: }
2405: }
2406:
2407: public UserProfile getUserProfileById(IPerson person, int profileId)
2408: throws Exception {
2409: int userId = person.getID();
2410: Connection con = RDBMServices.getConnection();
2411: try {
2412: Statement stmt = con.createStatement();
2413: try {
2414: String sQuery = "SELECT USER_ID, PROFILE_ID, PROFILE_NAME, DESCRIPTION, LAYOUT_ID, STRUCTURE_SS_ID, THEME_SS_ID FROM UP_USER_PROFILE WHERE USER_ID="
2415: + userId + " AND PROFILE_ID=" + profileId;
2416: if (log.isDebugEnabled())
2417: log
2418: .debug("RDBMUserLayoutStore::getUserProfileById(): "
2419: + sQuery);
2420: ResultSet rs = stmt.executeQuery(sQuery);
2421: try {
2422: if (rs.next()) {
2423: String temp3 = rs.getString(3);
2424: String temp4 = rs.getString(4);
2425: int layoutId = rs.getInt(5);
2426: if (rs.wasNull()) {
2427: layoutId = 0;
2428: }
2429: int structSsId = rs.getInt(6);
2430: if (rs.wasNull()) {
2431: structSsId = 0;
2432: }
2433: int themeSsId = rs.getInt(7);
2434: if (rs.wasNull()) {
2435: themeSsId = 0;
2436: }
2437: UserProfile userProfile = new UserProfile(
2438: profileId, temp3, temp4, layoutId,
2439: structSsId, themeSsId);
2440: userProfile.setLocaleManager(new LocaleManager(
2441: person));
2442: return userProfile;
2443: } else {
2444: throw new Exception(
2445: "Unable to find User Profile for user "
2446: + userId + " and profile "
2447: + profileId);
2448: }
2449: } finally {
2450: rs.close();
2451: }
2452: } finally {
2453: stmt.close();
2454: }
2455: } finally {
2456: RDBMServices.releaseConnection(con);
2457: }
2458: }
2459:
2460: public Hashtable getUserProfileList(IPerson person)
2461: throws Exception {
2462: int userId = person.getID();
2463:
2464: Hashtable pv = new Hashtable();
2465: Connection con = RDBMServices.getConnection();
2466: try {
2467: Statement stmt = con.createStatement();
2468: try {
2469: String sQuery = "SELECT USER_ID, PROFILE_ID, PROFILE_NAME, DESCRIPTION, LAYOUT_ID, STRUCTURE_SS_ID, THEME_SS_ID FROM UP_USER_PROFILE WHERE USER_ID="
2470: + userId;
2471: if (log.isDebugEnabled())
2472: log
2473: .debug("RDBMUserLayoutStore::getUserProfileList(): "
2474: + sQuery);
2475: ResultSet rs = stmt.executeQuery(sQuery);
2476: try {
2477: while (rs.next()) {
2478: int layoutId = rs.getInt(5);
2479: if (rs.wasNull()) {
2480: layoutId = 0;
2481: }
2482: int structSsId = rs.getInt(6);
2483: if (rs.wasNull()) {
2484: structSsId = 0;
2485: }
2486: int themeSsId = rs.getInt(7);
2487: if (rs.wasNull()) {
2488: themeSsId = 0;
2489: }
2490:
2491: UserProfile upl = new UserProfile(rs.getInt(2),
2492: rs.getString(3), rs.getString(4),
2493: layoutId, structSsId, themeSsId);
2494: pv.put(new Integer(upl.getProfileId()), upl);
2495: }
2496: } finally {
2497: rs.close();
2498: }
2499: } finally {
2500: stmt.close();
2501: }
2502: } finally {
2503: RDBMServices.releaseConnection(con);
2504: }
2505: return pv;
2506: }
2507:
2508: /**
2509: * Remove (with cleanup) a structure stylesheet channel attribute
2510: * @param stylesheetId id of the structure stylesheet
2511: * @param pName name of the attribute
2512: * @param con active database connection
2513: */
2514: private void removeStructureChannelAttribute(int stylesheetId,
2515: String pName, Connection con) throws java.sql.SQLException {
2516: Statement stmt = con.createStatement();
2517:
2518: try {
2519: String sQuery = "DELETE FROM UP_SS_STRUCT_PAR WHERE SS_ID="
2520: + stylesheetId + " AND TYPE=3 AND PARAM_NAME='"
2521: + pName + "'";
2522: if (log.isDebugEnabled())
2523: log
2524: .debug("RDBMUserLayoutStore::removeStructureChannelAttribute() : "
2525: + sQuery);
2526: stmt.executeQuery(sQuery);
2527: // clean up user preference tables
2528: sQuery = "DELETE FROM UP_SS_USER_ATTS WHERE SS_ID="
2529: + stylesheetId
2530: + " AND SS_TYPE=1 AND PARAM_TYPE=3 AND PARAM_NAME='"
2531: + pName + "'";
2532: if (log.isDebugEnabled())
2533: log
2534: .debug("RDBMUserLayoutStore::removeStructureChannelAttribute() : "
2535: + sQuery);
2536: stmt.executeQuery(sQuery);
2537: } finally {
2538: stmt.close();
2539: }
2540: }
2541:
2542: /**
2543: * Remove (with cleanup) a structure stylesheet folder attribute
2544: * @param stylesheetId id of the structure stylesheet
2545: * @param pName name of the attribute
2546: * @param con active database connection
2547: */
2548: private void removeStructureFolderAttribute(int stylesheetId,
2549: String pName, Connection con) throws java.sql.SQLException {
2550: Statement stmt = con.createStatement();
2551: try {
2552: String sQuery = "DELETE FROM UP_SS_STRUCT_PAR WHERE SS_ID="
2553: + stylesheetId + " AND TYPE=2 AND PARAM_NAME='"
2554: + pName + "'";
2555: if (log.isDebugEnabled())
2556: log
2557: .debug("RDBMUserLayoutStore::removeStructureFolderAttribute() : "
2558: + sQuery);
2559: stmt.executeQuery(sQuery);
2560: // clean up user preference tables
2561: sQuery = "DELETE FROM UP_SS_USER_ATTS WHERE SS_ID="
2562: + stylesheetId
2563: + " AND SS_TYPE=1 AND PARAM_TYPE=2 AND PARAM_NAME='"
2564: + pName + "'";
2565: if (log.isDebugEnabled())
2566: log
2567: .debug("RDBMUserLayoutStore::removeStructureFolderAttribute() : "
2568: + sQuery);
2569: stmt.executeQuery(sQuery);
2570: } finally {
2571: stmt.close();
2572: }
2573: }
2574:
2575: public void removeStructureStylesheetDescription(int stylesheetId)
2576: throws Exception {
2577: Connection con = RDBMServices.getConnection();
2578: try {
2579: Statement stmt = con.createStatement();
2580: try {
2581: // detele all associated theme stylesheets
2582: String sQuery = "SELECT SS_ID FROM UP_SS_THEME WHERE STRUCT_SS_ID="
2583: + stylesheetId;
2584: if (log.isDebugEnabled())
2585: log
2586: .debug("RDBMUserLayoutStore::removeStructureStylesheetDescription() : "
2587: + sQuery);
2588: ResultSet rs = stmt.executeQuery(sQuery);
2589: try {
2590: while (rs.next()) {
2591: removeThemeStylesheetDescription(rs
2592: .getInt("SS_ID"));
2593: }
2594: } finally {
2595: rs.close();
2596: }
2597: sQuery = "DELETE FROM UP_SS_STRUCT WHERE SS_ID="
2598: + stylesheetId;
2599: if (log.isDebugEnabled())
2600: log
2601: .debug("RDBMUserLayoutStore::removeStructureStylesheetDescription() : "
2602: + sQuery);
2603: stmt.executeUpdate(sQuery);
2604: // delete params
2605: sQuery = "DELETE FROM UP_SS_STRUCT_PAR WHERE SS_ID="
2606: + stylesheetId;
2607: if (log.isDebugEnabled())
2608: log
2609: .debug("RDBMUserLayoutStore::removeStructureStylesheetDescription() : "
2610: + sQuery);
2611: stmt.executeUpdate(sQuery);
2612: RDBMServices.commit(con);
2613: } catch (Exception e) {
2614: // Roll back the transaction
2615: RDBMServices.rollback(con);
2616: throw e;
2617: } finally {
2618: stmt.close();
2619: }
2620: } finally {
2621: RDBMServices.releaseConnection(con);
2622: }
2623: }
2624:
2625: /**
2626: * Remove (with cleanup) a structure stylesheet param
2627: * @param stylesheetId id of the structure stylesheet
2628: * @param pName name of the parameter
2629: * @param con active database connection
2630: */
2631: private void removeStructureStylesheetParam(int stylesheetId,
2632: String pName, Connection con) throws java.sql.SQLException {
2633: Statement stmt = con.createStatement();
2634: try {
2635: String sQuery = "DELETE FROM UP_SS_STRUCT_PAR WHERE SS_ID="
2636: + stylesheetId + " AND TYPE=1 AND PARAM_NAME='"
2637: + pName + "'";
2638: if (log.isDebugEnabled())
2639: log
2640: .debug("RDBMUserLayoutStore::removeStructureStylesheetParam() : "
2641: + sQuery);
2642: stmt.executeQuery(sQuery);
2643: // clean up user preference tables
2644: sQuery = "DELETE FROM UP_SS_USER_PARM WHERE SS_ID="
2645: + stylesheetId
2646: + " AND SS_TYPE=1 AND PARAM_TYPE=1 AND PARAM_NAME='"
2647: + pName + "'";
2648: if (log.isDebugEnabled())
2649: log
2650: .debug("RDBMUserLayoutStore::removeStructureStylesheetParam() : "
2651: + sQuery);
2652: stmt.executeQuery(sQuery);
2653: } finally {
2654: stmt.close();
2655: }
2656: }
2657:
2658: /**
2659: * Remove (with cleanup) a theme stylesheet channel attribute
2660: * @param stylesheetId id of the theme stylesheet
2661: * @param pName name of the attribute
2662: * @param con active database connection
2663: */
2664: private void removeThemeChannelAttribute(int stylesheetId,
2665: String pName, Connection con) throws java.sql.SQLException {
2666: Statement stmt = con.createStatement();
2667: try {
2668: String sQuery = "DELETE FROM UP_SS_THEME_PARM WHERE SS_ID="
2669: + stylesheetId + " AND TYPE=3 AND PARAM_NAME='"
2670: + pName + "'";
2671: if (log.isDebugEnabled())
2672: log
2673: .debug("RDBMUserLayoutStore::removeThemeChannelAttribute() : "
2674: + sQuery);
2675: stmt.executeQuery(sQuery);
2676: // clean up user preference tables
2677: sQuery = "DELETE FROM UP_SS_USER_ATTS WHERE SS_ID="
2678: + stylesheetId
2679: + " AND SS_TYPE=2 AND PARAM_TYPE=3 AND PARAM_NAME='"
2680: + pName + "'";
2681: if (log.isDebugEnabled())
2682: log
2683: .debug("RDBMUserLayoutStore::removeThemeStylesheetParam() : "
2684: + sQuery);
2685: stmt.executeQuery(sQuery);
2686: } finally {
2687: stmt.close();
2688: }
2689: }
2690:
2691: public void removeThemeStylesheetDescription(int stylesheetId)
2692: throws Exception {
2693: Connection con = RDBMServices.getConnection();
2694: try {
2695: Statement stmt = con.createStatement();
2696: try {
2697: String sQuery = "DELETE FROM UP_SS_THEME WHERE SS_ID="
2698: + stylesheetId;
2699: if (log.isDebugEnabled())
2700: log
2701: .debug("RDBMUserLayoutStore::removeThemeStylesheetDescription() : "
2702: + sQuery);
2703: stmt.executeUpdate(sQuery);
2704: // delete params
2705: sQuery = "DELETE FROM UP_SS_THEME_PARM WHERE SS_ID="
2706: + stylesheetId;
2707: if (log.isDebugEnabled())
2708: log
2709: .debug("RDBMUserLayoutStore::removeThemeStylesheetDescription() : "
2710: + sQuery);
2711: stmt.executeUpdate(sQuery);
2712:
2713: // nuke all of the profiles that use it
2714: sQuery = "SELECT USER_ID,PROFILE_ID FROM UP_USER_PROFILE WHERE THEME_SS_ID="
2715: + stylesheetId;
2716: if (log.isDebugEnabled())
2717: log
2718: .debug("RDBMUserLayoutStore::removeThemeStylesheetDescription() : "
2719: + sQuery);
2720: ResultSet rs = stmt.executeQuery(sQuery);
2721: try {
2722: while (rs.next()) {
2723: deleteUserProfile(rs.getInt("USER_ID"), rs
2724: .getInt("PROFILE_ID"));
2725: }
2726: } finally {
2727: rs.close();
2728: }
2729:
2730: // clean up user preferences - directly ( in case of loose params )
2731: sQuery = "DELETE FROM UP_SS_USER_PARM WHERE SS_ID="
2732: + stylesheetId + " AND SS_TYPE=2";
2733: if (log.isDebugEnabled())
2734: log
2735: .debug("RDBMUserLayoutStore::removeThemeStylesheetDescription() : "
2736: + sQuery);
2737: stmt.executeUpdate(sQuery);
2738: sQuery = "DELETE FROM UP_SS_USER_ATTS WHERE SS_ID="
2739: + stylesheetId + " AND SS_TYPE=2";
2740: if (log.isDebugEnabled())
2741: log
2742: .debug("RDBMUserLayoutStore::removeThemeStylesheetDescription() : "
2743: + sQuery);
2744: stmt.executeUpdate(sQuery);
2745:
2746: RDBMServices.commit(con);
2747: } catch (Exception e) {
2748: // Roll back the transaction
2749: RDBMServices.rollback(con);
2750: throw e;
2751: } finally {
2752: stmt.close();
2753: }
2754: } finally {
2755: RDBMServices.releaseConnection(con);
2756: }
2757: }
2758:
2759: /**
2760: * Remove (with cleanup) a theme stylesheet param
2761: * @param stylesheetId id of the theme stylesheet
2762: * @param pName name of the parameter
2763: * @param con active database connection
2764: */
2765: private void removeThemeStylesheetParam(int stylesheetId,
2766: String pName, Connection con) throws java.sql.SQLException {
2767: Statement stmt = con.createStatement();
2768: try {
2769: String sQuery = "DELETE FROM UP_SS_THEME_PARM WHERE SS_ID="
2770: + stylesheetId + " AND TYPE=1 AND PARAM_NAME='"
2771: + pName + "'";
2772: if (log.isDebugEnabled())
2773: log
2774: .debug("RDBMUserLayoutStore::removeThemeStylesheetParam() : "
2775: + sQuery);
2776: stmt.executeQuery(sQuery);
2777: // clean up user preference tables
2778: sQuery = "DELETE FROM UP_SS_USER_PARM WHERE SS_ID="
2779: + stylesheetId
2780: + " AND SS_TYPE=2 AND PARAM_TYPE=1 AND PARAM_NAME='"
2781: + pName + "'";
2782: if (log.isDebugEnabled())
2783: log
2784: .debug("RDBMUserLayoutStore::removeThemeStylesheetParam() : "
2785: + sQuery);
2786: stmt.executeQuery(sQuery);
2787: } finally {
2788: stmt.close();
2789: }
2790: }
2791:
2792: protected Element getStructure(Document doc, LayoutStructure ls)
2793: throws Exception {
2794: Element structure = null;
2795: if (ls.isChannel()) {
2796: ChannelDefinition channelDef = crs.getChannelDefinition(ls
2797: .getChanId());
2798: if (channelDef != null
2799: && channelApproved(channelDef.getApprovalDate())) {
2800: if (localeAware) {
2801: channelDef.setLocale(ls.getLocale()); // for i18n by Shoji
2802: }
2803: structure = channelDef.getDocument(doc, channelPrefix
2804: + ls.getStructId());
2805: } else {
2806: // Create an error channel if channel is missing or not approved
2807: ChannelDefinition cd = new ChannelDefinition(ls
2808: .getChanId());
2809: cd.setTitle("Missing channel");
2810: cd.setName("Missing channel");
2811: cd.setTimeout(20000);
2812: String missingChannel = "Unknown";
2813: if (channelDef != null) {
2814: missingChannel = channelDef.getName();
2815: }
2816: structure = cd
2817: .getDocument(
2818: doc,
2819: channelPrefix + ls.getStructId(),
2820: "The '"
2821: + missingChannel
2822: + "' channel is no longer available. Please remove it from your layout.",
2823: ErrorCode.CHANNEL_MISSING_EXCEPTION
2824: .getCode());
2825: }
2826: } else {
2827: structure = doc.createElement("folder");
2828:
2829: structure.setAttribute("ID", folderPrefix
2830: + ls.getStructId());
2831: structure.setIdAttribute("ID", true);
2832:
2833: structure.setAttribute("name", ls.getName());
2834: structure.setAttribute("type", (ls.getType() != null ? ls
2835: .getType() : "regular"));
2836: }
2837:
2838: structure.setAttribute("hidden", (ls.isHidden() ? "true"
2839: : "false"));
2840: structure.setAttribute("immutable", (ls.isImmutable() ? "true"
2841: : "false"));
2842: structure.setAttribute("unremovable",
2843: (ls.isUnremovable() ? "true" : "false"));
2844: if (localeAware) {
2845: structure.setAttribute("locale", ls.getLocale()); // for i18n by Shoji
2846: }
2847:
2848: for (Iterator layoutStructureParamIterator = ls.getParameters()
2849: .iterator(); layoutStructureParamIterator.hasNext();) {
2850:
2851: StructureParameter sp = (StructureParameter) layoutStructureParamIterator
2852: .next();
2853:
2854: if (!ls.isChannel()) { // Folder
2855: structure.setAttribute(sp.getName(), sp.getValue());
2856: } else { // Channel
2857: NodeList nodeListParameters = structure
2858: .getElementsByTagName("parameter");
2859: for (int j = 0; j < nodeListParameters.getLength(); j++) {
2860: Element parmElement = (Element) nodeListParameters
2861: .item(j);
2862: NamedNodeMap nm = parmElement.getAttributes();
2863:
2864: String nodeName = nm.getNamedItem("name")
2865: .getNodeValue();
2866: if (nodeName.equals(sp.getName())) {
2867: Node override = nm.getNamedItem("override");
2868: if (override != null
2869: && override.getNodeValue()
2870: .equals("yes")) {
2871: Node valueNode = nm.getNamedItem("value");
2872: valueNode.setNodeValue(sp.getValue());
2873: }
2874: }
2875: }
2876: }
2877: }
2878: return structure;
2879: }
2880:
2881: protected int saveStructure(Node node,
2882: PreparedStatement structStmt, PreparedStatement parmStmt)
2883: throws Exception {
2884: if (node == null || node.getNodeName().equals("parameter")) { // No more or parameter node
2885: return 0;
2886: }
2887: Element structure = (Element) node;
2888: int saveStructId = Integer.parseInt(structure
2889: .getAttribute("ID").substring(1));
2890: int nextStructId = 0;
2891: int childStructId = 0;
2892: String sQuery;
2893: if (DEBUG > 0) {
2894: log.debug("-->" + node.getNodeName() + "@" + saveStructId);
2895: }
2896: if (node.hasChildNodes()) {
2897: childStructId = saveStructure(node.getFirstChild(),
2898: structStmt, parmStmt);
2899: }
2900: nextStructId = saveStructure(node.getNextSibling(), structStmt,
2901: parmStmt);
2902: structStmt.clearParameters();
2903: structStmt.setInt(1, saveStructId);
2904: structStmt.setInt(2, nextStructId);
2905: structStmt.setInt(3, childStructId);
2906:
2907: String externalId = structure.getAttribute("external_id");
2908: if (externalId != null && externalId.trim().length() > 0) {
2909: Integer eID = new Integer(externalId);
2910: structStmt.setInt(4, eID.intValue());
2911: } else {
2912: structStmt.setNull(4, java.sql.Types.NUMERIC);
2913:
2914: }
2915: if (node.getNodeName().equals("channel")) {
2916: int chanId = Integer.parseInt(node.getAttributes()
2917: .getNamedItem("chanID").getNodeValue());
2918: structStmt.setInt(5, chanId);
2919: structStmt.setNull(6, java.sql.Types.VARCHAR);
2920: } else {
2921: structStmt.setNull(5, java.sql.Types.NUMERIC);
2922: structStmt.setString(6, structure.getAttribute("name"));
2923: }
2924: String structType = structure.getAttribute("type");
2925: structStmt.setString(7, structType);
2926: structStmt.setString(8, RDBMServices.dbFlag(xmlBool(structure
2927: .getAttribute("hidden"))));
2928: structStmt.setString(9, RDBMServices.dbFlag(xmlBool(structure
2929: .getAttribute("immutable"))));
2930: structStmt.setString(10, RDBMServices.dbFlag(xmlBool(structure
2931: .getAttribute("unremovable"))));
2932:
2933: if (log.isDebugEnabled())
2934: log.debug("RDBMUserLayoutStore::saveStructure(): "
2935: + structStmt);
2936: structStmt.executeUpdate();
2937:
2938: NodeList parameters = node.getChildNodes();
2939: if (parameters != null) {
2940: for (int i = 0; i < parameters.getLength(); i++) {
2941: if (parameters.item(i).getNodeName()
2942: .equals("parameter")) {
2943: Element parmElement = (Element) parameters.item(i);
2944: NamedNodeMap nm = parmElement.getAttributes();
2945: String nodeName = nm.getNamedItem("name")
2946: .getNodeValue();
2947: String nodeValue = nm.getNamedItem("value")
2948: .getNodeValue();
2949:
2950: Node override = nm.getNamedItem("override");
2951: if (DEBUG > 0) {
2952: System.err.println(nodeName + "=" + nodeValue);
2953: }
2954: if (override == null
2955: || !override.getNodeValue().equals("yes")) {
2956: if (DEBUG > 0)
2957: System.err
2958: .println("Not saving channel defined parameter value "
2959: + nodeName);
2960: } else {
2961: if (nodeValue != null && nodeValue.length() > 0) {
2962: parmStmt.clearParameters();
2963: parmStmt.setInt(1, saveStructId);
2964: parmStmt.setString(2, nodeName);
2965: parmStmt.setString(3, nodeValue);
2966: if (log.isDebugEnabled())
2967: log
2968: .debug("RDBMUserLayoutStore::saveStructure(): "
2969: + parmStmt);
2970: parmStmt.executeUpdate();
2971: }
2972: }
2973: }
2974: }
2975: }
2976: return saveStructId;
2977: }
2978:
2979: public void setStructureStylesheetUserPreferences(IPerson person,
2980: int profileId, StructureStylesheetUserPreferences ssup)
2981: throws Exception {
2982: int userId = person.getID();
2983: Connection con = null;
2984: Statement stmt = null;
2985: ResultSet rs = null;
2986:
2987: try {
2988: con = RDBMServices.getConnection();
2989:
2990: // Set autocommit false for the connection
2991: int stylesheetId = ssup.getStylesheetId();
2992: RDBMServices.setAutoCommit(con, false);
2993: stmt = con.createStatement();
2994: try {
2995: // write out params
2996: for (Enumeration e = ssup.getParameterValues().keys(); e
2997: .hasMoreElements();) {
2998: String pName = (String) e.nextElement();
2999: // see if the parameter was already there
3000: String sQuery = "SELECT PARAM_VAL FROM UP_SS_USER_PARM WHERE USER_ID="
3001: + userId
3002: + " AND PROFILE_ID="
3003: + profileId
3004: + " AND SS_ID="
3005: + stylesheetId
3006: + " AND SS_TYPE=1 AND PARAM_NAME='"
3007: + pName
3008: + "'";
3009: if (log.isDebugEnabled())
3010: log
3011: .debug("RDBMUserLayoutStore::setStructureStylesheetUserPreferences(): "
3012: + sQuery);
3013: rs = stmt.executeQuery(sQuery);
3014: if (rs.next()) {
3015: // update
3016: sQuery = "UPDATE UP_SS_USER_PARM SET PARAM_VAL='"
3017: + ssup.getParameterValue(pName)
3018: + "' WHERE USER_ID="
3019: + userId
3020: + " AND PROFILE_ID="
3021: + profileId
3022: + " AND SS_ID="
3023: + stylesheetId
3024: + " AND SS_TYPE=1 AND PARAM_NAME='"
3025: + pName + "'";
3026: } else {
3027: // insert
3028: sQuery = "INSERT INTO UP_SS_USER_PARM (USER_ID,PROFILE_ID,SS_ID,SS_TYPE,PARAM_NAME,PARAM_VAL) VALUES ("
3029: + userId
3030: + ","
3031: + profileId
3032: + ","
3033: + stylesheetId
3034: + ",1,'"
3035: + pName
3036: + "','"
3037: + ssup.getParameterValue(pName) + "')";
3038: }
3039: rs.close();
3040: rs = null;
3041: if (log.isDebugEnabled())
3042: log
3043: .debug("RDBMUserLayoutStore::setStructureStylesheetUserPreferences(): "
3044: + sQuery);
3045: stmt.executeUpdate(sQuery);
3046: }
3047:
3048: String sSql = "DELETE FROM UP_SS_USER_ATTS WHERE USER_ID="
3049: + userId;
3050: if (log.isDebugEnabled())
3051: log
3052: .debug("RDBMUserLayoutStore::setStructureStylesheetUserPreferences(): "
3053: + sSql);
3054: stmt.executeUpdate(sSql);
3055:
3056: // write out folder attributes
3057: for (Enumeration e = ssup.getFolders(); e
3058: .hasMoreElements();) {
3059: String folderId = (String) e.nextElement();
3060: for (Enumeration attre = ssup
3061: .getFolderAttributeNames(); attre
3062: .hasMoreElements();) {
3063: String pName = (String) attre.nextElement();
3064: String pValue = ssup
3065: .getDefinedFolderAttributeValue(
3066: folderId, pName);
3067: if (pValue != null) {
3068: // store user preferences
3069: String sQuery = "SELECT PARAM_VAL FROM UP_SS_USER_ATTS WHERE USER_ID="
3070: + userId
3071: + " AND PROFILE_ID="
3072: + profileId
3073: + " AND SS_ID="
3074: + stylesheetId
3075: + " AND SS_TYPE=1 AND STRUCT_ID="
3076: + folderId.substring(1)
3077: + " AND PARAM_NAME='"
3078: + pName
3079: + "' AND PARAM_TYPE=2";
3080: if (log.isDebugEnabled())
3081: log
3082: .debug("RDBMUserLayoutStore::setStructureStylesheetUserPreferences(): "
3083: + sQuery);
3084: rs = stmt.executeQuery(sQuery);
3085: if (rs.next()) {
3086: // update
3087: sQuery = "UPDATE UP_SS_USER_ATTS SET PARAM_VAL='"
3088: + pValue
3089: + "' WHERE USER_ID="
3090: + userId
3091: + " AND PROFILE_ID="
3092: + profileId
3093: + " AND SS_ID="
3094: + stylesheetId
3095: + " AND SS_TYPE=1 AND STRUCT_ID="
3096: + folderId.substring(1)
3097: + " AND PARAM_NAME='"
3098: + pName
3099: + "' AND PARAM_TYPE=2";
3100: } else {
3101: // insert
3102: sQuery = "INSERT INTO UP_SS_USER_ATTS (USER_ID,PROFILE_ID,SS_ID,SS_TYPE,STRUCT_ID,PARAM_NAME,PARAM_TYPE,PARAM_VAL) VALUES ("
3103: + userId
3104: + ","
3105: + profileId
3106: + ","
3107: + stylesheetId
3108: + ",1,"
3109: + folderId.substring(1)
3110: + ",'"
3111: + pName
3112: + "',2,'"
3113: + pValue
3114: + "')";
3115: }
3116: rs.close();
3117: rs = null;
3118: if (log.isDebugEnabled())
3119: log
3120: .debug("RDBMUserLayoutStore::setStructureStylesheetUserPreferences(): "
3121: + sQuery);
3122: stmt.executeUpdate(sQuery);
3123: }
3124: }
3125: }
3126: // write out channel attributes
3127: for (Enumeration e = ssup.getChannels(); e
3128: .hasMoreElements();) {
3129: String channelId = (String) e.nextElement();
3130: for (Enumeration attre = ssup
3131: .getChannelAttributeNames(); attre
3132: .hasMoreElements();) {
3133: String pName = (String) attre.nextElement();
3134: String pValue = ssup
3135: .getDefinedChannelAttributeValue(
3136: channelId, pName);
3137: if (pValue != null) {
3138: // store user preferences
3139: String sQuery = "SELECT PARAM_VAL FROM UP_SS_USER_ATTS WHERE USER_ID="
3140: + userId
3141: + " AND PROFILE_ID="
3142: + profileId
3143: + " AND SS_ID="
3144: + stylesheetId
3145: + " AND SS_TYPE=1 AND STRUCT_ID="
3146: + channelId.substring(1)
3147: + " AND PARAM_NAME='"
3148: + pName
3149: + "' AND PARAM_TYPE=3";
3150: if (log.isDebugEnabled())
3151: log
3152: .debug("RDBMUserLayoutStore::setStructureStylesheetUserPreferences(): "
3153: + sQuery);
3154: rs = stmt.executeQuery(sQuery);
3155: if (rs.next()) {
3156: // update
3157: sQuery = "UPDATE UP_SS_USER_ATTS SET PARAM_VAL='"
3158: + pValue
3159: + "' WHERE USER_ID="
3160: + userId
3161: + " AND PROFILE_ID="
3162: + profileId
3163: + " AND SS_ID="
3164: + stylesheetId
3165: + " AND SS_TYPE=1 AND STRUCT_ID="
3166: + channelId.substring(1)
3167: + " AND PARAM_NAME='"
3168: + pName
3169: + "' AND PARAM_TYPE=3";
3170: } else {
3171: // insert
3172: sQuery = "INSERT INTO UP_SS_USER_ATTS (USER_ID,PROFILE_ID,SS_ID,SS_TYPE,STRUCT_ID,PARAM_NAME,PARAM_TYPE,PARAM_VAL) VALUES ("
3173: + userId
3174: + ","
3175: + profileId
3176: + ","
3177: + stylesheetId
3178: + ",1,"
3179: + channelId.substring(1)
3180: + ",'"
3181: + pName
3182: + "',3,'"
3183: + pValue
3184: + "')";
3185: }
3186: rs.close();
3187: rs = null;
3188: if (log.isDebugEnabled())
3189: log
3190: .debug("RDBMUserLayoutStore::setStructureStylesheetUserPreferences(): "
3191: + sQuery);
3192: stmt.executeUpdate(sQuery);
3193: }
3194: }
3195: }
3196: // Commit the transaction
3197: RDBMServices.commit(con);
3198: } catch (Exception e) {
3199: // Roll back the transaction
3200: RDBMServices.rollback(con);
3201: throw e;
3202: }
3203: } finally {
3204: RDBMServices.closeResultSet(rs);
3205: RDBMServices.closeStatement(stmt);
3206: RDBMServices.releaseConnection(con);
3207: }
3208: }
3209:
3210: public void setThemeStylesheetUserPreferences(IPerson person,
3211: int profileId, ThemeStylesheetUserPreferences tsup)
3212: throws Exception {
3213: int userId = person.getID();
3214: Connection con = RDBMServices.getConnection();
3215: try {
3216: // Set autocommit false for the connection
3217: int stylesheetId = tsup.getStylesheetId();
3218: RDBMServices.setAutoCommit(con, false);
3219: Statement stmt = con.createStatement();
3220: try {
3221: int layoutId = this .getLayoutID(userId, profileId);
3222:
3223: boolean firstLayout = false;
3224: if (layoutId == 0) { // First personal layout for this user/profile
3225: layoutId = 1;
3226: firstLayout = true;
3227: }
3228:
3229: //Check to see if the user has a matching layout
3230: String sSql = "SELECT * FROM UP_USER_LAYOUT WHERE USER_ID="
3231: + userId + " AND LAYOUT_ID=" + layoutId;
3232: if (log.isDebugEnabled())
3233: log.debug("RDBMUserLayoutStore::setUserLayout(): "
3234: + sSql);
3235: ResultSet rs = stmt.executeQuery(sSql);
3236:
3237: try {
3238: //If not the default user is found and the layout rows from the
3239: //default user are copied for the current user.
3240: if (!rs.next()) {
3241: int defaultUserId;
3242:
3243: String sQuery = "SELECT USER_DFLT_USR_ID FROM UP_USER WHERE USER_ID="
3244: + userId;
3245: if (log.isDebugEnabled())
3246: log
3247: .debug("RDBMUserLayoutStore::setUserLayout(): "
3248: + sQuery);
3249:
3250: ResultSet rs2 = stmt.executeQuery(sQuery);
3251: try {
3252: rs2.next();
3253: defaultUserId = rs2.getInt(1);
3254: } finally {
3255: rs2.close();
3256: }
3257:
3258: // Add to UP_USER_LAYOUT
3259: sQuery = "SELECT USER_ID,LAYOUT_ID,LAYOUT_TITLE,INIT_STRUCT_ID FROM UP_USER_LAYOUT WHERE USER_ID="
3260: + defaultUserId;
3261: if (log.isDebugEnabled())
3262: log
3263: .debug("RDBMUserLayoutStore::setUserLayout(): "
3264: + sQuery);
3265:
3266: rs2 = stmt.executeQuery(sQuery);
3267:
3268: try {
3269: while (rs2.next()) {
3270: sQuery = "INSERT INTO UP_USER_LAYOUT "
3271: + "(USER_ID,LAYOUT_ID,LAYOUT_TITLE,INIT_STRUCT_ID) "
3272: + "VALUES(" + userId + ","
3273: + rs2.getInt("LAYOUT_ID") + ","
3274: + "'"
3275: + rs2.getString("LAYOUT_TITLE")
3276: + "',"
3277: + rs2.getInt("INIT_STRUCT_ID")
3278: + ")";
3279:
3280: if (log.isDebugEnabled())
3281: log
3282: .debug("RDBMUserLayoutStore::setUserLayout(): "
3283: + sQuery);
3284: stmt.executeUpdate(sQuery);
3285: }
3286: } finally {
3287: rs2.close();
3288: }
3289: }
3290: } finally {
3291: rs.close();
3292: }
3293:
3294: // write out params
3295: for (Enumeration e = tsup.getParameterValues().keys(); e
3296: .hasMoreElements();) {
3297: String pName = (String) e.nextElement();
3298: // see if the parameter was already there
3299: String sQuery = "SELECT PARAM_VAL FROM UP_SS_USER_PARM WHERE USER_ID="
3300: + userId
3301: + " AND PROFILE_ID="
3302: + profileId
3303: + " AND SS_ID="
3304: + stylesheetId
3305: + " AND SS_TYPE=2 AND PARAM_NAME='"
3306: + pName
3307: + "'";
3308: if (log.isDebugEnabled())
3309: log
3310: .debug("RDBMUserLayoutStore::setThemeStylesheetUserPreferences(): "
3311: + sQuery);
3312: rs = stmt.executeQuery(sQuery);
3313: if (rs.next()) {
3314: // update
3315: sQuery = "UPDATE UP_SS_USER_PARM SET PARAM_VAL='"
3316: + tsup.getParameterValue(pName)
3317: + "' WHERE USER_ID="
3318: + userId
3319: + " AND PROFILE_ID="
3320: + profileId
3321: + " AND SS_ID="
3322: + stylesheetId
3323: + " AND SS_TYPE=2 AND PARAM_NAME='"
3324: + pName + "'";
3325: } else {
3326: // insert
3327: sQuery = "INSERT INTO UP_SS_USER_PARM (USER_ID,PROFILE_ID,SS_ID,SS_TYPE,PARAM_NAME,PARAM_VAL) VALUES ("
3328: + userId
3329: + ","
3330: + profileId
3331: + ","
3332: + stylesheetId
3333: + ",2,'"
3334: + pName
3335: + "','"
3336: + tsup.getParameterValue(pName) + "')";
3337: }
3338: if (log.isDebugEnabled())
3339: log
3340: .debug("RDBMUserLayoutStore::setThemeStylesheetUserPreferences(): "
3341: + sQuery);
3342: stmt.executeUpdate(sQuery);
3343: }
3344: // write out channel attributes
3345: for (Enumeration e = tsup.getChannels(); e
3346: .hasMoreElements();) {
3347: String channelId = (String) e.nextElement();
3348: for (Enumeration attre = tsup
3349: .getChannelAttributeNames(); attre
3350: .hasMoreElements();) {
3351: String pName = (String) attre.nextElement();
3352: String pValue = tsup
3353: .getDefinedChannelAttributeValue(
3354: channelId, pName);
3355: if (pValue != null) {
3356: // store user preferences
3357: String sQuery = "SELECT PARAM_VAL FROM UP_SS_USER_ATTS WHERE USER_ID="
3358: + userId
3359: + " AND PROFILE_ID="
3360: + profileId
3361: + " AND SS_ID="
3362: + stylesheetId
3363: + " AND SS_TYPE=2 AND STRUCT_ID="
3364: + channelId.substring(1)
3365: + " AND PARAM_NAME='"
3366: + pName
3367: + "' AND PARAM_TYPE=3";
3368: if (log.isDebugEnabled())
3369: log
3370: .debug("RDBMUserLayoutStore::setThemeStylesheetUserPreferences(): "
3371: + sQuery);
3372: rs = stmt.executeQuery(sQuery);
3373: if (rs.next()) {
3374: // update
3375: sQuery = "UPDATE UP_SS_USER_ATTS SET PARAM_VAL='"
3376: + pValue
3377: + "' WHERE USER_ID="
3378: + userId
3379: + " AND PROFILE_ID="
3380: + profileId
3381: + " AND SS_ID="
3382: + stylesheetId
3383: + " AND SS_TYPE=2 AND STRUCT_ID="
3384: + channelId.substring(1)
3385: + " AND PARAM_NAME='"
3386: + pName
3387: + "' AND PARAM_TYPE=3";
3388: } else {
3389: // insert
3390: sQuery = "INSERT INTO UP_SS_USER_ATTS (USER_ID,PROFILE_ID,SS_ID,SS_TYPE,STRUCT_ID,PARAM_NAME,PARAM_TYPE,PARAM_VAL) VALUES ("
3391: + userId
3392: + ","
3393: + profileId
3394: + ","
3395: + stylesheetId
3396: + ",2,"
3397: + channelId.substring(1)
3398: + ",'"
3399: + pName
3400: + "',3,'"
3401: + pValue
3402: + "')";
3403: }
3404: if (log.isDebugEnabled())
3405: log
3406: .debug("RDBMUserLayoutStore::setThemeStylesheetUserPreferences(): "
3407: + sQuery);
3408: stmt.executeUpdate(sQuery);
3409: }
3410: }
3411: }
3412:
3413: if (firstLayout) {
3414:
3415: int defaultUserId;
3416: int defaultLayoutId;
3417: // Have to copy some of data over from the default user
3418: String sQuery = "SELECT USER_DFLT_USR_ID,USER_DFLT_LAY_ID FROM UP_USER WHERE USER_ID="
3419: + userId;
3420: if (log.isDebugEnabled())
3421: log
3422: .debug("RDBMUserLayoutStore::setUserLayout(): "
3423: + sQuery);
3424: rs = stmt.executeQuery(sQuery);
3425: try {
3426: rs.next();
3427: defaultUserId = rs.getInt(1);
3428: defaultLayoutId = rs.getInt(2);
3429: } finally {
3430: rs.close();
3431: }
3432:
3433: sQuery = "UPDATE UP_USER_PROFILE SET LAYOUT_ID=1 WHERE USER_ID="
3434: + userId + " AND PROFILE_ID=" + profileId;
3435: if (log.isDebugEnabled())
3436: log
3437: .debug("RDBMUserLayoutStore::setUserLayout(): "
3438: + sQuery);
3439: stmt.executeUpdate(sQuery);
3440: }
3441:
3442: // Commit the transaction
3443: RDBMServices.commit(con);
3444: } catch (Exception e) {
3445: // Roll back the transaction
3446: RDBMServices.rollback(con);
3447: throw e;
3448: } finally {
3449: stmt.close();
3450: }
3451: } finally {
3452: RDBMServices.releaseConnection(con);
3453: }
3454: }
3455:
3456: public void setUserBrowserMapping(IPerson person, String userAgent,
3457: int profileId) throws Exception {
3458: if (userAgent.length() > 255) {
3459: userAgent = userAgent.substring(0, 254);
3460: log
3461: .debug("userAgent trimmed to 255 characters. userAgent: "
3462: + userAgent);
3463: }
3464: int userId = person.getID();
3465: Connection con = RDBMServices.getConnection();
3466: try {
3467: // Set autocommit false for the connection
3468: RDBMServices.setAutoCommit(con, false);
3469: // remove the old mapping and add the new one
3470: try {
3471: PreparedStatement ps = null;
3472: try {
3473: ps = con
3474: .prepareStatement("DELETE FROM UP_USER_UA_MAP WHERE USER_ID=? AND USER_AGENT=?");
3475: ps.setInt(1, userId);
3476: ps.setString(2, userAgent);
3477: ps.executeUpdate();
3478: } finally {
3479: try {
3480: ps.close();
3481: } catch (Exception e) {
3482: //ignore
3483: }
3484: }
3485: try {
3486: log.debug("writing to UP_USER_UA_MAP: userId: "
3487: + userId + ", userAgent: " + userAgent
3488: + ", profileId: " + profileId);
3489: ps = con
3490: .prepareStatement("INSERT INTO UP_USER_UA_MAP (USER_ID,USER_AGENT,PROFILE_ID) VALUES (?,?,?)");
3491: ps.setInt(1, userId);
3492: ps.setString(2, userAgent);
3493: ps.setInt(3, profileId);
3494: ps.executeUpdate();
3495: } finally {
3496: try {
3497: ps.close();
3498: } catch (Exception e) {
3499: //ignore
3500: }
3501: }
3502: // Commit the transaction
3503: RDBMServices.commit(con);
3504: } catch (Exception e) {
3505: // Roll back the transaction
3506: RDBMServices.rollback(con);
3507: throw new PortalException("userId: " + userId
3508: + ", userAgent: " + userAgent + ", profileId: "
3509: + profileId, e);
3510: }
3511: } finally {
3512: RDBMServices.releaseConnection(con);
3513: }
3514: }
3515:
3516: /**
3517: * Save the user layout.
3518: * @param person
3519: * @param profile
3520: * @param layoutXML
3521: * @throws Exception
3522: */
3523: public void setUserLayout(IPerson person, UserProfile profile,
3524: Document layoutXML, boolean channelsAdded) throws Exception {
3525: long startTime = System.currentTimeMillis();
3526: int userId = person.getID();
3527: int profileId = profile.getProfileId();
3528: int layoutId = 0;
3529: ResultSet rs;
3530: Connection con = RDBMServices.getConnection();
3531: try {
3532: RDBMServices.setAutoCommit(con, false); // Need an atomic update here
3533:
3534: // Eventually we want to be able to just get layoutId from the
3535: // profile, but because of the template user layouts we have to do this for now ...
3536: layoutId = this .getLayoutID(userId, profileId);
3537:
3538: boolean firstLayout = false;
3539: if (layoutId == 0) {
3540: // First personal layout for this user/profile
3541: layoutId = 1;
3542: firstLayout = true;
3543: }
3544:
3545: String sql = "DELETE FROM UP_LAYOUT_PARAM WHERE USER_ID=? AND LAYOUT_ID=?";
3546: PreparedStatement pstmt = con.prepareStatement(sql);
3547: try {
3548: pstmt.clearParameters();
3549: pstmt.setInt(1, userId);
3550: pstmt.setInt(2, layoutId);
3551: if (log.isDebugEnabled())
3552: log.debug(sql);
3553: pstmt.executeUpdate();
3554: } finally {
3555: pstmt.close();
3556: }
3557:
3558: sql = "DELETE FROM UP_LAYOUT_STRUCT WHERE USER_ID=? AND LAYOUT_ID=?";
3559: pstmt = con.prepareStatement(sql);
3560: try {
3561: pstmt.clearParameters();
3562: pstmt.setInt(1, userId);
3563: pstmt.setInt(2, layoutId);
3564: if (log.isDebugEnabled())
3565: log.debug(sql);
3566: pstmt.executeUpdate();
3567: } finally {
3568: pstmt.close();
3569: }
3570:
3571: PreparedStatement structStmt = con
3572: .prepareStatement("INSERT INTO UP_LAYOUT_STRUCT "
3573: + "(USER_ID, LAYOUT_ID, STRUCT_ID, NEXT_STRUCT_ID, CHLD_STRUCT_ID,EXTERNAL_ID,CHAN_ID,NAME,TYPE,HIDDEN,IMMUTABLE,UNREMOVABLE) "
3574: + "VALUES (" + userId + "," + layoutId
3575: + ",?,?,?,?,?,?,?,?,?,?)");
3576:
3577: PreparedStatement parmStmt = con
3578: .prepareStatement("INSERT INTO UP_LAYOUT_PARAM "
3579: + "(USER_ID, LAYOUT_ID, STRUCT_ID, STRUCT_PARM_NM, STRUCT_PARM_VAL) "
3580: + "VALUES (" + userId + "," + layoutId
3581: + ",?,?,?)");
3582:
3583: int firstStructId;
3584: try {
3585: firstStructId = saveStructure(layoutXML.getFirstChild()
3586: .getFirstChild(), structStmt, parmStmt);
3587: } finally {
3588: structStmt.close();
3589: parmStmt.close();
3590: }
3591:
3592: //Check to see if the user has a matching layout
3593: sql = "SELECT * FROM UP_USER_LAYOUT WHERE USER_ID=? AND LAYOUT_ID=?";
3594: pstmt = con.prepareStatement(sql);
3595: try {
3596: pstmt.clearParameters();
3597: pstmt.setInt(1, userId);
3598: pstmt.setInt(2, layoutId);
3599: if (log.isDebugEnabled())
3600: log.debug(sql);
3601: rs = pstmt.executeQuery();
3602:
3603: try {
3604: if (!rs.next()) {
3605: // If not, the default user is found and the layout rows from the default user are copied for the current user.
3606: int defaultUserId;
3607:
3608: sql = "SELECT USER_DFLT_USR_ID FROM UP_USER WHERE USER_ID=?";
3609: PreparedStatement pstmt2 = con
3610: .prepareStatement(sql);
3611: try {
3612: pstmt2.clearParameters();
3613: pstmt2.setInt(1, userId);
3614: if (log.isDebugEnabled())
3615: log.debug(sql);
3616: ResultSet rs2 = null;
3617: try {
3618: rs2 = pstmt2.executeQuery();
3619: rs2.next();
3620: defaultUserId = rs2.getInt(1);
3621: } finally {
3622: rs2.close();
3623: }
3624: } finally {
3625: pstmt2.close();
3626: }
3627:
3628: // Add to UP_USER_LAYOUT
3629: sql = "SELECT USER_ID,LAYOUT_ID,LAYOUT_TITLE,INIT_STRUCT_ID FROM UP_USER_LAYOUT WHERE USER_ID=?";
3630: pstmt2 = con.prepareStatement(sql);
3631: try {
3632: pstmt2.clearParameters();
3633: pstmt2.setInt(1, defaultUserId);
3634: if (log.isDebugEnabled())
3635: log.debug(sql);
3636: ResultSet rs2 = pstmt2.executeQuery();
3637: try {
3638: while (rs2.next()) {
3639: sql = "INSERT INTO UP_USER_LAYOUT (USER_ID, LAYOUT_ID, LAYOUT_TITLE, INIT_STRUCT_ID) VALUES (?,?,?,?)";
3640: PreparedStatement pstmt3 = con
3641: .prepareStatement(sql);
3642: try {
3643: pstmt3.clearParameters();
3644: pstmt3.setInt(1, userId);
3645: pstmt3.setInt(2, rs2
3646: .getInt("LAYOUT_ID"));
3647: pstmt3
3648: .setString(
3649: 3,
3650: rs2
3651: .getString("LAYOUT_TITLE"));
3652: pstmt3
3653: .setInt(
3654: 4,
3655: rs2
3656: .getInt("INIT_STRUCT_ID"));
3657: if (log.isDebugEnabled())
3658: log.debug(sql);
3659: pstmt3.executeUpdate();
3660: } finally {
3661: pstmt3.close();
3662: }
3663: }
3664: } finally {
3665: rs2.close();
3666: }
3667: } finally {
3668: pstmt2.close();
3669: }
3670:
3671: }
3672: } finally {
3673: rs.close();
3674: }
3675: } finally {
3676: pstmt.close();
3677: }
3678:
3679: //Update the users layout with the correct inital structure ID
3680: sql = "UPDATE UP_USER_LAYOUT SET INIT_STRUCT_ID=? WHERE USER_ID=? AND LAYOUT_ID=?";
3681: pstmt = con.prepareStatement(sql);
3682: try {
3683: pstmt.clearParameters();
3684: pstmt.setInt(1, firstStructId);
3685: pstmt.setInt(2, userId);
3686: pstmt.setInt(3, layoutId);
3687: if (log.isDebugEnabled())
3688: log.debug(sql);
3689: pstmt.executeUpdate();
3690: } finally {
3691: pstmt.close();
3692: }
3693:
3694: // Update the last time the user saw the list of available channels
3695: if (channelsAdded) {
3696: sql = "UPDATE UP_USER SET LST_CHAN_UPDT_DT=? WHERE USER_ID=?";
3697: pstmt = con.prepareStatement(sql);
3698: try {
3699: pstmt.clearParameters();
3700: pstmt.setDate(1, new java.sql.Date(System
3701: .currentTimeMillis()));
3702: pstmt.setInt(2, userId);
3703: log.debug(sql);
3704: pstmt.executeUpdate();
3705: } finally {
3706: pstmt.close();
3707: }
3708: }
3709:
3710: if (firstLayout) {
3711: int defaultUserId;
3712: int defaultLayoutId;
3713: // Have to copy some of data over from the default user
3714: sql = "SELECT USER_DFLT_USR_ID,USER_DFLT_LAY_ID FROM UP_USER WHERE USER_ID=?";
3715: pstmt = con.prepareStatement(sql);
3716: try {
3717: pstmt.clearParameters();
3718: pstmt.setInt(1, userId);
3719: log.debug(sql);
3720: rs = pstmt.executeQuery();
3721: try {
3722: rs.next();
3723: defaultUserId = rs.getInt(1);
3724: defaultLayoutId = rs.getInt(2);
3725: } finally {
3726: rs.close();
3727: }
3728: } finally {
3729: pstmt.close();
3730: }
3731:
3732: sql = "UPDATE UP_USER_PROFILE SET LAYOUT_ID=1 WHERE USER_ID=? AND PROFILE_ID=?";
3733: pstmt = con.prepareStatement(sql);
3734: try {
3735: pstmt.clearParameters();
3736: pstmt.setInt(1, userId);
3737: pstmt.setInt(2, profileId);
3738: log.debug(sql);
3739: pstmt.executeUpdate();
3740: } finally {
3741: pstmt.close();
3742: }
3743:
3744: // Insert row(s) into up_ss_user_parm
3745: sql = "SELECT USER_ID, PROFILE_ID, SS_ID, SS_TYPE, PARAM_NAME, PARAM_VAL FROM UP_SS_USER_PARM WHERE USER_ID=?";
3746: pstmt = con.prepareStatement(sql);
3747: try {
3748: pstmt.clearParameters();
3749: pstmt.setInt(1, defaultUserId);
3750: log.debug(sql);
3751: rs = pstmt.executeQuery();
3752: try {
3753: while (rs.next()) {
3754: sql = "INSERT INTO UP_SS_USER_PARM (USER_ID, PROFILE_ID, SS_ID, SS_TYPE, PARAM_NAME, PARAM_VAL) VALUES(?,?,?,?,?,?)";
3755: PreparedStatement pstmt2 = con
3756: .prepareStatement(sql);
3757: try {
3758: pstmt2.clearParameters();
3759: pstmt2.setInt(1, userId);
3760: pstmt2.setInt(2, rs
3761: .getInt("PROFILE_ID"));
3762: pstmt2.setInt(3, rs.getInt("SS_ID"));
3763: pstmt2.setInt(4, rs.getInt("SS_TYPE"));
3764: pstmt2.setString(5, rs
3765: .getString("PARAM_NAME"));
3766: pstmt2.setString(6, rs
3767: .getString("PARAM_VAL"));
3768: log.debug(sql);
3769: pstmt2.executeUpdate();
3770: } finally {
3771: pstmt2.close();
3772: }
3773: }
3774: } finally {
3775: rs.close();
3776: }
3777: } finally {
3778: pstmt.close();
3779: }
3780: }
3781: RDBMServices.commit(con);
3782: } catch (Exception e) {
3783: RDBMServices.rollback(con);
3784: throw e;
3785: } finally {
3786: RDBMServices.releaseConnection(con);
3787: }
3788: if (log.isDebugEnabled()) {
3789: long stopTime = System.currentTimeMillis();
3790: log
3791: .debug("RDBMUserLayoutStore::setUserLayout(): Layout document for user "
3792: + userId
3793: + " took "
3794: + (stopTime - startTime)
3795: + " milliseconds to save");
3796: }
3797: }
3798:
3799: /**
3800: * Updates an existing structure stylesheet description with a new one. Old stylesheet
3801: * description is found based on the Id provided in the parameter structure.
3802: * @param ssd new stylesheet description
3803: */
3804: public void updateStructureStylesheetDescription(
3805: StructureStylesheetDescription ssd) throws Exception {
3806: Connection con = RDBMServices.getConnection();
3807: try {
3808: // Set autocommit false for the connection
3809: RDBMServices.setAutoCommit(con, false);
3810: Statement stmt = con.createStatement();
3811: try {
3812: int stylesheetId = ssd.getId();
3813: String sQuery = "UPDATE UP_SS_STRUCT SET SS_NAME='"
3814: + ssd.getStylesheetName() + "',SS_URI='"
3815: + ssd.getStylesheetURI()
3816: + "',SS_DESCRIPTION_URI='"
3817: + ssd.getStylesheetDescriptionURI()
3818: + "',SS_DESCRIPTION_TEXT='"
3819: + ssd.getStylesheetWordDescription()
3820: + "' WHERE SS_ID=" + stylesheetId;
3821: if (log.isDebugEnabled())
3822: log
3823: .debug("RDBMUserLayoutStore::updateStructureStylesheetDescription() : "
3824: + sQuery);
3825: stmt.executeUpdate(sQuery);
3826: // first, see what was there before
3827: HashSet oparams = new HashSet();
3828: HashSet ofattrs = new HashSet();
3829: HashSet ocattrs = new HashSet();
3830: sQuery = "SELECT PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE FROM UP_SS_STRUCT_PAR WHERE SS_ID="
3831: + stylesheetId;
3832: if (log.isDebugEnabled())
3833: log
3834: .debug("RDBMUserLayoutStore::updateStructureStylesheetDescription() : "
3835: + sQuery);
3836: Statement stmtOld = con.createStatement();
3837: ResultSet rsOld = stmtOld.executeQuery(sQuery);
3838: try {
3839: while (rsOld.next()) {
3840: int type = rsOld.getInt("TYPE");
3841: if (type == 1) {
3842: // stylesheet param
3843: String pName = rsOld
3844: .getString("PARAM_NAME");
3845: oparams.add(pName);
3846: if (!ssd.containsParameterName(pName)) {
3847: // delete param
3848: removeStructureStylesheetParam(
3849: stylesheetId, pName, con);
3850: } else {
3851: // update param
3852: sQuery = "UPDATE UP_SS_STRUCT_PAR SET PARAM_DEFAULT_VAL='"
3853: + ssd
3854: .getStylesheetParameterDefaultValue(pName)
3855: + "',PARAM_DESCRIPT='"
3856: + ssd
3857: .getStylesheetParameterWordDescription(pName)
3858: + "' WHERE SS_ID="
3859: + stylesheetId
3860: + " AND PARAM_NAME='"
3861: + pName
3862: + "' AND TYPE=1";
3863: if (log.isDebugEnabled())
3864: log
3865: .debug("RDBMUserLayoutStore::updateStructureStylesheetDescription() : "
3866: + sQuery);
3867: stmt.executeUpdate(sQuery);
3868: }
3869: } else if (type == 2) {
3870: // folder attribute
3871: String pName = rsOld
3872: .getString("PARAM_NAME");
3873: ofattrs.add(pName);
3874: if (!ssd.containsFolderAttribute(pName)) {
3875: // delete folder attribute
3876: removeStructureFolderAttribute(
3877: stylesheetId, pName, con);
3878: } else {
3879: // update folder attribute
3880: sQuery = "UPDATE UP_SS_STRUCT_PAR SET PARAM_DEFAULT_VAL='"
3881: + ssd
3882: .getFolderAttributeDefaultValue(pName)
3883: + "',PARAM_DESCRIPT='"
3884: + ssd
3885: .getFolderAttributeWordDescription(pName)
3886: + "' WHERE SS_ID="
3887: + stylesheetId
3888: + " AND PARAM_NAME='"
3889: + pName
3890: + "'AND TYPE=2";
3891: if (log.isDebugEnabled())
3892: log
3893: .debug("RDBMUserLayoutStore::updateStructureStylesheetDescription() : "
3894: + sQuery);
3895: stmt.executeUpdate(sQuery);
3896: }
3897: } else if (type == 3) {
3898: // channel attribute
3899: String pName = rsOld
3900: .getString("PARAM_NAME");
3901: ocattrs.add(pName);
3902: if (!ssd.containsChannelAttribute(pName)) {
3903: // delete channel attribute
3904: removeStructureChannelAttribute(
3905: stylesheetId, pName, con);
3906: } else {
3907: // update channel attribute
3908: sQuery = "UPDATE UP_SS_STRUCT_PAR SET PARAM_DEFAULT_VAL='"
3909: + ssd
3910: .getChannelAttributeDefaultValue(pName)
3911: + "',PARAM_DESCRIPT='"
3912: + ssd
3913: .getChannelAttributeWordDescription(pName)
3914: + "' WHERE SS_ID="
3915: + stylesheetId
3916: + " AND PARAM_NAME='"
3917: + pName
3918: + "' AND TYPE=3";
3919: if (log.isDebugEnabled())
3920: log
3921: .debug("RDBMUserLayoutStore::updateStructureStylesheetDescription() : "
3922: + sQuery);
3923: stmt.executeUpdate(sQuery);
3924: }
3925: } else {
3926: if (log.isDebugEnabled())
3927: log
3928: .debug("RDBMUserLayoutStore::updateStructureStylesheetDescription() : encountered param of unknown type! (stylesheetId="
3929: + stylesheetId
3930: + " param_name=\""
3931: + rsOld
3932: .getString("PARAM_NAME")
3933: + "\" type="
3934: + type
3935: + ").");
3936: }
3937: }
3938: } finally {
3939: rsOld.close();
3940: stmtOld.close();
3941: }
3942: // look for new attributes/parameters
3943: // insert all stylesheet params
3944: for (Enumeration e = ssd.getStylesheetParameterNames(); e
3945: .hasMoreElements();) {
3946: String pName = (String) e.nextElement();
3947: if (!oparams.contains(pName)) {
3948: sQuery = "INSERT INTO UP_SS_STRUCT_PAR (SS_ID,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE) VALUES ("
3949: + stylesheetId
3950: + ",'"
3951: + pName
3952: + "','"
3953: + ssd
3954: .getStylesheetParameterDefaultValue(pName)
3955: + "','"
3956: + ssd
3957: .getStylesheetParameterWordDescription(pName)
3958: + "',1)";
3959: if (log.isDebugEnabled())
3960: log
3961: .debug("RDBMUserLayoutStore::updateStructureStylesheetDescription(): "
3962: + sQuery);
3963: stmt.executeUpdate(sQuery);
3964: }
3965: }
3966: // insert all folder attributes
3967: for (Enumeration e = ssd.getFolderAttributeNames(); e
3968: .hasMoreElements();) {
3969: String pName = (String) e.nextElement();
3970: if (!ofattrs.contains(pName)) {
3971: sQuery = "INSERT INTO UP_SS_STRUCT_PAR (SS_ID,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE) VALUES ("
3972: + stylesheetId
3973: + ",'"
3974: + pName
3975: + "','"
3976: + ssd
3977: .getFolderAttributeDefaultValue(pName)
3978: + "','"
3979: + ssd
3980: .getFolderAttributeWordDescription(pName)
3981: + "',2)";
3982: if (log.isDebugEnabled())
3983: log
3984: .debug("RDBMUserLayoutStore::updateStructureStylesheetDescription(): "
3985: + sQuery);
3986: stmt.executeUpdate(sQuery);
3987: }
3988: }
3989: // insert all channel attributes
3990: for (Enumeration e = ssd.getChannelAttributeNames(); e
3991: .hasMoreElements();) {
3992: String pName = (String) e.nextElement();
3993: if (!ocattrs.contains(pName)) {
3994: sQuery = "INSERT INTO UP_SS_STRUCT_PAR (SS_ID,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE) VALUES ("
3995: + stylesheetId
3996: + ",'"
3997: + pName
3998: + "','"
3999: + ssd
4000: .getChannelAttributeDefaultValue(pName)
4001: + "','"
4002: + ssd
4003: .getChannelAttributeWordDescription(pName)
4004: + "',3)";
4005: if (log.isDebugEnabled())
4006: log
4007: .debug("RDBMUserLayoutStore::updateStructureStylesheetDescription(): "
4008: + sQuery);
4009: stmt.executeUpdate(sQuery);
4010: }
4011: }
4012: // Commit the transaction
4013: RDBMServices.commit(con);
4014: } catch (Exception e) {
4015: // Roll back the transaction
4016: RDBMServices.rollback(con);
4017: throw e;
4018: } finally {
4019: stmt.close();
4020: }
4021: } finally {
4022: RDBMServices.releaseConnection(con);
4023: }
4024: }
4025:
4026: /**
4027: * Updates an existing structure stylesheet description with a new one. Old stylesheet
4028: * description is found based on the Id provided in the parameter structure.
4029: * @param tsd new theme stylesheet description
4030: */
4031: public void updateThemeStylesheetDescription(
4032: ThemeStylesheetDescription tsd) throws Exception {
4033: Connection con = RDBMServices.getConnection();
4034: try {
4035: // Set autocommit false for the connection
4036: RDBMServices.setAutoCommit(con, false);
4037: Statement stmt = con.createStatement();
4038: try {
4039: int stylesheetId = tsd.getId();
4040:
4041: String sQuery = "UPDATE UP_SS_THEME SET SS_NAME='"
4042: + tsd.getStylesheetName() + "',SS_URI='"
4043: + tsd.getStylesheetURI()
4044: + "',SS_DESCRIPTION_URI='"
4045: + tsd.getStylesheetDescriptionURI()
4046: + "',SS_DESCRIPTION_TEXT='"
4047: + tsd.getStylesheetWordDescription()
4048: + "',SAMPLE_ICON_URI='"
4049: + tsd.getSampleIconURI() + "',SAMPLE_URI='"
4050: + tsd.getSamplePictureURI() + "',MIME_TYPE='"
4051: + tsd.getMimeType() + "',DEVICE_TYPE='"
4052: + tsd.getDeviceType() + "',SERIALIZER_NAME='"
4053: + tsd.getSerializerName()
4054: + "',UP_MODULE_CLASS='"
4055: + tsd.getCustomUserPreferencesManagerClass()
4056: + "' WHERE SS_ID=" + stylesheetId;
4057: if (log.isDebugEnabled())
4058: log
4059: .debug("RDBMUserLayoutStore::updateThemeStylesheetDescription() : "
4060: + sQuery);
4061: stmt.executeUpdate(sQuery);
4062: // first, see what was there before
4063: HashSet oparams = new HashSet();
4064: HashSet ocattrs = new HashSet();
4065: sQuery = "SELECT PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE FROM UP_SS_THEME_PARM WHERE SS_ID="
4066: + stylesheetId;
4067: if (log.isDebugEnabled())
4068: log
4069: .debug("RDBMUserLayoutStore::updateThemeStylesheetDescription() : "
4070: + sQuery);
4071: Statement stmtOld = con.createStatement();
4072: ResultSet rsOld = stmtOld.executeQuery(sQuery);
4073: try {
4074: while (rsOld.next()) {
4075: int type = rsOld.getInt("TYPE");
4076: if (type == 1) {
4077: // stylesheet param
4078: String pName = rsOld
4079: .getString("PARAM_NAME");
4080: oparams.add(pName);
4081: if (!tsd.containsParameterName(pName)) {
4082: // delete param
4083: removeThemeStylesheetParam(
4084: stylesheetId, pName, con);
4085: } else {
4086: // update param
4087: sQuery = "UPDATE UP_SS_THEME_PARM SET PARAM_DEFAULT_VAL='"
4088: + tsd
4089: .getStylesheetParameterDefaultValue(pName)
4090: + "',PARAM_DESCRIPT='"
4091: + tsd
4092: .getStylesheetParameterWordDescription(pName)
4093: + "' WHERE SS_ID="
4094: + stylesheetId
4095: + " AND PARAM_NAME='"
4096: + pName
4097: + "' AND TYPE=1";
4098: if (log.isDebugEnabled())
4099: log
4100: .debug("RDBMUserLayoutStore::updateThemeStylesheetDescription() : "
4101: + sQuery);
4102: stmt.executeUpdate(sQuery);
4103: }
4104: } else if (type == 2) {
4105: if (log.isDebugEnabled())
4106: log
4107: .debug("RDBMUserLayoutStore::updateThemeStylesheetDescription() : encountered a folder attribute specified for a theme stylesheet ! DB is corrupt. (stylesheetId="
4108: + stylesheetId
4109: + " param_name=\""
4110: + rsOld
4111: .getString("PARAM_NAME")
4112: + "\" type="
4113: + type
4114: + ").");
4115: } else if (type == 3) {
4116: // channel attribute
4117: String pName = rsOld
4118: .getString("PARAM_NAME");
4119: ocattrs.add(pName);
4120: if (!tsd.containsChannelAttribute(pName)) {
4121: // delete channel attribute
4122: removeThemeChannelAttribute(
4123: stylesheetId, pName, con);
4124: } else {
4125: // update channel attribute
4126: sQuery = "UPDATE UP_SS_THEME_PARM SET PARAM_DEFAULT_VAL='"
4127: + tsd
4128: .getChannelAttributeDefaultValue(pName)
4129: + "',PARAM_DESCRIPT='"
4130: + tsd
4131: .getChannelAttributeWordDescription(pName)
4132: + "' WHERE SS_ID="
4133: + stylesheetId
4134: + " AND PARAM_NAME='"
4135: + pName
4136: + "' AND TYPE=3";
4137: if (log.isDebugEnabled())
4138: log
4139: .debug("RDBMUserLayoutStore::updateThemeStylesheetDescription() : "
4140: + sQuery);
4141: stmt.executeUpdate(sQuery);
4142: }
4143: } else {
4144: if (log.isDebugEnabled())
4145: log
4146: .debug("RDBMUserLayoutStore::updateThemeStylesheetDescription() : encountered param of unknown type! (stylesheetId="
4147: + stylesheetId
4148: + " param_name=\""
4149: + rsOld
4150: .getString("PARAM_NAME")
4151: + "\" type="
4152: + type
4153: + ").");
4154: }
4155: }
4156: } finally {
4157: rsOld.close();
4158: stmtOld.close();
4159: }
4160: // look for new attributes/parameters
4161: // insert all stylesheet params
4162: for (Enumeration e = tsd.getStylesheetParameterNames(); e
4163: .hasMoreElements();) {
4164: String pName = (String) e.nextElement();
4165: if (!oparams.contains(pName)) {
4166: sQuery = "INSERT INTO UP_SS_THEME_PARM (SS_ID,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE) VALUES ("
4167: + stylesheetId
4168: + ",'"
4169: + pName
4170: + "','"
4171: + tsd
4172: .getStylesheetParameterDefaultValue(pName)
4173: + "','"
4174: + tsd
4175: .getStylesheetParameterWordDescription(pName)
4176: + "',1)";
4177: if (log.isDebugEnabled())
4178: log
4179: .debug("RDBMUserLayoutStore::updateThemeStylesheetDescription(): "
4180: + sQuery);
4181: stmt.executeUpdate(sQuery);
4182: }
4183: }
4184: // insert all channel attributes
4185: for (Enumeration e = tsd.getChannelAttributeNames(); e
4186: .hasMoreElements();) {
4187: String pName = (String) e.nextElement();
4188: if (!ocattrs.contains(pName)) {
4189: sQuery = "INSERT INTO UP_SS_THEME_PARM (SS_ID,PARAM_NAME,PARAM_DEFAULT_VAL,PARAM_DESCRIPT,TYPE) VALUES ("
4190: + stylesheetId
4191: + ",'"
4192: + pName
4193: + "','"
4194: + tsd
4195: .getChannelAttributeDefaultValue(pName)
4196: + "','"
4197: + tsd
4198: .getChannelAttributeWordDescription(pName)
4199: + "',3)";
4200: if (log.isDebugEnabled())
4201: log
4202: .debug("RDBMUserLayoutStore::updateThemeStylesheetDescription(): "
4203: + sQuery);
4204: stmt.executeUpdate(sQuery);
4205: }
4206: }
4207: // Commit the transaction
4208: RDBMServices.commit(con);
4209: } catch (Exception e) {
4210: // Roll back the transaction
4211: RDBMServices.rollback(con);
4212: throw e;
4213: } finally {
4214: stmt.close();
4215: }
4216: } finally {
4217: RDBMServices.releaseConnection(con);
4218: }
4219: }
4220:
4221: public void updateUserProfile(IPerson person, UserProfile profile)
4222: throws Exception {
4223: int userId = person.getID();
4224: Connection con = RDBMServices.getConnection();
4225: try {
4226: Statement stmt = con.createStatement();
4227: try {
4228: String sQuery = "UPDATE UP_USER_PROFILE SET LAYOUT_ID="
4229: + profile.getLayoutId() + ", THEME_SS_ID="
4230: + profile.getThemeStylesheetId()
4231: + ", STRUCTURE_SS_ID="
4232: + profile.getStructureStylesheetId()
4233: + ", DESCRIPTION='"
4234: + profile.getProfileDescription()
4235: + "', PROFILE_NAME='"
4236: + profile.getProfileName()
4237: + "' WHERE USER_ID = " + userId
4238: + " AND PROFILE_ID=" + profile.getProfileId();
4239: if (log.isDebugEnabled())
4240: log
4241: .debug("RDBMUserLayoutStore::updateUserProfile() : "
4242: + sQuery);
4243: stmt.executeUpdate(sQuery);
4244: } finally {
4245: stmt.close();
4246: }
4247: } finally {
4248: RDBMServices.releaseConnection(con);
4249: }
4250: }
4251:
4252: public void setSystemBrowserMapping(String userAgent, int profileId)
4253: throws Exception {
4254: this .setUserBrowserMapping(systemUser, userAgent, profileId);
4255: }
4256:
4257: private int getSystemBrowserMapping(String userAgent)
4258: throws Exception {
4259: return getUserBrowserMapping(systemUser, userAgent);
4260: }
4261:
4262: public UserProfile getUserProfile(IPerson person, String userAgent)
4263: throws Exception {
4264: int profileId = getUserBrowserMapping(person, userAgent);
4265: if (profileId == 0)
4266: return null;
4267: return this .getUserProfileById(person, profileId);
4268: }
4269:
4270: public UserProfile getSystemProfile(String userAgent)
4271: throws Exception {
4272: int profileId = getSystemBrowserMapping(userAgent);
4273: if (profileId == 0)
4274: return null;
4275: UserProfile up = this .getUserProfileById(systemUser, profileId);
4276: up.setSystemProfile(true);
4277: return up;
4278: }
4279:
4280: public UserProfile getSystemProfileById(int profileId)
4281: throws Exception {
4282: UserProfile up = this .getUserProfileById(systemUser, profileId);
4283: up.setSystemProfile(true);
4284: return up;
4285: }
4286:
4287: public Hashtable getSystemProfileList() throws Exception {
4288: Hashtable pl = this .getUserProfileList(systemUser);
4289: for (Enumeration e = pl.elements(); e.hasMoreElements();) {
4290: UserProfile up = (UserProfile) e.nextElement();
4291: up.setSystemProfile(true);
4292: }
4293: return pl;
4294: }
4295:
4296: public void updateSystemProfile(UserProfile profile)
4297: throws Exception {
4298: this .updateUserProfile(systemUser, profile);
4299: }
4300:
4301: public UserProfile addSystemProfile(UserProfile profile)
4302: throws Exception {
4303: return addUserProfile(systemUser, profile);
4304: }
4305:
4306: public void deleteSystemProfile(int profileId) throws Exception {
4307: this .deleteUserProfile(systemUser, profileId);
4308: }
4309:
4310: private class SystemUser implements IPerson {
4311: public void setID(int sID) {
4312: }
4313:
4314: public int getID() {
4315: return 0;
4316: }
4317:
4318: public void setFullName(String sFullName) {
4319: }
4320:
4321: public String getFullName() {
4322: return "uPortal System Account";
4323: }
4324:
4325: public Object getAttribute(String key) {
4326: return null;
4327: }
4328:
4329: public Object[] getAttributeValues(String key) {
4330: return null;
4331: }
4332:
4333: public void setAttribute(String key, Object value) {
4334: }
4335:
4336: public void setAttributes(Map attrs) {
4337: }
4338:
4339: public Enumeration getAttributes() {
4340: return null;
4341: }
4342:
4343: public Enumeration getAttributeNames() {
4344: return null;
4345: }
4346:
4347: public boolean isGuest() {
4348: return (false);
4349: }
4350:
4351: public ISecurityContext getSecurityContext() {
4352: return (null);
4353: }
4354:
4355: public void setSecurityContext(ISecurityContext context) {
4356: }
4357:
4358: public EntityIdentifier getEntityIdentifier() {
4359: return null;
4360: }
4361:
4362: public void setEntityIdentifier(EntityIdentifier ei) {
4363: }
4364: }
4365:
4366: private IPerson systemUser = new SystemUser(); // We should be getting this from the uPortal
4367:
4368: public UserPreferences getUserPreferences(IPerson person,
4369: int profileId) throws Exception {
4370: UserPreferences up = null;
4371: UserProfile profile = this
4372: .getUserProfileById(person, profileId);
4373: if (profile != null) {
4374: up = getUserPreferences(person, profile);
4375: }
4376: return (up);
4377: }
4378:
4379: public UserPreferences getUserPreferences(IPerson person,
4380: UserProfile profile) throws Exception {
4381: int profileId = profile.getProfileId();
4382: UserPreferences up = new UserPreferences(profile);
4383: up
4384: .setStructureStylesheetUserPreferences(getStructureStylesheetUserPreferences(
4385: person, profileId, profile
4386: .getStructureStylesheetId()));
4387: up
4388: .setThemeStylesheetUserPreferences(getThemeStylesheetUserPreferences(
4389: person, profileId, profile
4390: .getThemeStylesheetId()));
4391: return up;
4392: }
4393:
4394: public void putUserPreferences(IPerson person, UserPreferences up)
4395: throws Exception {
4396: // store profile
4397: UserProfile profile = up.getProfile();
4398: this .updateUserProfile(person, profile);
4399: this .setStructureStylesheetUserPreferences(person, profile
4400: .getProfileId(), up
4401: .getStructureStylesheetUserPreferences());
4402: this
4403: .setThemeStylesheetUserPreferences(person, profile
4404: .getProfileId(), up
4405: .getThemeStylesheetUserPreferences());
4406: }
4407:
4408: /**
4409: * Returns the current layout ID for the user and profile. If the profile doesn't exist or the
4410: * layout_id field is null 0 is returned.
4411: *
4412: * @param userId The userId for the profile
4413: * @param profileId The profileId for the profile
4414: * @return The layout_id field or 0 if it does not exist or is null
4415: * @throws SQLException
4416: */
4417: protected int getLayoutID(int userId, int profileId)
4418: throws SQLException {
4419: String query = "SELECT LAYOUT_ID " + "FROM UP_USER_PROFILE "
4420: + "WHERE USER_ID=? AND PROFILE_ID=?";
4421:
4422: Connection con = RDBMServices.getConnection();
4423: int layoutId = 0;
4424:
4425: try {
4426: PreparedStatement pstmt = con.prepareStatement(query);
4427:
4428: try {
4429: if (log.isDebugEnabled())
4430: log
4431: .debug("RDBMUserLayoutStore::getLayoutID(userId="
4432: + userId
4433: + ", profileId="
4434: + profileId + " ): " + query);
4435:
4436: pstmt.setInt(1, userId);
4437: pstmt.setInt(2, profileId);
4438: ResultSet rs = pstmt.executeQuery();
4439:
4440: try {
4441: if (rs.next()) {
4442: layoutId = rs.getInt(1);
4443:
4444: if (rs.wasNull()) {
4445: layoutId = 0;
4446: }
4447: }
4448: } finally {
4449: rs.close();
4450: }
4451: } finally {
4452: pstmt.close();
4453: }
4454: } finally {
4455: RDBMServices.releaseConnection(con);
4456: }
4457:
4458: return layoutId;
4459: }
4460: }
|