
org.efaps.db.Update 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.
The newest version!
/*
* Copyright 2003 - 2013 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$
* Last Changed: $Date$
* Last Changed By: $Author$
*/
package org.efaps.db;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.collections4.iterators.ReverseListIterator;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.efaps.admin.access.AccessCache;
import org.efaps.admin.access.AccessTypeEnums;
import org.efaps.admin.datamodel.Attribute;
import org.efaps.admin.datamodel.AttributeType;
import org.efaps.admin.datamodel.Dimension.UoM;
import org.efaps.admin.datamodel.SQLTable;
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.event.Return.ReturnValues;
import org.efaps.ci.CIAttribute;
import org.efaps.db.transaction.ConnectionResource;
import org.efaps.db.wrapper.SQLUpdate;
import org.efaps.util.EFapsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author The eFaps Team
* @version $Id$
*/
public class Update
{
/**
* Variable to get the Status of this update.
*/
private static final Status STATUSOK = new Status();
/**
* Logging instance used in this class.
*/
private static final Logger LOG = LoggerFactory.getLogger(Update.class);
/**
* The instance variable stores the instance for which this update is made.
*/
private Instance instance = null;
/**
* Mapping of SQL-Table to Attribute and Value.
*/
private final Map> table2values = new Hashtable>();
/**
* Mapping of attribute to values.
*/
private final Map attr2values = new HashMap();
/**
* Mapping of attribute to values.
*/
private final Map trigRelevantAttr2values = new HashMap();
/**
* @param _type Type to be updated
* @param _id id to be updated
* @throws EFapsException on error
*/
public Update(final Type _type,
final String _id)
throws EFapsException
{
this(Instance.get(_type, _id));
}
/**
* @param _type Type to be updated
* @param _id id to be updated
* @throws EFapsException on error
*/
public Update(final Type _type,
final long _id)
throws EFapsException
{
this(Instance.get(_type, _id));
}
/**
* @param _type Type to be updated
* @param _id id to be updated
* @throws EFapsException on error
*/
public Update(final String _type,
final String _id)
throws EFapsException
{
this(Type.get(_type), _id);
}
/**
* @param _oid OID of the instance to be updated.
* @throws EFapsException on error
*/
public Update(final String _oid)
throws EFapsException
{
this(Instance.get(_oid));
}
/**
* @param _instance instance to be updated.
* @throws EFapsException on error
*/
public Update(final Instance _instance)
throws EFapsException
{
setInstance(_instance);
addAlwaysUpdateAttributes();
if (!Update.STATUSOK.getStati().isEmpty()) {
Update.STATUSOK.getStati().clear();
}
}
/**
* Add all attributes of the type which must be always updated.
* @throws EFapsException on error
*/
protected void addAlwaysUpdateAttributes()
throws EFapsException
{
final Iterator> iter = getInstance().getType().getAttributes().entrySet().iterator();
while (iter.hasNext()) {
final Map.Entry, ?> entry = (Map.Entry, ?>) iter.next();
final Attribute attr = (Attribute) entry.getValue();
final AttributeType attrType = attr.getAttributeType();
if (attrType.isAlwaysUpdate()) {
addInternal(attr, false, (Object[]) null);
}
}
}
/**
* The method closes the SQL statement.
*
* @see #statement
* @throws EFapsException on error
*/
public void close()
throws EFapsException
{
}
/**
* The method gets all events for the given EventType and executes them in
* the given order. If no events are defined, nothing is done. The method
* return TRUE if a event was found, otherwise FALSE.
*
* @param _eventtype trigger events to execute
* @return true if a trigger was found and executed, otherwise false
* @throws EFapsException on error
*/
protected boolean executeEvents(final EventType _eventtype)
throws EFapsException
{
boolean ret = false;
final List triggers = getInstance().getType().getEvents(_eventtype);
if (triggers != null) {
final Parameter parameter = new Parameter();
parameter.put(ParameterValues.NEW_VALUES, getNewValuesMap());
parameter.put(ParameterValues.INSTANCE, getInstance());
for (final EventDefinition evenDef : triggers) {
evenDef.execute(parameter);
}
ret = true;
}
return ret;
}
/**
* @return the map of new values send to the triggers
*/
protected final Map getNewValuesMap()
{
// convert the map in a more simple map (following existing API)
final Map ret = new HashMap();
for (final Entry entry : this.trigRelevantAttr2values.entrySet()) {
ret.put(entry.getKey(), entry.getValue().getValues());
}
return ret;
}
/**
* @param _attr name of attribute to update
* @param _values attribute values
* @throws EFapsException on error
* @return Status
*/
public Status add(final String _attr,
final Object... _values)
throws EFapsException
{
final Attribute attr = getInstance().getType().getAttribute(_attr);
if (attr == null) {
throw new EFapsException(getClass(), "add.UnknownAttributeName", _attr);
}
return add(attr, _values);
}
/**
* @param _attr attribute to update
* @param _values attribute value
* @throws EFapsException on error
* @return Status
*/
public Status add(final Attribute _attr,
final Object... _values)
throws EFapsException
{
return addInternal(_attr, true, _values);
}
/**
* @param _attr attribute to update
* @param _values attribute value
* @throws EFapsException on error
* @return Status
*/
public Status add(final CIAttribute _attr,
final Object... _values)
throws EFapsException
{
final Attribute attr = getInstance().getType().getAttribute(_attr.name);
if (attr == null) {
throw new EFapsException(getClass(), "add.UnknownAttributeName", _attr);
}
return add(attr, _values);
}
/**
* @param _attr Attribute to add
* @param _value value to add
* @param _triggerRelevant is the attribute triggerrelevant
* @return Status
* @throws EFapsException on error
*/
protected Status addInternal(final Attribute _attr,
final boolean _triggerRelevant,
final Object... _value)
throws EFapsException
{
Status ret = Update.STATUSOK;
if (_attr.hasEvents(EventType.VALIDATE)) {
final List returns = _attr.executeEvents(EventType.VALIDATE, ParameterValues.NEW_VALUES, _value);
for (final Return retu : returns) {
if (retu.get(ReturnValues.TRUE) == null) {
ret = new Status(retu.get(ReturnValues.VALUES), _attr, _value);
break;
}
}
}
final Value value = getValue(_attr, _value);
validate(getInstance(), value);
List values = this.table2values.get(_attr.getTable());
if (values == null) {
values = new ArrayList();
this.table2values.put(_attr.getTable(), values);
}
values.add(value);
this.attr2values.put(_attr, value);
if (_triggerRelevant) {
this.trigRelevantAttr2values.put(_attr, value);
}
return ret;
}
/**
* @param _instance instance to be validated
* @param _value value to be validated
* @throws EFapsException if invalid
*/
protected void validate(final Instance _instance,
final Value _value)
throws EFapsException
{
_value.getAttribute().getAttributeType().getDbAttrType()
.valiate4Update(_value.getAttribute(), getInstance(), _value.getValues());
}
/**
* @param _attr Attribute
* @param _value values to be evaluated
* @return value
*/
protected Value getValue(final Attribute _attr,
final Object[] _value)
{
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy