001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.mda.db.model;
024:
025: import java.sql.DatabaseMetaData;
026: import java.sql.ResultSet;
027: import java.sql.ResultSetMetaData;
028: import java.sql.SQLException;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.Properties;
033:
034: import org.apache.xpath.CachedXPathAPI;
035: import org.w3c.dom.Element;
036:
037: import biz.hammurapi.config.ConfigurationException;
038: import biz.hammurapi.web.HammurapiWebException;
039: import biz.hammurapi.web.interaction.InteractionInstance;
040: import biz.hammurapi.web.interaction.InteractionInstance.StepInstance;
041: import biz.hammurapi.web.mda.Template;
042: import biz.hammurapi.web.mda.TemplateFactory;
043: import biz.hammurapi.web.metadata.DbmColumnImpl;
044: import biz.hammurapi.web.properties.PropertySet;
045: import biz.hammurapi.web.properties.TransientPropertySet;
046: import biz.hammurapi.xml.dom.AbstractDomObject;
047: import biz.hammurapi.xml.dom.CompositeDomSerializer;
048:
049: public class Column extends DbmColumnImpl {
050:
051: public Column() {
052: }
053:
054: public Column(boolean force) {
055: super (force);
056: }
057:
058: public Column(ResultSet rs) throws SQLException {
059: super (rs);
060: }
061:
062: public Column(Element holder, boolean force)
063: throws ConfigurationException {
064: super (holder, force);
065: }
066:
067: public Column(Element holder, Properties nameMap,
068: CachedXPathAPI cxpa, boolean force)
069: throws ConfigurationException {
070: super (holder, nameMap, cxpa, force);
071: }
072:
073: private Table owner;
074:
075: public Table getOwner() {
076: return owner;
077: }
078:
079: void setOwner(Table owner) {
080: this .owner = owner;
081: }
082:
083: private Map metaData = new HashMap();
084:
085: public Object getMetaValue(String name) {
086: return metaData.get(name);
087: }
088:
089: public void toDom(Element holder) {
090: super .toDom(holder);
091: if (metaData != null) {
092: CompositeDomSerializer.getThreadInstance()
093: .toDomSerializable(metaData).toDom(
094: AbstractDomObject.addElement(holder,
095: "meta-data"));
096: }
097: AbstractDomObject.addTextElement(holder, "java-name",
098: getJavaName());
099: AbstractDomObject.addTextElement(holder, "label", getLabel());
100: }
101:
102: boolean loadMetaData(DatabaseMetaData metaData) throws SQLException {
103: Schema schema = owner.getOwner();
104: Catalog catalog = schema.getOwner();
105: ResultSet rs = metaData.getColumns(catalog.getName(), schema
106: .getName(), owner.getName(), getName());
107: if (rs.next()) {
108: ResultSetMetaData rsmd = rs.getMetaData();
109: for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
110: this .metaData.put(rsmd.getColumnName(i), rs
111: .getObject(i));
112: }
113: }
114: rs.close();
115: return !this .metaData.isEmpty();
116: }
117:
118: public String getJavaName() {
119: String ret = super .getJavaName();
120: return ret == null || ret.trim().length() == 0 ? getOwner()
121: .getOwner().getOwner().getOwner().getGenerationPolicy()
122: .generateColumnName(getName()) : ret;
123: }
124:
125: public String getLabel() {
126: String ret = super .getLabel();
127: return ret == null || ret.trim().length() == 0 ? getOwner()
128: .getOwner().getOwner().getOwner().getGenerationPolicy()
129: .generateLabel(getJavaName()) : ret;
130: }
131:
132: /**
133: * Generates code from template specified for given column for given view
134: * @param viewName
135: * @return
136: * @throws SQLException
137: * @throws HammurapiWebException
138: * @throws
139: */
140: public String generate(String viewName) throws SQLException,
141: HammurapiWebException {
142: InteractionInstance interactionInstance = getOwner()
143: .getInteractionInstance();
144: if (interactionInstance == null) {
145: return "<!-- Column " + getName()
146: + ": Interation instance not set -->";
147: }
148:
149: String presentationId = null;
150: Iterator it = interactionInstance.getStepsByDefinitionName(
151: "Select column presentation").iterator();
152: while (it.hasNext()) {
153: StepInstance step = (StepInstance) it.next();
154: if ("completed".equals(step.getStatus())
155: && viewName.equals(step.getProperty("viewName",
156: true))) {
157: presentationId = (String) step.getProperty("columns/"
158: + getId());
159: break;
160: }
161: }
162:
163: if (presentationId == null) {
164: return "<!-- Column " + getName()
165: + ": Presentation is not set -->";
166: }
167:
168: PropertySet presentationParameters = new TransientPropertySet();
169: it = interactionInstance.getStepsByDefinitionName(
170: "Presentation configuration").iterator();
171: while (it.hasNext()) {
172: StepInstance step = (StepInstance) it.next();
173: if ("completed".equals(step.getStatus())
174: && viewName.equals(step.getProperty("viewName",
175: true))
176: && String.valueOf(getId()).equals(
177: step.getProperty("column"))) {
178: presentationParameters = step.getProperties()
179: .getSubset("parameter/");
180: break;
181: }
182: }
183:
184: TemplateFactory templateFactory = getOwner().getOwner()
185: .getOwner().getOwner().getTemplateFactory();
186: if (templateFactory == null) {
187: return "<!-- Column " + getName()
188: + ": Template factory is not set -->";
189: }
190:
191: Template template = templateFactory.getTemplate(Integer
192: .parseInt(presentationId));
193: Map context = new HashMap();
194: context.put("column", this );
195: context.put("presentationParameters", presentationParameters);
196:
197: final PropertySet pp = presentationParameters;
198:
199: context.put("presentationParametersHelper",
200: new PresentationParametersHelper() {
201:
202: public String getIf(String parameterName,
203: String value, String ifTrue, String ifFalse) {
204: return value.equals(pp.get(parameterName)) ? ifTrue
205: : ifFalse;
206: }
207:
208: public String getIf(String parameterName,
209: String value, String ifTrue) {
210: return getIf(parameterName, value, ifTrue, "");
211: }
212:
213: });
214: return template.evaluate(context, getClass().getClassLoader());
215: }
216:
217: /**
218: * Helper interface.
219: * @author Pavel Vlasov
220: */
221: public interface PresentationParametersHelper {
222:
223: /**
224: * Helper method.
225: * @param parameterName Parameter name
226: * @param value value
227: * @param ifTrue Return value if parameter is equal to the value
228: * @param ifFalse Return value if parameter is not equal to the value.
229: * @return ifTrue if parameter value equals to the passed value, ifFalse otherwise
230: */
231: public String getIf(String parameterName, String value,
232: String ifTrue, String ifFalse);
233:
234: /**
235: * Helper method.
236: * @param parameterName Parameter name
237: * @param value value
238: * @param ifTrue Return value if parameter is equal to the value
239: * @return ifTrue if parameter value equals to the passed value, Empty string otherwise
240: */
241: public String getIf(String parameterName, String value,
242: String ifTrue);
243: }
244:
245: /**
246: * Generates code from template specified for given column for given view
247: * @param viewName
248: * @return
249: * @throws SQLException
250: * @throws HammurapiWebException
251: * @throws
252: */
253: public Template getTemplate(String viewName) throws SQLException,
254: HammurapiWebException {
255: InteractionInstance interactionInstance = getOwner()
256: .getInteractionInstance();
257: if (interactionInstance == null) {
258: return null;
259: }
260:
261: String presentationId = null;
262: Iterator it = interactionInstance.getStepsByDefinitionName(
263: "Select column presentation").iterator();
264: while (it.hasNext()) {
265: StepInstance step = (StepInstance) it.next();
266: String svn = (String) step.getProperty("viewName", true);
267: if (svn != null) {
268: svn = svn.trim();
269: }
270: if ("completed".equals(step.getStatus())
271: && viewName.equals(svn)) {
272: presentationId = (String) step.getProperty("columns/"
273: + getId());
274: break;
275: }
276: }
277:
278: if (presentationId == null) {
279: return null;
280: }
281:
282: TemplateFactory templateFactory = getOwner().getOwner()
283: .getOwner().getOwner().getTemplateFactory();
284: if (templateFactory == null) {
285: return null;
286: }
287: return templateFactory.getTemplate(Integer
288: .parseInt(presentationId));
289: }
290:
291: public boolean isPrimaryKey() {
292: Iterator it = getOwner().getPrimaryKeys().iterator();
293: while (it.hasNext()) {
294: Map metaMap = (Map) it.next();
295: if (getName().equals(metaMap.get("COLUMN_NAME"))) {
296: return true;
297: }
298: }
299: return false;
300: }
301:
302: // public String getTemplateParameter(String viewName, String parameterName) {
303: //
304: // }
305: }
|