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.analysis;
024:
025: import java.io.UnsupportedEncodingException;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.Properties;
033: import java.util.TreeMap;
034:
035: import org.apache.xpath.CachedXPathAPI;
036: import org.w3c.dom.Element;
037:
038: import biz.hammurapi.config.ConfigurationException;
039: import biz.hammurapi.sql.DataAccessObject;
040: import biz.hammurapi.sql.SQLProcessor;
041: import biz.hammurapi.util.CollectionVisitable;
042: import biz.hammurapi.util.Visitable;
043: import biz.hammurapi.util.Visitor;
044: import biz.hammurapi.web.analysis.sql.AnalysisEngine;
045: import biz.hammurapi.web.analysis.sql.AnalysisTermImpl;
046: import biz.hammurapi.web.file.sql.FileEngine;
047: import biz.hammurapi.web.file.sql.FileInfo;
048: import biz.hammurapi.web.menu.Menu;
049: import biz.hammurapi.web.util.Escaper;
050: import biz.hammurapi.xml.dom.DOMUtils;
051:
052: public class AnalysisTermEx extends AnalysisTermImpl implements
053: DataAccessObject, Visitable {
054:
055: public static final String RO_TERM_TOOLTIP_PREFIX = "roTermTooltip";
056:
057: public static final String TERM_TOOLTIP_PREFIX = "termTooltip";
058:
059: public static final String TERM_TOOLTIP_SUFFIX = ".html";
060:
061: public static final String TOOLTIP_URL = "/ef/system/analysis.AnalysisActions/getTermTooltip/";
062:
063: public static final String TERM_URL = "/ef/xmenu/system/analysis.AnalysisActions/viewGlossary/analysis/viewGlossary.html?ID=";
064:
065: public static final String TERM_RO_URL = "/ef/xmenu/system/analysis.AnalysisActions/viewGlossaryRo/";
066:
067: public AnalysisTermEx() {
068: super ();
069: }
070:
071: public AnalysisTermEx(boolean force) {
072: super (force);
073: }
074:
075: public AnalysisTermEx(Element holder, boolean force)
076: throws ConfigurationException {
077: super (holder, force);
078: }
079:
080: public AnalysisTermEx(Element holder, Properties nameMap,
081: CachedXPathAPI cxpa, boolean force)
082: throws ConfigurationException {
083: super (holder, nameMap, cxpa, force);
084: }
085:
086: public AnalysisTermEx(ResultSet rs) throws SQLException {
087: super (rs);
088: }
089:
090: private boolean isReadOnlyView;
091:
092: /**
093: * Sets readOnly flag. If this flag is set expanded term links point to glossary read-only view.
094: * @param isReadOnlyView
095: */
096: public void setReadOnlyView(boolean isReadOnlyView) {
097: this .isReadOnlyView = isReadOnlyView;
098: }
099:
100: public boolean isReadOnlyView() {
101: return isReadOnlyView;
102: }
103:
104: private String alternativeText;
105:
106: public void setAlternativeText(String alternativeText) {
107: this .alternativeText = alternativeText;
108: }
109:
110: public String getAlternativeText() {
111: return alternativeText;
112: }
113:
114: /**
115: * Renders term link.
116: */
117: public String toString() {
118: StringBuffer ret = new StringBuffer();
119: if (!Menu.isBlank(getDescription())) {
120: ret
121: .append("<a style=\"display:none\" href=\"${context-path}"
122: + TOOLTIP_URL);
123: if (isReadOnlyView()) {
124: ret.append(RO_TERM_TOOLTIP_PREFIX);
125: } else {
126: ret.append(TERM_TOOLTIP_PREFIX);
127: }
128: ret.append(getId());
129: ret.append(TERM_TOOLTIP_SUFFIX);
130: ret.append("\" id=\"termTooltip" + getId() + "\" >Term "
131: + getId() + "</a>");
132: }
133: ret
134: .append("<a style=\"TEXT-DECORATION:none;font:bold;color:black;border-bottom:gray dotted 1px\" ");
135:
136: // Show tooltip only if there is content to show.
137: if (!Menu.isBlank(getDescription())) {
138: ret
139: .append("onmouseover=\"helpTooltip.showTooltip(event,document.getElementById('termTooltip"
140: + getId() + "'));return false;\"");
141: ret.append("onmouseout=\"helpTooltip.hideTooltip()\" ");
142: }
143: if (glossaryInfo != null) {
144: Escaper escaper = new Escaper();
145: ret.append("href=\"${context-path}");
146: if (isReadOnlyView()) {
147: ret.append(TERM_RO_URL);
148: ret.append(getGlossaryId());
149: ret.append("/");
150: try {
151: ret.append(escaper.url(glossaryInfo.getName()));
152: } catch (UnsupportedEncodingException e) {
153: ret.append(getName());
154: }
155: ret.append(".html#Term");
156: ret.append(getId());
157: } else {
158: ret.append(TERM_URL);
159: ret.append(getGlossaryId());
160: ret.append("#Term");
161: ret.append(getId());
162: }
163: ret.append("\"");
164: }
165:
166: ret.append(">");
167:
168: ret.append(Menu.isBlank(getAlternativeText()) ? getName()
169: : getAlternativeText());
170: ret.append("</a>");
171: return ret.toString();
172: }
173:
174: private Map subTerms = new TreeMap();
175: private Collection children = new ArrayList();
176:
177: private FileInfo glossaryInfo;
178:
179: public void toDom(Element holder) {
180: super .toDom(holder);
181: holder.setAttribute("ref-count", String.valueOf(refCount));
182: DOMUtils.toDom(subTerms, "sub-terms", holder);
183: }
184:
185: public void setSQLProcessor(SQLProcessor processor)
186: throws SQLException {
187: AnalysisEngine engine = new AnalysisEngine(processor);
188: Iterator it = engine.getAnalysisTermByParent(
189: new Integer(getId()), children, getClass()).iterator();
190: while (it.hasNext()) {
191: AnalysisTermEx term = (AnalysisTermEx) it.next();
192: term.setParent(this );
193: String cName = Menu.isBlank(term.getCategory()) ? "" : term
194: .getCategory();
195: Collection cc = (Collection) subTerms.get(cName);
196: if (cc == null) {
197: cc = new ArrayList();
198: subTerms.put(cName, cc);
199: }
200: cc.add(term);
201: }
202:
203: if (getGlossaryId() != null) {
204: glossaryInfo = new FileEngine(processor)
205: .getFileInfo(getGlossaryId().intValue());
206: }
207: }
208:
209: public Map getSubTerms() {
210: return subTerms;
211: }
212:
213: public Collection getChildren() {
214: return children;
215: }
216:
217: private int refCount;
218:
219: void incRef() {
220: ++refCount;
221: if (parent != null) {
222: parent.incRef();
223: }
224: }
225:
226: public int getRefCount() {
227: return refCount;
228: }
229:
230: private AnalysisTermEx parent;
231:
232: void setParent(AnalysisTermEx parent) {
233: this .parent = parent;
234: setParentId(parent == null ? null : new Integer(parent.getId()));
235: }
236:
237: public AnalysisTermEx getParent() {
238: return parent;
239: }
240:
241: public boolean accept(Visitor visitor) {
242: if (visitor.visit(this )) {
243: new CollectionVisitable(getChildren(), false)
244: .accept(visitor);
245: return true;
246: }
247: return false;
248: }
249:
250: }
|