finished basic account add
[Bookkeeping.git] / src / com / interrupt / bookkeeping / util / Util.java
blob3393f38d8ad3c423393c1d676e5c5d23d913247c
2 package com.interrupt.bookkeeping.util;
4 import org.apache.log4j.Logger;
6 import com.interrupt.bob.base.IBob;
7 import com.interrupt.bob.base.IVisitor;
8 import com.interrupt.bookkeeping.cc.bkell.Bkell;
9 import com.interrupt.bookkeeping.exception.BkellException;
10 import com.interrupt.logs.GLog;
11 import com.interrupt.logs.GLogMessage;
12 import com.interrupt.logs.GLogMessages;
13 import com.interrupt.logs.GLogs;
14 import com.interrupt.logs.ILog;
15 import com.interrupt.logs.ILogMessage;
16 import com.interrupt.logs.ILogMessages;
17 import com.interrupt.logs.ILogs;
18 import com.interrupt.util.IdGenerator;
20 public class Util {
23 public static String OPEN_STATE = "open";
24 public static String CLOSED_STATE = "closed";
25 public static String REVERSED_STATE = "reversed";
26 public static String REVERSE_CLOSED_STATE = "reverse_closed";
27 public static String REMOVED_STATE = "removed";
29 private static Logger logger = Logger.getLogger(Util.class);
32 /** filter XML to get rid of spaces
34 public static String filterSpacesFromXML(String cinput_s) {
36 String input_1 = cinput_s.replaceAll("< ", "<");
38 String input_11 = input_1.replaceAll(" : ", ":");
39 String input_2 = input_11.replaceAll("\\/ >", " />");
40 String input_x = input_2.replaceAll("< \\/", "</");
41 String input_y = input_x.replaceAll("<\\/ ", "</");
42 String input_xx = input_y.replaceAll(" \\/ ", "/");
43 String input_3 = input_xx.replaceAll(" = ", "=");
45 //String input_4 = input_3.replaceAll("\" ", "\"");
46 //String input_5 = input_4.replaceAll(" \"", "\"");
47 //String input_6 = input_5.replaceAll("\' ", "\'");
48 //String input_7 = input_6.replaceAll(" \'", "\'");
50 return input_3;
53 public static IBob populateEmptyIDs(String inputXML) {
55 IBob wrapper = com.interrupt.bob.util.Util.loadBobFromXML(inputXML);
56 return Util.populateEmptyIDs(wrapper);
58 public static IBob populateEmptyIDs(IBob input) {
60 /**
61 * make sure each bob has an ID
63 class IdPopulator implements IVisitor {
64 private Logger logger = null;
65 public IdPopulator() {
66 logger = Logger.getLogger(com.interrupt.bookkeeping.util.Util.class);
67 logger.debug("IdPopulator > Populating IDs");
69 public void visit(IBob bob) {
70 if(bob.getAttributeValue("id") == null || bob.getAttributeValue("id").trim().length() < 1) {
71 bob.setProperty(IBob.XML_DEPTH, "1"); //** just print out the parent document
72 logger.debug("found empty id for["+ bob.getTagName() +"]");
73 bob.setAttributeValue("id", IdGenerator.generateId());
74 logger.debug("tag AFTER ID population["+ bob.toXML(false) +"]");
78 input.acceptFirst(new IdPopulator());
80 return input;
83 public static Throwable getRootCause(Throwable e) {
85 Throwable cause = e.getCause();
86 if( cause != null) {
88 logger.error("next cause message["+ cause.getMessage() +"]");
89 return cause;
91 return e;
93 public static BkellException generateBkellException(Throwable e) {
96 logger.error("generateBkellException CALLED");
98 Throwable roote = Util.getRootCause(e);
99 String topMessage_S = e.getMessage();
100 String rootMessage_S = roote.getMessage();
102 logger.error("TOP error message["+ topMessage_S +"]");
103 logger.error("ROOT error message["+ rootMessage_S +"]");
105 ILogMessages messages = new GLogMessages();
106 if( topMessage_S != null ) {
107 ILogMessage tmessage = new GLogMessage();
108 tmessage.setContent(topMessage_S);
110 messages.addLogMessage(tmessage);
112 ILogMessage rmessage = null;
113 if(rootMessage_S != null) {
115 if(topMessage_S != null) {
117 if(topMessage_S != rootMessage_S) {
119 rmessage = new GLogMessage();
120 rmessage.setContent(rootMessage_S);
121 messages.addLogMessage(rmessage);
124 else if(rootMessage_S != null) {
126 rmessage = new GLogMessage();
127 rmessage.setContent(rootMessage_S);
128 messages.addLogMessage(rmessage);
132 ILog log = new GLog();
133 log.setLevel("ERROR");
134 log.addLogMessages(messages);
136 ILogs logs = new GLogs();
137 logs.addLog(log);
139 BkellException bke = new BkellException(e.getMessage(),e);
140 bke.setLogMessages(logs);
142 return bke;
145 public static void main(String args[]) {