001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.properties;
024:
025: import java.sql.Connection;
026: import java.sql.SQLException;
027: import java.util.Collection;
028:
029: import biz.hammurapi.config.Component;
030: import biz.hammurapi.config.ConfigurationException;
031: import biz.hammurapi.config.Context;
032: import biz.hammurapi.sql.DataAccessObject;
033: import biz.hammurapi.sql.IdentityGenerator;
034: import biz.hammurapi.sql.IdentityManager;
035: import biz.hammurapi.sql.IdentityRetriever;
036: import biz.hammurapi.sql.SQLProcessor;
037: import biz.hammurapi.sql.SQLRuntimeException;
038: import biz.hammurapi.web.property.sql.PropertyEngine;
039:
040: /**
041: * Root class for managing property sets
042: * @author Pavel
043: *
044: */
045: public class PropertySetFactory implements DataAccessObject, Component {
046:
047: private SQLProcessor processor;
048: private Object owner;
049: IdentityManager identityManager;
050: private PropertyEngine engine;
051:
052: public void setSQLProcessor(SQLProcessor processor)
053: throws SQLException {
054: this .processor = processor;
055: this .engine = new PropertyEngine(processor);
056: }
057:
058: /**
059: * Creates property set
060: * @param isTransient If this paramter is true then property set returned is not backed by the database.
061: * @return New empty property set
062: */
063: public PropertySet create(boolean isTransient) {
064: if (isTransient) {
065: return new TransientPropertySet();
066: }
067:
068: try {
069: Connection con = processor.getConnection();
070: DbPropertySet dps = new DbPropertySet(true);
071: dps.setDescription("New property set");
072: if (identityManager instanceof IdentityGenerator) {
073: dps.setId(((IdentityGenerator) identityManager)
074: .generate(con, "PROPERTY_SET"));
075: }
076: new PropertyEngine(new SQLProcessor(con, null))
077: .insertPropertySet(dps);
078: if (identityManager instanceof IdentityRetriever) {
079: dps.setId(((IdentityRetriever) identityManager)
080: .retrieve(con));
081: }
082: processor.releaseConnection(con);
083: dps.setSQLProcessor(processor);
084: return dps;
085: } catch (SQLException e) {
086: throw new SQLRuntimeException(e);
087: }
088: }
089:
090: /**
091: * Finds property set by id.
092: * @param id
093: * @return
094: * @throws SQLException
095: */
096: public PersistentPropertySet find(int id) throws SQLException {
097: DbPropertySet ret = (DbPropertySet) engine.getPropertySet(id,
098: DbPropertySet.class);
099: if (ret != null) {
100: ret.setSQLProcessor(processor);
101: }
102: return ret;
103: }
104:
105: /**
106: * Finds property set by owner and name
107: * @param ownerType
108: * @param ownerId
109: * @param setName
110: * @param create If this parameter is true then this method creates property set if one doesn't already exist
111: * @return
112: * @throws SQLException
113: */
114: public PersistentPropertySet find(String ownerType, String ownerId,
115: String setName, boolean create) throws SQLException {
116: DbPropertySet ret = (DbPropertySet) engine
117: .getPropertySetNameEQ(ownerType, ownerId, setName,
118: DbPropertySet.class);
119: if (ret == null) {
120: ret = (DbPropertySet) create(false);
121: ret.setOwnerType(ownerType);
122: ret.setOwnerId(ownerId);
123: ret.setName(setName);
124: ret.update();
125: } else {
126: ret.setSQLProcessor(processor);
127: }
128: return ret;
129: }
130:
131: /**
132: * Finds all property sets for a given owner.
133: * @param ownerType
134: * @param ownerId
135: * @return
136: */
137: public Collection find(String ownerType, String ownerId) {
138: return engine.getPropertySetOwnerEQ(ownerType, ownerId,
139: DbPropertySet.class);
140: }
141:
142: public void setOwner(Object owner) {
143: this .owner = owner;
144: }
145:
146: public void start() throws ConfigurationException {
147: identityManager = (IdentityManager) ((Context) owner)
148: .get("IdentityManager");
149: }
150:
151: public void stop() throws ConfigurationException {
152: // Nothing to do here
153: }
154:
155: public void setIdentityManager(IdentityManager identityManager) {
156: this.identityManager = identityManager;
157: }
158: }
|