KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > utility > WindowTree


1 /*
2  *************************************************************************
3  * The contents of this file are subject to the Openbravo Public License
4  * Version 1.0 (the "License"), being the Mozilla Public License
5  * Version 1.1 with a permitted attribution clause; you may not use this
6  * file except in compliance with the License. You may obtain a copy of
7  * the License at http://www.openbravo.com/legal/license.html
8  * Software distributed under the License is distributed on an "AS IS"
9  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
10  * License for the specific language governing rights and limitations
11  * under the License.
12  * The Original Code is Openbravo ERP.
13  * The Initial Developer of the Original Code is Openbravo SL
14  * All portions are Copyright (C) 2001-2006 Openbravo SL
15  * All Rights Reserved.
16  * Contributor(s): ______________________________________.
17  ************************************************************************
18 */

19 package org.openbravo.erpCommon.utility;
20
21 import org.openbravo.base.secureApp.*;
22 import org.openbravo.data.Sqlc;
23 import org.openbravo.xmlEngine.XmlDocument;
24 import java.io.*;
25 import javax.servlet.*;
26 import javax.servlet.http.*;
27 import org.openbravo.utils.Replace;
28 import org.openbravo.utils.FormatUtilities;
29
30
31
32 public class WindowTree extends HttpSecureAppServlet {
33   
34   static final String JavaDoc CHILD_SHEETS="frameWindowTreeF3";
35
36   public void init (ServletConfig config) {
37     super.init(config);
38     boolHist = false;
39   }
40   
41   public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
42     VariablesSecureApp vars = new VariablesSecureApp(request);
43     if (vars.commandIn("DEFAULT", "TAB")) {
44       String JavaDoc strTabId = vars.getGlobalVariable("inpTabId", "WindowTree|tabId");
45       String JavaDoc strTreeID="";
46       String JavaDoc key=WindowTreeData.selectKey(this, strTabId);
47       {
48         String JavaDoc TreeType = WindowTreeUtility.getTreeType(key);
49         WindowTreeData[] data = WindowTreeData.selectTreeID(this, vars.getClient(), TreeType);
50         if (data!=null && data.length>0) strTreeID = data[0].id;
51       }
52       WindowTreeData[] data = WindowTreeData.selectTabName(this, strTabId);
53       String JavaDoc windowName = FormatUtilities.replace(data[0].description);
54       String JavaDoc tabName = FormatUtilities.replace(data[0].name);
55       String JavaDoc strHref = windowName + "/" + tabName + "_Relation.html?Command=" + vars.getCommand();
56       {
57         WindowTreeData[] parents = WindowTreeData.selectParents(this, strTabId);
58         if (parents!=null && parents.length>0) {
59           for (int i=0;i<parents.length;i++) {
60             String JavaDoc strField = "inp" + Sqlc.TransformaNombreColumna(parents[i].name);
61             String JavaDoc strData = vars.getGlobalVariable(strField, parents[i].nodeId + "|" + parents[i].name);
62             strHref += "&" + strField + "=" + strData;
63           }
64         }
65       }
66       if (strTreeID.equals("")) advisePopUp(response, "Error", Utility.messageBD(this, "AccessTableNoView", vars.getLanguage()));//response.sendRedirect(strDireccion + "/" + strHref);
67
else printPageDataSheet(response, vars, strTabId);
68     } else if (vars.commandIn("ASSIGN")) {
69       String JavaDoc strTabId = vars.getRequiredStringParameter("inpTabId");
70       String JavaDoc strTop = vars.getRequiredStringParameter("inpTop");
71       String JavaDoc strLink = vars.getRequiredStringParameter("inpLink");
72       String JavaDoc strChild = vars.getStringParameter("inpChild", "N");
73       String JavaDoc strResult = WindowTreeChecks.checkChanges(this, vars, strTabId, strTop, strLink, strChild.equals("Y"));
74       if (strResult.equals("")) changeNode(vars, strTabId, strTop, strLink, strChild);
75       else vars.setSessionValue("WindowTree|message", strResult);
76       vars.setSessionValue("WindowTree|tabId", strTabId);
77       response.sendRedirect(strDireccion + request.getServletPath() + "?Command=DEFAULT&inpTabId=" + strTabId);
78     } else throw new ServletException();
79   }
80
81   public String JavaDoc loadNodes(VariablesSecureApp vars, String JavaDoc key, boolean editable, String JavaDoc strTabId) throws ServletException {
82     String JavaDoc TreeType = WindowTreeUtility.getTreeType(key);
83     String JavaDoc TreeID="";
84     String JavaDoc TreeName="";
85     String JavaDoc TreeDescription="";
86     WindowTreeData[] data = WindowTreeData.selectTreeID(this, vars.getClient(), TreeType);
87     StringBuffer JavaDoc menu = new StringBuffer JavaDoc();
88     if (data==null || data.length==0) {
89       log4j.error("WindowTree.loadNodes() - Unknown TreeNode: TreeType " + TreeType + " - TreeKey " + key);
90       throw new ServletException("WindowTree.loadNodes() - Unknown TreeNode");
91     } else {
92       TreeID = data[0].id;
93       TreeName = data[0].name;
94       TreeDescription = data[0].description;
95     }
96     if (log4j.isDebugEnabled()) log4j.debug("WindowTree.loadNodes() - TreeType: " + TreeType + " || TreeID: " + TreeID);
97     menu.append("<ul>\n");
98     menu.append(WindowTreeUtility.addNodeElement(TreeName, TreeDescription, CHILD_SHEETS, true, "", strDireccion, "clickItem(0, '" + Replace.replace(TreeName, "'", "\\'") + "', 'N');", "dblClickItem(0);", true, "0", ""));
99     //menu.append(WindowTreeUtility.addNodeElement(TreeName, TreeDescription, CHILD_SHEETS, true, "", strDireccion, "clickItem(0, '" + Replace.replace(TreeName, "'", "\\'") + "', 'N');", "", true, "0", ""));
100
menu.append(generateTree(WindowTreeUtility.getTree(this, vars, TreeType, TreeID, editable, "", "", strTabId), strDireccion, "0", true));
101     menu.append("</ul>\n");
102     return menu.toString();
103   }
104
105   public String JavaDoc loadChildNodes(VariablesSecureApp vars, String JavaDoc key, String JavaDoc strTreeID, String JavaDoc strParentID, boolean editable, String JavaDoc strTabId) throws ServletException {
106     String JavaDoc TreeType = WindowTreeUtility.getTreeType(key);
107     StringBuffer JavaDoc menu = new StringBuffer JavaDoc();
108     menu.append("<ul>\n");
109     StringBuffer JavaDoc script = new StringBuffer JavaDoc();
110     script.append(generateTree(WindowTreeUtility.getTree(this, vars, TreeType, strTreeID, editable, strParentID, "", strTabId), strDireccion, strParentID, true));
111     if (!script.equals("")) {
112       String JavaDoc TreeName="", TreeDescription="";
113       WindowTreeData[] data=null;
114       if (strParentID.equals("0")) data = WindowTreeData.selectTreeID(this, vars.getClient(), TreeType);
115       else data = WindowTreeUtility.getTree(this, vars, TreeType, strTreeID, editable, "", strParentID, strTabId);
116       if (data==null || data.length==0) {
117         log4j.error("WindowTree.loadNodes() - Unknown TreeNode");
118         throw new ServletException("WindowTree.loadNodes() - Unknown TreeNode");
119       } else {
120         TreeName = data[0].name;
121         TreeDescription = data[0].description;
122       }
123       menu.append(WindowTreeUtility.addNodeElement(TreeName, TreeDescription, CHILD_SHEETS, true, "", strDireccion, "", "", false, "0", ""));
124       menu.append(script);
125     }
126     menu.append("</ul>\n");
127     return menu.toString();
128   }
129
130   public String JavaDoc generateTree(WindowTreeData[] data, String JavaDoc strDireccion, String JavaDoc indice, boolean isFirst) {
131     if (data==null || data.length==0) return "";
132     if (log4j.isDebugEnabled()) log4j.debug("WindowTree.generateTree() - data: " + data.length);
133     if (indice == null) indice="0";
134     boolean hayDatos=false;
135     StringBuffer JavaDoc strCabecera = new StringBuffer JavaDoc();
136     StringBuffer JavaDoc strResultado= new StringBuffer JavaDoc();
137     strCabecera.append("<ul" + ((indice.equals("0") || isFirst)?"":" style='display:none;'") + ">");
138     isFirst=false;
139     for (int i=0;i<data.length;i++) {
140       if (data[i].parentId.equals(indice)) {
141         hayDatos=true;
142         String JavaDoc strHijos = generateTree(data, strDireccion, data[i].nodeId, isFirst);
143         strResultado.append(WindowTreeUtility.addNodeElement(data[i].name, data[i].description, CHILD_SHEETS, data[i].issummary.equals("Y"), WindowTreeUtility.windowType(data[i].action), strDireccion, "clickItem(" + data[i].nodeId + ", '" + Replace.replace(data[i].name, "'", "\\'") + "', '" + data[i].issummary + "');", "dblClickItem(" + data[i].nodeId + ");", !strHijos.equals(""), data[i].nodeId, data[i].action));
144         //strResultado.append(WindowTreeUtility.addNodeElement(data[i].name, data[i].description, CHILD_SHEETS, data[i].issummary.equals("Y"), WindowTreeUtility.windowType(data[i].action), strDireccion, "clickItem(" + data[i].nodeId + ", '" + Replace.replace(data[i].name, "'", "\\'") + "', '" + data[i].issummary + "');", "", !strHijos.equals(""), data[i].nodeId, data[i].action));
145
strResultado.append(strHijos);
146       }
147     }
148     return (hayDatos?(strCabecera.toString() + strResultado.toString() + "</ul>"):"");
149   }
150
151   void printPageDataSheet(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc TabId) throws IOException, ServletException {
152     if (log4j.isDebugEnabled()) log4j.debug("Output: Tree's screen for the tab: " + TabId);
153     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/utility/WindowTree").createXmlDocument();
154     
155     xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";");
156     xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n");
157     String JavaDoc strTreeID="";
158     String JavaDoc key=WindowTreeData.selectKey(this, TabId);
159     {
160       String JavaDoc TreeType = WindowTreeUtility.getTreeType(key);
161       WindowTreeData[] data = WindowTreeData.selectTreeID(this, vars.getClient(), TreeType);
162       if (data!=null && data.length>0) strTreeID = data[0].id;
163     }
164     WindowTreeData[] data = WindowTreeData.selectTabName(this, TabId);
165     String JavaDoc windowName = FormatUtilities.replace(data[0].description);
166     String JavaDoc tabName = FormatUtilities.replace(data[0].name);
167     xmlDocument.setParameter("description", data[0].name);
168     xmlDocument.setParameter("page", "../" + windowName + "/" + tabName + "_Edition.html");
169     xmlDocument.setParameter("menu", loadNodes(vars, key, WindowTreeData.selectEditable(this, TabId).equals("Y"), TabId));
170     xmlDocument.setParameter("treeID", strTreeID);
171     xmlDocument.setParameter("tabID", TabId);
172     key = "inp" + Sqlc.TransformaNombreColumna(key);
173     xmlDocument.setParameter("keyField", key);
174     xmlDocument.setParameter("keyFieldScript", "function getKeyField() {\n return document.frmMain." + key + ";\n}\n");
175     
176     {
177       OBError myMessage = vars.getMessage("WindowTree");
178       vars.removeMessage("WindowTree");
179       if (myMessage!=null) {
180         xmlDocument.setParameter("messageType", myMessage.getType());
181         xmlDocument.setParameter("messageTitle", myMessage.getTitle());
182         xmlDocument.setParameter("messageMessage", myMessage.getMessage());
183       }
184     }
185
186     response.setContentType("text/html; charset=UTF-8");
187     PrintWriter out = response.getWriter();
188     out.println(xmlDocument.print());
189     out.close();
190   }
191
192   void changeNode(VariablesSecureApp vars, String JavaDoc strTabId, String JavaDoc strTop, String JavaDoc strLink, String JavaDoc strChild) throws ServletException {
193     String JavaDoc key = WindowTreeData.selectKey(this, strTabId);
194     String JavaDoc TreeType = WindowTreeUtility.getTreeType(key);
195     String JavaDoc TreeID="";
196     String JavaDoc strParent=strTop;
197     boolean editable = WindowTreeData.selectEditable(this, strTabId).equals("Y");
198     //Calculating the TreeID
199
{
200       WindowTreeData[] data = WindowTreeData.selectTreeID(this, vars.getClient(), TreeType);
201       if (data==null || data.length==0) {
202         log4j.error("WindowTree.loadNodes() - Unknown TreeNode");
203         throw new ServletException("WindowTree.loadNodes() - Unknown TreeNode");
204       } else {
205         TreeID = data[0].id;
206       }
207     }
208     //Calculating the parent
209
{
210       WindowTreeData[] data = WindowTreeUtility.getTree(this, vars, TreeType, TreeID, editable, "", strTop, strTabId);
211       if (data==null || data.length==0) {
212         log4j.error("WindowTree.loadNodes() - Unknown Top Node");
213         throw new ServletException("WindowTree.loadNodes() - Unknown Top Node");
214       }
215       
216       if (!data[0].issummary.equals("Y") || !strChild.equals("Y")) {
217         strParent = data[0].parentId;
218       }
219     }
220     WindowTreeData[] data = WindowTreeUtility.getTree(this, vars, TreeType, TreeID, editable, strParent, "", strTabId);
221     int seqNo=10;
222     try {
223       if (data==null || data.length==0) {
224         WindowTreeUtility.setNode(this, vars, TreeType, TreeID, strParent, strLink, Integer.toString(seqNo));
225       } else {
226         boolean updated=false;
227         if (strParent.equals(strTop)) {
228           WindowTreeUtility.setNode(this, vars, TreeType, TreeID, strParent, strLink, Integer.toString(seqNo));
229           seqNo += 10;
230           updated=true;
231         }
232         for (int i=0;i<data.length;i++) {
233           if (!data[i].nodeId.equals(strLink)) {
234             WindowTreeUtility.setNode(this, vars, TreeType, TreeID, data[i].parentId, data[i].nodeId, Integer.toString(seqNo));
235             seqNo += 10;
236             if (!updated && data[i].nodeId.equals(strTop)) {
237               WindowTreeUtility.setNode(this, vars, TreeType, TreeID, strParent, strLink, Integer.toString(seqNo));
238               seqNo += 10;
239               updated=true;
240             }
241           }
242         }
243         if (!updated) WindowTreeUtility.setNode(this, vars, TreeType, TreeID, strParent, strLink, Integer.toString(seqNo));
244       }
245     } catch (ServletException e) {
246       log4j.error("WindowTree.changeNode() - Couldn't change the node: " + strLink);
247       log4j.error("WindowTree.setNode() - error: " + e);
248       throw new ServletException(e);
249     }
250   }
251
252   public String JavaDoc getServletInfo() {
253     return "Servlet that presents the tree of a TreeNode windo windoww";
254   } // end of getServletInfo() method
255
}
256
Popular Tags