KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > editor > YAMMEditor


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib.editor;
11
12 import java.io.IOException JavaDoc;
13 import java.util.*;
14
15 import org.mmbase.bridge.*;
16 import org.mmbase.util.functions.*;
17 import org.mmbase.storage.search.*;
18 import org.mmbase.util.logging.Logger;
19 import org.mmbase.util.logging.Logging;
20 import javax.servlet.jsp.PageContext JavaDoc;
21
22 /**
23  * This is an example implementation of the EditTag. It extends the implementation
24  * class Editor of EditTag. To create your own editor with the edittag you could
25  * or rather should also extend Editor.<br />
26  * EditTagYAMMe works together with the editor yammeditor.jsp. It creates an URL
27  * that looks something like this:<br />
28  * yammeditor.jsp?nrs=76&fields=76_number;76_title;76_subtitle;76_intro;80_gui();
29  *
30  * @author Andr&eacute; van Toly
31  * @version $Id: YAMMEditor.java,v 1.10 2006/07/06 11:36:12 michiel Exp $
32  * @see EditTag
33  * @see BasicEditor
34  * @since MMBase-1.8
35  */

36
37 public class YAMMEditor extends Editor {
38
39     private static final Logger log = Logging.getLoggerInstance(YAMMEditor.class);
40
41     private static final Parameter[] PARAMS = new Parameter[] {
42         new Parameter("url", String JavaDoc.class, "/mmbase/edit/yammeditor/yammeditor.jsp"),
43         new Parameter("icon", String JavaDoc.class, "/mmbase/style/images/change.gif")
44     };
45
46
47     protected Parameter[] getParameterDefinition() {
48         return PARAMS;
49     }
50
51
52
53     private List startList = new ArrayList(); // startnodes: 346
54
private List pathList = new ArrayList(); // paths: 346_news,posrel,urls
55
private List nList = new ArrayList(); // nodes: 346_602
56
private List fList = new ArrayList(); // 602_news.title
57
// Map to accommadate the fields and their startnodes
58
private Map fld2snMap = new HashMap();
59
60
61     /**
62      * Create a string containing all the needed html to find the editor. Or rather
63      * it calls one of the older methods i wrote, that was easier then rewriting
64      * the old method.
65      *
66      * @param context PageContext used to write a string with a link and an icon to access yammeditor.jsp
67      */

68     public void getEditorHTML(PageContext JavaDoc context) throws IOException JavaDoc {
69         String JavaDoc html = "Sorry. You should see an icon and a link to yammeditor here.";
70
71         String JavaDoc url = parameters.getString("url");
72         String JavaDoc icon = parameters.getString("icon");
73
74         url = makeRelative(url, context);
75         icon = makeRelative(icon, context);
76         html = makeHTML(url, icon);
77
78         log.debug("returning: " + html);
79         context.getOut().write(html);
80     }
81
82     /**
83      * Values passed by the EditTag from the FieldTags.
84      *
85      * @param queryList List with SearchQuery objects from fields
86      * @param nodenrList List with nodenumbers
87      * @param fieldList List with fieldnames
88      */

89     public void registerFields(List queryList, List nodenrList, List fieldList) {
90         log.debug("processing fields");
91         for (int i = 0; i < nodenrList.size(); i++) {
92             String JavaDoc fldName = (String JavaDoc) fieldList.get(i);
93             log.debug("processing field '" + fldName + "'");
94             Query query = (Query) queryList.get(i);
95             String JavaDoc nodenr = (String JavaDoc) nodenrList.get(i);
96
97             processField(query, nodenr, fldName);
98         }
99
100         // clear the lists
101
//queryList.clear();
102
//nodenrList.clear();
103
//fieldList.clear();
104
}
105
106     /**
107      * Processes a field with nodenr and query into several lists
108      *
109      * @param query SearchQuery that delivered the field
110      * @param nodenr Nodenumber of the node the field belongs to
111      * @param fieldName Name of the field
112      */

113     protected void processField(Query query, String JavaDoc nodenr, String JavaDoc fieldName) {
114
115         // fill paths
116
String JavaDoc path = getPathFromQuery(query);
117         if (!path.equals("") && !pathList.contains(path)) {
118             pathList.add(path);
119             log.debug("Added path : " + path);
120         }
121
122         List nl = getNodesFromQuery(query, nodenr);
123         Iterator e = nl.iterator(); // iterate over the startnodes
124
while (e.hasNext()) {
125             String JavaDoc nr = (String JavaDoc)e.next();
126             boolean startnode = false;
127
128             /* fills fld2snMap (only used to keep track of startnodes,
129             it contains nodenr's
130             when a nr is found in this map ...
131             */

132
133             if (!fld2snMap.containsValue(nr) ) {
134                 fld2snMap.put(String.valueOf(nodenr), nr);
135                 log.debug("Added nodenr : " + nodenr + " sn : " + nr + " to fld2snMap");
136             } else if (fld2snMap.isEmpty()) {
137                 fld2snMap.put(String.valueOf(nodenr), nr);
138                 startnode = true;
139                 log.debug("Added nodenr (startnode): " + nodenr + " sn : " + nr + " to fld2snMap");
140             } else { // a node is a startnode when there was
141
startnode = true; // no previous field with this nodenr as startnodenr
142
}
143
144             // fill startList (startnodes)
145
if (!startList.contains(nr) && startnode) { // 507 (= just startnodenr)
146
startList.add(nr);
147                 log.debug("Added startnode : " + nr);
148             }
149
150             // fill nodeList (just the nodes in a page)
151
String JavaDoc str = nr + "_" + String.valueOf(nodenr); // 507_234 (= startnodenr_nodenr)
152
if (!nList.contains(str)) {
153                 nList.add(str);
154                 log.debug("Added nodenr : " + str);
155             }
156
157             // fill fieldList (all the used fields in a page) // 507_title (= startnodenr_fieldname)
158
String JavaDoc fieldstr = nr + "_" + fieldName;
159             if (!fList.contains(fieldstr)) {
160                 fList.add(fieldstr);
161                 log.debug("Added field : " + fieldstr);
162             }
163         }
164
165     }
166
167     protected List getNodesFromQuery(Query query, String JavaDoc nr) {
168         List nl = new ArrayList();
169         List steps = query.getSteps();
170
171         if (steps.size() == 1) { // why ?
172
nl.add(nr);
173             log.debug("1. added nr to list of all the nodes in query: " + nr);
174         }
175
176         Iterator si = steps.iterator();
177         while (si.hasNext()) {
178             Step step = (Step) si.next();
179
180             // Get the nodes from this step
181
// (haalt alle nodes uit step, itereert erover en stopt ze in nl )
182
SortedSet nodeSet = step.getNodes();
183             for (Iterator nsi = nodeSet.iterator(); nsi.hasNext();) {
184                 Integer JavaDoc n = (Integer JavaDoc)nsi.next();
185                 nr = String.valueOf(n);
186
187                 if (!nl.contains(nr)) {
188                     nl.add(nr);
189                     log.debug("2. added nr to list of all the nodes in query: " + nr);
190                 }
191             }
192
193         }
194         return nl;
195     }
196
197
198     /**
199      * Generates pathList with the needed paths for the editor from an list
200      * with queries.
201      *
202      * @param ql List with queries
203      * @return List with paths from #getPathFromQuery
204      */

205     protected List fillPathList(List ql) {
206         List pl = new ArrayList();
207
208         Iterator i = ql.iterator();
209         while (i.hasNext()) {
210             Query q = (Query) i.next();
211             String JavaDoc path = getPathFromQuery(q);
212             if (!path.equals("") && !pl.contains(path)) {
213                 pl.add(path);
214                 log.debug("Added path '" + path + "' to pl");
215             }
216         }
217         log.debug("Filled pathList with " + pl.size() + " paths.");
218
219         return pl;
220     }
221
222     /**
223     * Get the path from this query where the field originated from.
224     * Returns an empty String when there is no path (startnode f.e.).
225     *
226     * @param query The query
227     * @return A String containing a path in the form 345_news,posrel,urls
228     * meaning: startnode(s)_path
229     */

230     protected String JavaDoc getPathFromQuery(Query query) {
231         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
232
233         java.util.List JavaDoc steps = query.getSteps();
234         log.debug("Nr of steps : " + steps.size());
235         if (steps.size() > 1) { // no need to look for a path when there is just 1 step
236
Iterator si = steps.iterator();
237             while (si.hasNext()) {
238                 Step step = (Step) si.next();
239
240                 String JavaDoc nodenrs = "";
241                 SortedSet nodeSet = step.getNodes(); // Get the (start?)nodes from this step
242
for (Iterator nsi = nodeSet.iterator(); nsi.hasNext();) {
243                     Integer JavaDoc number = (Integer JavaDoc)nsi.next();
244                     if (nodenrs.equals("")) {
245                         nodenrs = String.valueOf(number);
246                     } else {
247                         nodenrs = nodenrs + "," + String.valueOf(number);
248                     }
249
250                 }
251
252                 // path: Get one nodetype at the time (the steps)
253
if (step.getAlias() != null) {
254                     if (path.length() == 0) {
255                         path.append(nodenrs).append('_').append(step.getAlias());
256                     } else {
257                         path.append(",").append(step.getAlias());
258                     }
259                 }
260             }
261         }
262         return path.toString();
263     }
264
265     /**
266     * Creates a ; seperated String from a List to create the url to yammeditor with
267     * paths, fields, startnodes or whatnot
268     *
269     * @param al One of the Lists to use
270     * @return A ; seperated String with the elements in the List
271     *
272     */

273     protected String JavaDoc makeList4Url(List al) {
274         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
275         if (al.size() > 0) {
276             Iterator e = al.iterator();
277             while(e.hasNext()) {
278                 if (sb.length() == 0) {
279                     sb.append( (String JavaDoc) e.next() );
280                 } else {
281                     sb.append(';').append( (String JavaDoc) e.next() );
282                 }
283             }
284         }
285         return sb.toString();
286     }
287
288     /**
289     * Creates a string with the link (and icon) to the editor
290     *
291     * @param url An url to an editor
292     * @param icon An url to a graphic file
293     * @return An HTML string with a link suitable for the editor yammeditor.jsp
294     *
295     */

296     public String JavaDoc makeHTML(String JavaDoc url, String JavaDoc icon) {
297         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
298
299         html.append("<div class=\"et\"><a title=\"click to edit\" HREF=\"");
300         html.append(url);
301         html.append("?nrs=").append(makeList4Url(startList)); // startnodes: 676 (startnode)
302
html.append("&amp;paths=").append(makeList4Url(pathList)); // paths: 676_news,posrel,images (startnode_path)
303
html.append("&amp;fields=").append(makeList4Url(fList)); // fields: 345_images.number (startnode_nodetype.field)
304
html.append("&amp;nodes=").append(makeList4Url(nList)); // nodes: 676_345 (startnode_nodenr)
305
html.append("\" onclick=\"window.open(this.href); return false;\">");
306
307         if (!icon.equals("")) {
308             html.append("<img SRC=\"").append(icon).append("\" alt=\"edit\">");
309         } else {
310             html.append("edit");
311         }
312
313         html.append("</a></div>");
314
315         return html.toString();
316     }
317
318     /**
319      * Copied this method from the UrlTag.
320      * If it would be nice that an URL starting with '/' would be generated relatively to the current request URL, then this method can do it.
321      * If the URL is not used to write to (this) page, then you probably don't want that.
322      *
323      * The behaviour can be overruled by starting the URL with two '/'s.
324      *
325      */

326     protected String JavaDoc makeRelative(String JavaDoc url, PageContext JavaDoc pageContext) {
327         StringBuffer JavaDoc show = new StringBuffer JavaDoc(url);
328         javax.servlet.http.HttpServletRequest JavaDoc req = (javax.servlet.http.HttpServletRequest JavaDoc)pageContext.getRequest();
329         if (show.charAt(0) == '/') { // absolute on servletcontex
330
if (show.length() > 1 && show.charAt(1) == '/') {
331                 log.debug("'absolute' url, not making relative");
332                 /*
333                 if (addContext()) {
334                     show.deleteCharAt(0);
335                     show.insert(0, req.getContextPath());
336                 }
337                 */

338             } else {
339                 log.debug("'absolute' url");
340                 String JavaDoc thisDir = new java.io.File JavaDoc(req.getServletPath()).getParent();
341                 show.insert(0, org.mmbase.util.UriParser.makeRelative(thisDir, "/")); // makes a relative path to root.
342
}
343         }
344         return show.toString();
345     }
346
347 }
348
Popular Tags