001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010:
011: package org.mmbase.model;
012:
013: import org.mmbase.util.logging.*;
014: import org.mmbase.module.core.*;
015: import org.mmbase.util.*;
016: import org.mmbase.util.xml.*;
017:
018: import org.w3c.dom.*;
019: import org.xml.sax.InputSource;
020:
021: import java.io.*;
022:
023: /**
024: * @javadoc
025: */
026: public class CloudModelBuilder {
027:
028: private static Logger log = Logging
029: .getLoggerInstance(CloudModelBuilder.class);
030: private String path;
031: private Document document;
032: private BuilderReader reader;
033:
034: public CloudModelBuilder(String name) {
035: }
036:
037: public void setPath(String path) {
038: this .path = path;
039: }
040:
041: public boolean writeToFile(String filepath) {
042: InputStream in = ResourceLoader.getConfigurationRoot()
043: .getResourceAsStream(path);
044: if (in != null) {
045: try {
046: FileOutputStream out = new FileOutputStream(filepath);
047: byte[] buf = new byte[1024];
048: int len;
049: while ((len = in.read(buf)) > 0) {
050: out.write(buf, 0, len);
051: }
052: in.close();
053: out.flush();
054: out.close();
055: } catch (Exception e) {
056: e.printStackTrace();
057: return false;
058: }
059: } else {
060: log.error("Resource not found : " + path);
061: }
062: return true;
063: }
064:
065: public boolean removeField(String name) {
066: if (document == null)
067: openDocument();
068: Element fe = reader.getElementByPath(document
069: .getDocumentElement(), "builder.fieldlist");
070: if (fe != null) {
071: for (Element field : reader.getChildElements(fe, "field")) {
072: Element namenode = reader.getElementByPath(field,
073: "field.db.name");
074: if (namenode != null
075: && namenode.getFirstChild().getNodeValue()
076: .equals(name)) {
077: fe.removeChild(field);
078: save();
079: }
080: }
081: }
082: return true;
083: }
084:
085: public boolean setGuiName(String fieldname, String country,
086: String value) {
087: if (document == null)
088: openDocument();
089: Element fe = reader.getElementByPath(document
090: .getDocumentElement(), "builder.fieldlist");
091: if (fe != null) {
092: for (Element field : reader.getChildElements(fe, "field")) {
093: Element namenode = reader.getElementByPath(field,
094: "field.db.name");
095: if (namenode != null
096: && namenode.getFirstChild().getNodeValue()
097: .equals(fieldname)) {
098: // that we have found the correct field find
099: // find the gui names
100: Element guinode = reader.getElementByPath(field,
101: "field.gui");
102: if (guinode != null) {
103: boolean found = false;
104: for (Element guiname : reader.getChildElements(
105: guinode, "guiname")) {
106: String oldcountry = guiname
107: .getAttribute("xml:lang");
108: if (oldcountry != null
109: && oldcountry.equals(country)) {
110: guiname.getFirstChild().setNodeValue(
111: value);
112: save();
113: found = true;
114: }
115: }
116: if (!found) {
117: String newpart = " <guiname xml:lang=\""
118: + country
119: + "\">"
120: + value
121: + "</guiname>\r";
122: mergePart(guinode, newpart);
123: save();
124: }
125: } else {
126: String newpart = " <gui>\r";
127: newpart += " <guiname xml:lang=\""
128: + country + "\">" + value
129: + "</guiname>\r";
130: newpart += " </gui>\r";
131: mergePart(field, newpart);
132: save();
133: }
134: }
135: }
136: }
137: return true;
138: }
139:
140: public boolean setBuilderDBState(String fieldname, String value) {
141: if (document == null)
142: openDocument();
143: Element fe = reader.getElementByPath(document
144: .getDocumentElement(), "builder.fieldlist");
145: if (fe != null) {
146: for (Element field : reader.getChildElements(fe, "field")) {
147: Element namenode = reader.getElementByPath(field,
148: "field.db.name");
149: if (namenode != null
150: && namenode.getFirstChild().getNodeValue()
151: .equals(fieldname)) {
152: // that we have found the correct field find
153: // find the type node
154: Element typenode = reader.getElementByPath(field,
155: "field.db.type");
156: NamedNodeMap nnm = typenode.getAttributes();
157: if (nnm != null) {
158: Node dbstate = nnm.getNamedItem("state");
159: dbstate.getFirstChild().setNodeValue(value);
160: save();
161: }
162: }
163: }
164: }
165: return true;
166: }
167:
168: public boolean setBuilderDBKey(String fieldname, String value) {
169: if (document == null)
170: openDocument();
171: Element fe = reader.getElementByPath(document
172: .getDocumentElement(), "builder.fieldlist");
173: if (fe != null) {
174: for (Element field : reader.getChildElements(fe, "field")) {
175: Element namenode = reader.getElementByPath(field,
176: "field.db.name");
177: if (namenode != null
178: && namenode.getFirstChild().getNodeValue()
179: .equals(fieldname)) {
180: // that we have found the correct field find
181: // find the type node
182: Element typenode = reader.getElementByPath(field,
183: "field.db.type");
184: NamedNodeMap nnm = typenode.getAttributes();
185: if (nnm != null) {
186: Node key = nnm.getNamedItem("key");
187: key.getFirstChild().setNodeValue(value);
188: save();
189: }
190: }
191: }
192: }
193: return true;
194: }
195:
196: public boolean setBuilderDBNotNull(String fieldname, String value) {
197: if (document == null)
198: openDocument();
199: Element fe = reader.getElementByPath(document
200: .getDocumentElement(), "builder.fieldlist");
201: if (fe != null) {
202: for (Element field : reader.getChildElements(fe, "field")) {
203: Element namenode = reader.getElementByPath(field,
204: "field.db.name");
205: if (namenode != null
206: && namenode.getFirstChild().getNodeValue()
207: .equals(fieldname)) {
208: // that we have found the correct field find
209: // find the type node
210: Element typenode = reader.getElementByPath(field,
211: "field.db.type");
212: NamedNodeMap nnm = typenode.getAttributes();
213: if (nnm != null) {
214: Node notnull = nnm.getNamedItem("notnull");
215: notnull.getFirstChild().setNodeValue(value);
216: save();
217: }
218: }
219: }
220: }
221: return true;
222: }
223:
224: public boolean setBuilderDBSize(String fieldname, String value) {
225: if (document == null)
226: openDocument();
227: Element fe = reader.getElementByPath(document
228: .getDocumentElement(), "builder.fieldlist");
229: if (fe != null) {
230: for (Element field : reader.getChildElements(fe, "field")) {
231: Element namenode = reader.getElementByPath(field,
232: "field.db.name");
233: if (namenode != null
234: && namenode.getFirstChild().getNodeValue()
235: .equals(fieldname)) {
236: // that we have found the correct field find
237: // find the type node
238: Element typenode = reader.getElementByPath(field,
239: "field.db.type");
240: NamedNodeMap nnm = typenode.getAttributes();
241: if (nnm != null) {
242: Node dbsize = nnm.getNamedItem("size");
243: dbsize.getFirstChild().setNodeValue(value);
244: save();
245: }
246: }
247: }
248: }
249: return true;
250: }
251:
252: public boolean addField(int pos, String name, String type,
253: String guitype, String state, String required,
254: String unique, String size) {
255: if (document == null)
256: openDocument();
257: Element fe = reader.getElementByPath(document
258: .getDocumentElement(), "builder.fieldlist");
259: if (fe != null) {
260: String newpart = " <field>\r";
261: newpart += " <editor>\r";
262: newpart += " <positions>\r";
263: newpart += " <input>" + pos + "</input>\r";
264: newpart += " <list>" + pos + "</list>\r";
265: newpart += " <search>" + pos + "</search>\r";
266: newpart += " </positions>\r";
267: newpart += " </editor>\r";
268:
269: newpart += " <datatype base=\""
270: + guitype
271: + "\" xmlns=\"http://www.mmbase.org/xmlns/datatypes\"/>\r";
272: newpart += " <db>\r";
273: newpart += " <name>" + name + "</name>\r";
274: newpart += " <type key=\"" + unique
275: + "\" notnull=\"" + required + "\" size=\"" + size
276: + "\" state=\"" + state + "\">" + type
277: + "</type>\r";
278: newpart += " </db>\r";
279: newpart += " </field>\r";
280: mergePart(fe, newpart);
281: }
282: save();
283: return true;
284: }
285:
286: private void mergePart(Element fe, String newpart) {
287: try {
288: Element nf = (DocumentReader.getDocumentBuilder(false,
289: null, null).parse(new InputSource(new StringReader(
290: newpart)))).getDocumentElement();
291: fe.appendChild(document.importNode(nf, true));
292: } catch (Exception e) {
293: log.error("Can't merge new xml code");
294: }
295: }
296:
297: public boolean save() {
298: // save the file back using the ResourceLoader
299: try {
300: ResourceLoader.getConfigurationRoot().storeDocument(path,
301: document);
302: } catch (Exception e) {
303: e.printStackTrace();
304: return false;
305: }
306: return true;
307: }
308:
309: private void openDocument() {
310: try {
311: document = ResourceLoader.getConfigurationRoot()
312: .getDocument(path);
313: reader = new BuilderReader(document, MMBase.getMMBase());
314: } catch (Exception e) {
315: log.error("missing builderfile file : " + path);
316: e.printStackTrace();
317: }
318: }
319:
320: }
|