-- adapted webapp to connect to remote eXist instances
[Bookkeeping.git] / src / com / interrupt / bookkeeping / cc / bkell / command / AbstractCommand.java
blobf3b7893cac3195839e5296bb72b9223b25b55e50
2 package com.interrupt.bookkeeping.cc.bkell.command;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Iterator;
7 import java.util.Map;
8 import java.util.HashMap;
9 import java.util.Set;
11 import org.apache.log4j.Logger;
12 import org.xml.sax.Attributes;
14 import com.interrupt.bob.base.Bob;
15 import com.interrupt.bob.base.BobSystem;
17 import com.interrupt.bob.base.IBob;
18 import com.interrupt.bookkeeping.GSystem;
19 import com.interrupt.bookkeeping.ISystem;
20 import com.interrupt.bookkeeping.cc.bkell.IBkell;
21 import com.interrupt.bookkeeping.cc.bkell.aauth.Aauthentication;
22 import com.interrupt.bookkeeping.exception.AuthorisationException;
23 import com.interrupt.bookkeeping.exception.CommandException;
24 import com.interrupt.bookkeeping.users.IUser;
25 import com.interrupt.spittoon.Spittoon;
27 public abstract class AbstractCommand extends GCommand {
30 protected Logger logger = Logger.getLogger(AbstractCommand.class);
31 private Map tokenMap = null;
32 private IToken _token = null;
33 private IBob _tokenReciever = null;
34 private IOptions _options = null;
35 private Aauthentication aauthentication = null;
37 protected Spittoon spittoon = null;
38 protected String contextXPath = null;
41 public AbstractCommand() {
43 this(null, null);
45 public AbstractCommand(Spittoon spitt) {
47 this(null, spitt);
49 public AbstractCommand(String commandName, Spittoon spitt) {
51 spittoon = spitt;
52 if(commandName != null) {
54 this.setName(commandName);
56 ISystem bsystem = (GSystem)Bob.loadS( CreateCommand.class.getResourceAsStream("/bookkeeping.system.xml"), "xml/bookkeeping.system.xml" );
57 IBkell bkell = bsystem.findBkellById("bkell.main");
58 ITokens tokens = bkell.findTokensById("tokens.def");
59 ICommands commands = bkell.findCommandsById("commands.def");
61 ICommand command = commands.findCommandByName(commandName);
62 ITokens possibleTokens = (ITokens)command.allTokens().get(0);
64 // set the list of possible tokens
65 this.setTokens(possibleTokens);
68 this.setTokenLiterals(new GTokenLiterals());
69 tokenMap = new HashMap();
70 tokenMap.put("debit", "com.interrupt.bookkeeping.account.GDebit");
71 tokenMap.put("credit", "com.interrupt.bookkeeping.account.GCredit");
72 tokenMap.put("entry", "com.interrupt.bookkeeping.journal.Entry");
73 tokenMap.put("entries", "com.interrupt.bookkeeping.journal.Entries");
74 tokenMap.put("journal", "com.interrupt.bookkeeping.journal.GJournal");
75 tokenMap.put("account", "com.interrupt.bookkeeping.account.Account");
76 tokenMap.put("transaction", "com.interrupt.bookkeeping.journal.Transaction");
78 tokenMap.put("system", "com.interrupt.bookkeeping.System");
79 tokenMap.put("journals", "com.interrupt.bookkeeping.journal.Journals");
80 tokenMap.put("accounts", "com.interrupt.bookkeeping.account.Accounts");
81 tokenMap.put("user", "com.interrupt.bookkeeping.users.User");
82 tokenMap.put("users", "com.interrupt.bookkeeping.users.Users");
83 tokenMap.put("group", "com.interrupt.bookkeeping.users.Group");
84 tokenMap.put("groups", "com.interrupt.bookkeeping.users.Groups");
86 _options = new Options();
91 public Spittoon getSpittoon() {
92 return spittoon;
94 public void setSpittoon(Spittoon spittoon) {
95 this.spittoon = spittoon;
98 /**
99 * i) there should be 1 token executed for this command
100 * ii) there should be 1 accompanying list of options supplied to the command
103 // set tokens
104 public void setToken(IToken token) {
106 if( !this.validateCommandToken(token) ) {
107 throw new CommandException("Command token is not in the set of possible tokens["+ this.getTokens().toXML() +"]");
109 this.getTokens().removeAllToken();
110 this.getTokens().addToken(token);
112 _token = token;
115 public IToken getToken() {
117 logger.debug("this.getTokens(): " + this.getTokens());
118 logger.debug("_token: "+ _token);
119 return this.getTokens().findTokenByName(_token.getName());
122 // set options
123 public void addOption(IOption option) {
124 this._options.addOption(option);
126 public IOptions getOptions() {
127 return this._options;
129 public void setOptions(IOptions options) {
130 this._options = options;
133 public IOption getOption(String oname) {
135 return (IOption)this._options.findOptionByName(oname);
138 public String getContextXPath() {
140 logger.debug("AbstractCommand.GETContextXPath CALLED > contextXPath["+contextXPath+"]");
141 return contextXPath;
143 public void setContextXPath(String contextXPath) {
145 logger.debug("AbstractCommand.SETContextXPath CALLED > contextXPath["+contextXPath+"]");
146 this.contextXPath = contextXPath;
150 /* there should be 1 set of possible tokens for any given command
152 public ITokens getTokens() {
154 List allToken = this.allTokens();
155 if(allToken.isEmpty()) {
156 return null;
158 return (ITokens)this.allTokens().get(0);
161 public void setTokens(ITokens oset) {
163 this.addTokens(oset);
165 public void addTokens(ITokens oset) {
166 this.removeAllTokens();
167 super.addTokens(oset);
171 public Aauthentication getAauthentication() {
172 return aauthentication;
174 public void setAauthentication(Aauthentication aauthentication) {
175 this.aauthentication = aauthentication;
179 /* EXECUTE this command
181 public IResult execute(IUser user) {
183 // check that the user is authorized to perform this operation
185 boolean authorised = this.aauthentication.authorised(user, this);
186 if(!authorised) {
187 throw new AuthorisationException("User is either not Authenticated or Authorised to perform this action");
190 return null;
194 /**
195 * a command can only be executed on a given token or set of tokens
198 public boolean validateCommandToken(IToken token) {
200 boolean valid = false;
201 String tokenName = token.getName();
203 List possibleTokens = this.getTokens().allToken();
204 Iterator oiter = possibleTokens.iterator();
205 Token eacho = null;
206 while( oiter.hasNext() ) {
208 eacho = (Token)oiter.next();
209 String each_n = eacho.getName();
211 if(tokenName.equals(each_n)) {
212 valid = true;
213 break;
217 return valid;
221 public List isolateSearchList(Attributes attributes, List possibleResults) {
223 List isolatedList = possibleResults;
224 Map isolatedMap = new HashMap();
227 //** putting a match count for each attribute
228 Iterator iter = possibleResults.iterator();
229 IBob eachBob = null;
230 while(iter.hasNext()) {
232 eachBob = (IBob)iter.next();
233 //logger.debug("");
234 //logger.debug("");
235 //logger.debug("NEW Iteration");
237 // 2. isolate on attributes
238 int matchCount = 0;
239 for( int i=0; i < attributes.getLength(); i++ ) { // COMPARE Attributes
241 //logger.debug("");
242 String compareValue = attributes.getValue(i);
243 if((compareValue != null) && (compareValue.trim().length() > 0)) {
245 Attributes eachAttributes = eachBob.getAttributes();
246 for (int j = 0; j < eachAttributes.getLength(); j++) { // EACH Attributes
248 String bobValue = eachAttributes.getValue(j);
249 //logger.debug("compareValue["+compareValue+"] / bobValue["+bobValue+"]");
250 if(compareValue.equals(bobValue)) {
252 // add even if match only only 1 attribute
253 matchCount += 1;
254 //logger.debug("MATCH!!! count["+matchCount+"]");
259 if(matchCount > 0) {
260 isolatedMap.put(eachBob, new Integer(matchCount));
270 //return new ArrayList(isolatedMap.keySet());
272 int latestMatch = 0;
273 int highestMatch = 0;
275 Set kset = isolatedMap.keySet();
276 Iterator iter2 = kset.iterator();
277 IBob eachBob2 = null;
278 Integer eachInteger = null;
279 while(iter2.hasNext()) {
281 eachBob2 = (IBob)iter2.next();
282 eachInteger = (Integer)isolatedMap.get(eachBob2);
283 latestMatch = eachInteger.intValue();
285 //logger.debug("AbstractCommand.isolateSearchList / LatestMatch["+latestMatch+"] / HighestMatch["+highestMatch+"]");
287 if(latestMatch == highestMatch) {
288 isolatedList.add(eachBob2);
290 else if(latestMatch > highestMatch) {
292 highestMatch = latestMatch;
293 isolatedList = new ArrayList();
294 isolatedList.add(eachBob2);
300 return isolatedList;
305 public String tokenToFQClassName(String token) {
308 if(token == null || token.trim().length() == 0) {
310 throw new CommandException( "token name is NULL" );
313 String value = null;
314 Set keys = tokenMap.keySet();
315 if(!keys.contains(token)) {
317 throw new CommandException( "token["+token+"] does not exist" );
320 Iterator kiter = keys.iterator();
321 while(kiter.hasNext()) {
323 String each = (String)kiter.next();
324 if(token.trim().equals(each)) {
326 value = (String)tokenMap.get(each);
327 break;
332 return value;
336 /* there should be 1 set of possible token.literals for any given command
338 public ITokenLiterals getTokenLiterals() {
340 List allTokenLiterals = this.allTokenLiterals();
341 if(allTokenLiterals.isEmpty()) {
342 return null;
344 return (ITokenLiterals)this.allTokenLiterals().get(0);
346 public void setTokenLiterals(ITokenLiterals oset) {
347 this.addTokenLiterals(oset);
349 public void addTokenLiterals(ITokenLiterals oset) {
350 this.removeAllTokenLiterals();
351 super.addTokenLiterals(oset);
355 /* I want to be able to add token.literals from the command itself
357 public void addTokenLiteral(IBob addition) {
358 this.getTokenLiterals().addChild( addition );
362 public void removeAllTokenLiteral() {
364 ITokenLiterals tokenLiterals = this.getTokenLiterals();
365 if(tokenLiterals == null) {
366 return;
369 tokenLiterals.removeAllChildren();
373 /* Add / Remove / Reverse commands have a reciever on which a command applies
374 * token.literals
376 public IBob getTokenReciever() {
378 return this._tokenReciever;
381 public void setTokenReciever(IBob treciever) {
383 this.removeChild(this._tokenReciever);
385 this.addChild(treciever);
386 this._tokenReciever = treciever;