/*
* argun 1.0
* Web 2.0 delivery framework
* Copyright (C) 2007 Hammurapi Group
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* URL: http://www.hammurapi.biz
* e-Mail: support@hammurapi.biz
*/
package biz.hammurapi.web.properties;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import biz.hammurapi.config.Component;
import biz.hammurapi.config.ConfigurationException;
import biz.hammurapi.config.Context;
import biz.hammurapi.sql.DataAccessObject;
import biz.hammurapi.sql.IdentityGenerator;
import biz.hammurapi.sql.IdentityManager;
import biz.hammurapi.sql.IdentityRetriever;
import biz.hammurapi.sql.SQLProcessor;
import biz.hammurapi.sql.SQLRuntimeException;
import biz.hammurapi.web.property.sql.PropertyEngine;
/**
* Root class for managing property sets
* @author Pavel
*
*/
public class PropertySetFactory implements DataAccessObject, Component {
private SQLProcessor processor;
private Object owner;
IdentityManager identityManager;
private PropertyEngine engine;
public void setSQLProcessor(SQLProcessor processor) throws SQLException {
this.processor = processor;
this.engine = new PropertyEngine(processor);
}
/**
* Creates property set
* @param isTransient If this paramter is true then property set returned is not backed by the database.
* @return New empty property set
*/
public PropertySet create(boolean isTransient) {
if (isTransient) {
return new TransientPropertySet();
}
try {
Connection con = processor.getConnection();
DbPropertySet dps = new DbPropertySet(true);
dps.setDescription("New property set");
if (identityManager instanceof IdentityGenerator) {
dps.setId(((IdentityGenerator) identityManager).generate(con, "PROPERTY_SET"));
}
new PropertyEngine(new SQLProcessor(con, null)).insertPropertySet(dps);
if (identityManager instanceof IdentityRetriever) {
dps.setId(((IdentityRetriever) identityManager).retrieve(con));
}
processor.releaseConnection(con);
dps.setSQLProcessor(processor);
return dps;
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
}
/**
* Finds property set by id.
* @param id
* @return
* @throws SQLException
*/
public PersistentPropertySet find(int id) throws SQLException {
DbPropertySet ret = (DbPropertySet) engine.getPropertySet(id, DbPropertySet.class);
if (ret!=null) {
ret.setSQLProcessor(processor);
}
return ret;
}
/**
* Finds property set by owner and name
* @param ownerType
* @param ownerId
* @param setName
* @param create If this parameter is true then this method creates property set if one doesn't already exist
* @return
* @throws SQLException
*/
public PersistentPropertySet find(String ownerType, String ownerId, String setName, boolean create) throws SQLException {
DbPropertySet ret = (DbPropertySet) engine.getPropertySetNameEQ(ownerType, ownerId, setName, DbPropertySet.class);
if (ret==null) {
ret = (DbPropertySet) create(false);
ret.setOwnerType(ownerType);
ret.setOwnerId(ownerId);
ret.setName(setName);
ret.update();
} else {
ret.setSQLProcessor(processor);
}
return ret;
}
/**
* Finds all property sets for a given owner.
* @param ownerType
* @param ownerId
* @return
*/
public Collection find(String ownerType, String ownerId) {
return engine.getPropertySetOwnerEQ(ownerType, ownerId, DbPropertySet.class);
}
public void setOwner(Object owner) {
this.owner = owner;
}
public void start() throws ConfigurationException {
identityManager = (IdentityManager) ((Context) owner).get("IdentityManager");
}
public void stop() throws ConfigurationException {
// Nothing to do here
}
public void setIdentityManager(IdentityManager identityManager) {
this.identityManager = identityManager;
}
}
|