org.efaps.admin.AbstractAdminObject Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of efaps-kernel Show documentation
Show all versions of efaps-kernel Show documentation
eFaps is a framework used to map objects with or without attached files to
a relational database and optional file systems (only for attaches files). Configurable access control can be provided down to object and attribute level depending on implementation and use case. Depending on requirements, events (like triggers) allow to implement business logic and to separate business logic from user interface.
The framework includes integrations (e.g. webdav, full text search) and a web application as 'simple' configurable user interface. Some best practises, example web application modules (e.g. team work module) support administrators and implementers using this framework.
/*
*
* Copyright 2003 - 2011 The eFaps Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Revision: $Rev: 6210 $
* Last Changed: $Date: 2011-02-18 16:02:56 -0500 (Fri, 18 Feb 2011) $
* Last Changed By: $Author: [email protected] $
*/
package org.efaps.admin;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.efaps.admin.datamodel.Type;
import org.efaps.admin.event.EventDefinition;
import org.efaps.admin.event.EventType;
import org.efaps.admin.event.Parameter;
import org.efaps.admin.event.Parameter.ParameterValues;
import org.efaps.admin.event.Return;
import org.efaps.admin.ui.AbstractUserInterfaceObject;
import org.efaps.db.Context;
import org.efaps.db.wrapper.SQLPart;
import org.efaps.db.wrapper.SQLSelect;
import org.efaps.util.EFapsException;
import org.efaps.util.cache.CacheObjectInterface;
import org.efaps.util.cache.CacheReloadException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author The eFaps Team
* @version $Id: AbstractAdminObject.java 6210 2011-02-18 21:02:56Z [email protected] $
*/
public abstract class AbstractAdminObject
implements CacheObjectInterface
{
/**
* Logging instance used in this class.
*/
private static final Logger LOG = LoggerFactory.getLogger(AbstractAdminObject.class);
/**
* The instance variable stores the id of the collections object.
*
* @see #getId
*/
private final long id;
/**
* This is the instance variable for the universal unique identifier of this
* admin object.
*
* @see #getUUID
*/
private final UUID uuid;
/**
* This is the instance variable for the name of this admin object.
*
* @see #setName
* @see #getName
*/
private final String name;
/**
* This is the instance variable for the properties.
*
* @getProperties
*/
private final Map properties = new HashMap();
/**
* All events for this AdminObject are stored in this map.
*/
private final Map> events = new HashMap>();
/**
* Constructor to set instance variables {@link #id}, {@link #uuid} and
* {@link #name} of this administrational object.
*
* @param _id id to set
* @param _uuid universal unique identifier
* @param _name name to set
* @see #id
* @see #uuid
* @see #name
*/
protected AbstractAdminObject(final long _id,
final String _uuid,
final String _name)
{
this.id = _id;
this.uuid = (_uuid == null) ? null : UUID.fromString(_uuid.trim());
this.name = (_name == null) ? null : _name.trim();
}
/**
* Sets the link properties for this object.
*
* @param _linkType type of the link property
* @param _toId to id
* @param _toType to type
* @param _toName to name
* @throws EFapsException on error
*/
protected void setLinkProperty(final Type _linkType,
final long _toId,
final Type _toType,
final String _toName)
throws EFapsException
{
}
/**
* The instance method sets all properties of this administrational object.
* All properties are stores in instance variable {@link #properties}.
*
* @param _name name of the property (key)
* @param _value value of the property
* @see #properties
* @throws CacheReloadException on error
*/
protected void setProperty(final String _name,
final String _value)
throws CacheReloadException
{
getProperties().put(_name, _value);
}
/**
* The value of the given property is returned.
*
* @param _name name of the property (key)
* @return value of the property with the given name / key.
* @see #properties
*/
public String getProperty(final String _name)
{
return getProperties().get(_name);
}
/**
* Adds a new event to this AdminObject.
*
* @param _eventtype Eventtype class name to add
* @param _eventdef EventDefinition to add
* @see #events
*/
public void addEvent(final EventType _eventtype, final EventDefinition _eventdef)
{
List evenList = this.events.get(_eventtype);
if (evenList == null) {
evenList = new ArrayList();
this.events.put(_eventtype, evenList);
}
if (!evenList.contains(_eventdef)) {
int pos = 0;
for (final EventDefinition cur : evenList) {
if (_eventdef.getIndexPos() > cur.getIndexPos()) {
break;
}
pos++;
}
evenList.add(pos, _eventdef);
}
}
/**
* Returns the list of events defined for given event type.
*
* @param _eventType event type
* @return list of events for the given event type
*/
public List getEvents(final EventType _eventType)
{
return this.events.get(_eventType);
}
/**
* Does this instance have Event, for the specified EventType ?
*
* @param _eventtype type of event to check for
* @return true, if this instance has a trigger, otherwise
* false.
*/
public boolean hasEvents(final EventType _eventtype)
{
return this.events.get(_eventtype) != null;
}
/**
* The method gets all events for the given event type and executes them in
* the given order. If no events are defined, nothing is done.
*
* @param _eventtype type of event to execute
* @param _args arguments used as parameter (doubles with first parameters
* defining the key, second parameter the value itself)
* @return List with Returns
* @throws EFapsException on error
*/
public List executeEvents(final EventType _eventtype, final Object... _args)
throws EFapsException
{
final List ret = new ArrayList();
if (hasEvents(_eventtype)) {
final Parameter param = new Parameter();
if (_args != null) {
// add all parameters
for (int i = 0; i < _args.length; i += 2) {
if (((i + 1) < _args.length) && (_args[i] instanceof ParameterValues)) {
param.put((ParameterValues) _args[i], _args[i + 1]);
}
}
}
ret.addAll(executeEvents(_eventtype, param));
}
return ret;
}
/**
* The method gets all events for the given event type and executes them in
* the given order. If no events are defined, nothing is done.
*
* @param _eventtype type of event to execute
* @param _param Parameter to be passed to the esjp
* @return List with Returns
* @throws EFapsException on error
*/
public List executeEvents(final EventType _eventtype, final Parameter _param)
throws EFapsException
{
final List ret = new ArrayList();
if (hasEvents(_eventtype)) {
if (this instanceof AbstractUserInterfaceObject) {
// add ui object to parameter
_param.put(ParameterValues.UIOBJECT, this);
}
// execute all triggers
for (final EventDefinition evenDef : this.events.get(_eventtype)) {
ret.add(evenDef.execute(_param));
}
}
return ret;
}
/**
* The instance method reads the properties for this administration object.
* Each found property is set with instance method {@link #setProperty}.
*
* @see #setProperty
* @throws CacheReloadException on error
*/
protected void readFromDB4Properties() throws CacheReloadException
{
Statement stmt = null;
try {
stmt = Context.getThreadContext().getConnection().createStatement();
final SQLSelect select = new SQLSelect()
.column("NAME")
.column("VALUE")
.from("T_CMPROPERTY")
.addPart(SQLPart.WHERE)
.addColumnPart(null, "ABSTRACT")
.addPart(SQLPart.EQUAL)
.addValuePart(getId());
final ResultSet rs = stmt.executeQuery(select.getSQL());
while (rs.next()) {
final String nameStr = rs.getString(1).trim();
final String value = rs.getString(2).trim();
setProperty(nameStr, value);
}
rs.close();
} catch (final EFapsException e) {
throw new CacheReloadException("could not read properties for " + "'" + getName() + "'", e);
} catch (final SQLException e) {
throw new CacheReloadException("could not read properties for " + "'" + getName() + "'", e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (final SQLException e) {
AbstractAdminObject.LOG.warn("Catched SQLExeption in class" + this.getClass());
}
}
}
}
/**
* Reads all links for this administration object. Each found link property
* is set with instance method {@link setLinkProperty}.
*
* @see #setLinkProperty
* @throws CacheReloadException on error
*
*/
protected void readFromDB4Links() throws CacheReloadException
{
Statement stmt = null;
try {
stmt = Context.getThreadContext().getConnection().createStatement();
final SQLSelect select = new SQLSelect()
.column(0, "TYPEID")
.column(0, "TOID")
.column(1, "TYPEID")
.column(1, "NAME")
.from("T_CMABSTRACT2ABSTRACT", 0)
.leftJoin("T_CMABSTRACT", 1, "ID", 0, "TOID")
.addPart(SQLPart.WHERE)
.addColumnPart(0, "FROMID")
.addPart(SQLPart.EQUAL)
.addValuePart(getId());
final ResultSet resultset = stmt.executeQuery(select.getSQL());
while (resultset.next()) {
final long conTypeId = resultset.getLong(1);
final long toId = resultset.getLong(2);
final long toTypeId = resultset.getLong(3);
final String toName = resultset.getString(4);
final Type conType = Type.get(conTypeId);
final Type toType = Type.get(toTypeId);
if (conType != null && toType != null) {
setLinkProperty(conType, toId, toType, toName.trim());
}
}
resultset.close();
//CHECKSTYLE:OFF
} catch (final Exception e) {
//CHECKSTYLE:ON
throw new CacheReloadException("could not read db links for " + "'" + getName() + "'", e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (final SQLException e) {
AbstractAdminObject.LOG.warn("Catched SQLExeption in class" + this.getClass());
}
}
}
}
/**
* This is the getter method for instance variable {@link #id}.
*
* @return value of instance variable {@id}
* @see #id
*/
public long getId()
{
return this.id;
}
/**
* This is the getter method for instance variable {@link #uuid}.
*
* @return value of instance variable {@uuid}
* @see #uuid
*/
public UUID getUUID()
{
return this.uuid;
}
/**
* This is the getter method for instance variable {@link #name}.
*
* @return value of instance variable {@link #name}
* @see #name
* @see #setName
*/
public String getName()
{
return this.name;
}
/**
* This is the getter method for instance variable {@link #properties}.
*
* @return value of instance variable {@link #properties}
* @see #properties
*/
protected Map getProperties()
{
return this.properties;
}
/**
* This is the getter method for instance variable {@link #events}.
*
* @return value of instance variable {@link #events}
* @see #events
*/
protected Map> getEvents()
{
return this.events;
}
/**
* The method overrides the original method 'toString' and returns the name
* of the user interface object.
*
* @return name of the user interface object
*/
@Override
public String toString()
{
return new ToStringBuilder(this).append("name", getName()).append("uuid", getUUID()).append("id", getId())
.append("properties", getProperties()).append("events", this.events).toString();
}
}