org.efaps.db.AbstractPrintQuery 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 - 2012 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: 7483 $
* Last Changed: $Date: 2012-05-11 11:57:38 -0500 (Fri, 11 May 2012) $
* Last Changed By: $Author: [email protected] $
*/
package org.efaps.db;
import java.io.StringReader;
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 org.efaps.admin.datamodel.Attribute;
import org.efaps.admin.datamodel.AttributeSet;
import org.efaps.admin.datamodel.Type;
import org.efaps.beans.ValueList;
import org.efaps.beans.valueparser.ParseException;
import org.efaps.beans.valueparser.ValueParser;
import org.efaps.ci.CIAttribute;
import org.efaps.db.print.OneSelect;
import org.efaps.db.print.Phrase;
import org.efaps.db.transaction.ConnectionResource;
import org.efaps.db.wrapper.SQLPart;
import org.efaps.db.wrapper.SQLSelect;
import org.efaps.util.EFapsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract class all print queries are based on.
*
* @author The eFaps Team
* @version $Id: AbstractPrintQuery.java 7483 2012-05-11 16:57:38Z [email protected] $
*/
public abstract class AbstractPrintQuery
{
/**
* Logging instance used in this class.
*/
protected static final Logger LOG = LoggerFactory.getLogger(PrintQuery.class);
/**
* Mapping of Select statements to OneSelect.
*/
private final Map selectStmt2OneSelect = new HashMap();
/**
* Mapping of attributes to OneSelect.
*/
private final Map attr2OneSelect = new HashMap();
/**
* Mapping of sql tables to table index.
* @see #tableIndex
*/
private final Map sqlTable2Index = new HashMap();
/**
* Mapping of key to Phrase.
*/
private final Map key2Phrase = new HashMap();
/**
* List of all OneSelect belonging to this PrintQuery.
*/
private final List allSelects = new ArrayList();
/**
* Index of an sqltable.
*/
private int tableIndex = 0;
/**
* Must the list of instance be in the same order as given.
* (There are some cases the sequence might be different returned from the
* database. To enforce the exact sequence this flag can be set. But sorting
* takes time and should not be used by default.)
*/
private boolean enforceSorted;
/**
* Index of the type column.
*/
private int typeColumnIndex = 0;
/**
* Add an attribute to the PrintQuery. It is used to get editable values
* from the eFaps DataBase.
*
* @param _attributes Attribute to add
* @return this PrintQuery
* @throws EFapsException on error
*/
public AbstractPrintQuery addAttribute(final CIAttribute... _attributes)
throws EFapsException
{
if (isMarked4execute()) {
for (final CIAttribute attr : _attributes) {
addAttribute(attr.name);
}
}
return this;
}
/**
* Add an attribute to the PrintQuery. It is used to get editable values
* from the eFaps DataBase.
*
* @param _attributes Attribute to add
* @return this PrintQuery
*/
public AbstractPrintQuery addAttribute(final Attribute... _attributes)
{
if (isMarked4execute()) {
for (final Attribute attr : _attributes) {
final OneSelect oneselect = new OneSelect(this, attr);
this.allSelects.add(oneselect);
this.attr2OneSelect.put(attr.getName(), oneselect);
}
}
return this;
}
/**
* Add a oneselect to this print query.
*
* @param _oneSelect select to be added
*/
protected void addOneSelect(final OneSelect _oneSelect)
{
this.allSelects.add(_oneSelect);
}
/**
* Getter method for instance variable {@link #allSelects}.
*
* @return value of instance variable {@link #allSelects}
*/
protected List getAllSelects()
{
return this.allSelects;
}
/**
* Method to get the attribute for an _attributeName
.
*
* @param _attributeName name of the attribute
* @return Attribute
*/
public Attribute getAttribute4Attribute(final String _attributeName)
{
final OneSelect oneselect = this.attr2OneSelect.get(_attributeName);
return oneselect == null ? null : oneselect.getAttribute();
}
/**
* Method to get the instance for an _attributeName
.
* @param _attributeName name of the attribute
* @return list of instance
* @throws EFapsException on error
*/
public List getInstances4Attribute(final String _attributeName)
throws EFapsException
{
final OneSelect oneselect = this.attr2OneSelect.get(_attributeName);
return oneselect == null ? null : oneselect.getInstances();
}
/**
* Get the object returned by the given name of an attribute.
*
* @param class the return value will be casted to
* @param _attribute attribute the object is wanted for
* @return object for the select statement
* @throws EFapsException on error
*/
public T getAttribute(final CIAttribute _attribute)
throws EFapsException
{
return this.getAttribute(_attribute.name);
}
/**
* Get the object returned by the given name of an attribute.
*
* @param class the return value will be casted to
* @param _attributeName name of the attribute the object is wanted for
* @return object for the select statement
* @throws EFapsException on error
*/
@SuppressWarnings("unchecked")
public T getAttribute(final String _attributeName)
throws EFapsException
{
final OneSelect oneselect = this.attr2OneSelect.get(_attributeName);
return oneselect == null ? null : (T) oneselect.getObject();
}
/**
* Get the object returned by the given Attribute.
*
* @param class the return value will be casted to
* @param _attribute the object is wanted for
* @return object for the select statement
* @throws EFapsException on error
*/
@SuppressWarnings("unchecked")
public T getAttribute(final Attribute _attribute)
throws EFapsException
{
return (T) getAttribute(_attribute.getName());
}
/**
* Method returns the Main type of the query. In case that the query is
* based on only one type, this Type is returned. In case that the query
* contains different Types, the type returned must be the type, all other
* types are derived from.
*
* @return Type
*/
public abstract Type getMainType();
/**
* Method to get the instances this PrintQuery is executed on.
*
* @return List of instances
*/
public abstract List getInstanceList();
/**
* Method to get the current Instance.
*
* @return current Instance
*/
public abstract Instance getCurrentInstance();
/**
* Add an AttributeSet to the PrintQuery. It is used to get editable values
* from the eFaps DataBase.
*
* @param _setName Name of the AttributeSet to add
* @return this PrintQuery
* @throws EFapsException on error
*/
public AbstractPrintQuery addAttributeSet(final String _setName)
throws EFapsException
{
final Type type = getMainType();
final AttributeSet set = AttributeSet.find(type.getName(), _setName);
addAttributeSet(set);
return this;
}
/**
* Add an AttributeSet to the PrintQuery. It is used to get editable values
* from the eFaps DataBase. The AttributeSet is internally transformed into
* an linkfrom query.
*
* @param _set AttributeSet to add
* @return this PrintQuery
* @throws EFapsException on error
*/
public AbstractPrintQuery addAttributeSet(final AttributeSet _set)
throws EFapsException
{
final String key = "linkfrom[" + _set.getName() + "#" + _set.getAttributeName() + "]";
final OneSelect oneselect = new OneSelect(this, key);
this.allSelects.add(oneselect);
this.attr2OneSelect.put(_set.getAttributeName(), oneselect);
oneselect.analyzeSelectStmt();
for (final String setAttrName : _set.getSetAttributes()) {
if (!setAttrName.equals(_set.getAttributeName())) {
oneselect.getFromSelect().addOneSelect(new OneSelect(this, _set.getAttribute(setAttrName)));
}
}
oneselect.getFromSelect().getMainOneSelect().setAttribute(_set.getAttribute(_set.getAttributeName()));
return this;
}
/**
* Get the object returned by the given name of an AttributeSet.
*
* @param class the return value will be casted to
* @param _setName name of the AttributeSet the object is wanted for
* @return object for the select statement
* @throws EFapsException on error
*/
@SuppressWarnings("unchecked")
public T getAttributeSet(final String _setName)
throws EFapsException
{
final OneSelect oneselect = this.attr2OneSelect.get(_setName);
Map ret = null;
if (oneselect.getFromSelect().hasResult()) {
ret = new HashMap();
// in an attributset the first one is fake
boolean first = true;
for (final OneSelect onsel : oneselect.getFromSelect().getAllSelects()) {
if (first) {
first = false;
} else {
final ArrayList