/*
* 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.mda.db.model;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.apache.xpath.CachedXPathAPI;
import org.w3c.dom.Element;
import biz.hammurapi.config.ConfigurationException;
import biz.hammurapi.web.HammurapiWebException;
import biz.hammurapi.web.interaction.InteractionInstance;
import biz.hammurapi.web.interaction.InteractionInstance.StepInstance;
import biz.hammurapi.web.mda.Template;
import biz.hammurapi.web.mda.TemplateFactory;
import biz.hammurapi.web.metadata.DbmColumnImpl;
import biz.hammurapi.web.properties.PropertySet;
import biz.hammurapi.web.properties.TransientPropertySet;
import biz.hammurapi.xml.dom.AbstractDomObject;
import biz.hammurapi.xml.dom.CompositeDomSerializer;
public class Column extends DbmColumnImpl {
public Column() {
}
public Column(boolean force) {
super(force);
}
public Column(ResultSet rs) throws SQLException {
super(rs);
}
public Column(Element holder, boolean force) throws ConfigurationException {
super(holder, force);
}
public Column(Element holder, Properties nameMap, CachedXPathAPI cxpa, boolean force) throws ConfigurationException {
super(holder, nameMap, cxpa, force);
}
private Table owner;
public Table getOwner() {
return owner;
}
void setOwner(Table owner) {
this.owner = owner;
}
private Map metaData = new HashMap();
public Object getMetaValue(String name) {
return metaData.get(name);
}
public void toDom(Element holder) {
super.toDom(holder);
if (metaData!=null) {
CompositeDomSerializer.getThreadInstance().toDomSerializable(metaData).toDom(AbstractDomObject.addElement(holder, "meta-data"));
}
AbstractDomObject.addTextElement(holder, "java-name", getJavaName());
AbstractDomObject.addTextElement(holder, "label", getLabel());
}
boolean loadMetaData(DatabaseMetaData metaData) throws SQLException {
Schema schema = owner.getOwner();
Catalog catalog = schema.getOwner();
ResultSet rs = metaData.getColumns(catalog.getName(), schema.getName(), owner.getName(), getName());
if (rs.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
for (int i=1; i<=rsmd.getColumnCount(); ++i) {
this.metaData.put(rsmd.getColumnName(i), rs.getObject(i));
}
}
rs.close();
return !this.metaData.isEmpty();
}
public String getJavaName() {
String ret = super.getJavaName();
return ret==null || ret.trim().length()==0 ? getOwner().getOwner().getOwner().getOwner().getGenerationPolicy().generateColumnName(getName()) : ret;
}
public String getLabel() {
String ret = super.getLabel();
return ret==null || ret.trim().length()==0 ? getOwner().getOwner().getOwner().getOwner().getGenerationPolicy().generateLabel(getJavaName()) : ret;
}
/**
* Generates code from template specified for given column for given view
* @param viewName
* @return
* @throws SQLException
* @throws HammurapiWebException
* @throws
*/
public String generate(String viewName) throws SQLException, HammurapiWebException {
InteractionInstance interactionInstance = getOwner().getInteractionInstance();
if (interactionInstance==null) {
return "<!-- Column "+getName()+": Interation instance not set -->";
}
String presentationId = null;
Iterator it = interactionInstance.getStepsByDefinitionName("Select column presentation").iterator();
while (it.hasNext()) {
StepInstance step = (StepInstance) it.next();
if ("completed".equals(step.getStatus()) && viewName.equals(step.getProperty("viewName", true))) {
presentationId = (String) step.getProperty("columns/"+getId());
break;
}
}
if (presentationId==null) {
return "<!-- Column "+getName()+": Presentation is not set -->";
}
PropertySet presentationParameters = new TransientPropertySet();
it = interactionInstance.getStepsByDefinitionName("Presentation configuration").iterator();
while (it.hasNext()) {
StepInstance step = (StepInstance) it.next();
if ("completed".equals(step.getStatus())
&& viewName.equals(step.getProperty("viewName", true))
&& String.valueOf(getId()).equals(step.getProperty("column"))) {
presentationParameters = step.getProperties().getSubset("parameter/");
break;
}
}
TemplateFactory templateFactory = getOwner().getOwner().getOwner().getOwner().getTemplateFactory();
if (templateFactory==null) {
return "<!-- Column "+getName()+": Template factory is not set -->";
}
Template template = templateFactory.getTemplate(Integer.parseInt(presentationId));
Map context = new HashMap();
context.put("column", this);
context.put("presentationParameters", presentationParameters);
final PropertySet pp=presentationParameters;
context.put(
"presentationParametersHelper",
new PresentationParametersHelper() {
public String getIf(String parameterName, String value, String ifTrue, String ifFalse) {
return value.equals(pp.get(parameterName)) ? ifTrue : ifFalse;
}
public String getIf(String parameterName, String value, String ifTrue) {
return getIf(parameterName, value, ifTrue, "");
}
});
return template.evaluate(context, getClass().getClassLoader());
}
/**
* Helper interface.
* @author Pavel Vlasov
*/
public interface PresentationParametersHelper {
/**
* Helper method.
* @param parameterName Parameter name
* @param value value
* @param ifTrue Return value if parameter is equal to the value
* @param ifFalse Return value if parameter is not equal to the value.
* @return ifTrue if parameter value equals to the passed value, ifFalse otherwise
*/
public String getIf(String parameterName, String value, String ifTrue, String ifFalse);
/**
* Helper method.
* @param parameterName Parameter name
* @param value value
* @param ifTrue Return value if parameter is equal to the value
* @return ifTrue if parameter value equals to the passed value, Empty string otherwise
*/
public String getIf(String parameterName, String value, String ifTrue);
}
/**
* Generates code from template specified for given column for given view
* @param viewName
* @return
* @throws SQLException
* @throws HammurapiWebException
* @throws
*/
public Template getTemplate(String viewName) throws SQLException, HammurapiWebException {
InteractionInstance interactionInstance = getOwner().getInteractionInstance();
if (interactionInstance==null) {
return null;
}
String presentationId = null;
Iterator it = interactionInstance.getStepsByDefinitionName("Select column presentation").iterator();
while (it.hasNext()) {
StepInstance step = (StepInstance) it.next();
String svn = (String) step.getProperty("viewName", true);
if (svn!=null) {
svn=svn.trim();
}
if ("completed".equals(step.getStatus()) && viewName.equals(svn)) {
presentationId = (String) step.getProperty("columns/"+getId());
break;
}
}
if (presentationId==null) {
return null;
}
TemplateFactory templateFactory = getOwner().getOwner().getOwner().getOwner().getTemplateFactory();
if (templateFactory==null) {
return null;
}
return templateFactory.getTemplate(Integer.parseInt(presentationId));
}
public boolean isPrimaryKey() {
Iterator it=getOwner().getPrimaryKeys().iterator();
while (it.hasNext()) {
Map metaMap = (Map) it.next();
if (getName().equals(metaMap.get("COLUMN_NAME"))) {
return true;
}
}
return false;
}
// public String getTemplateParameter(String viewName, String parameterName) {
//
// }
}
|