holy s**tgit status I lost my most recent changes in origin/master -> trying to put...
[Bookkeeping.git] / src / com / interrupt / bookkeeping / cc / bkell / command / AbstractCommand.java
blobb7298b0776d600d21d582097363034dc63d9806c
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.system.BookkeepingSystemProperties;
25 import com.interrupt.bookkeeping.users.IUser;
26 import com.interrupt.spittoon.Spittoon;
28 public abstract class AbstractCommand extends GCommand {
31 protected Logger logger = Logger.getLogger(AbstractCommand.class);
32 private Map tokenMap = null;
33 private IToken _token = null;
34 private IBob _tokenReciever = null;
35 private IOptions _options = null;
36 private Aauthentication aauthentication = null;
38 protected Spittoon spittoon = null;
39 protected String contextXPath = null;
40 protected String contextUrl = null;
42 public AbstractCommand() {
44 this(null, null);
46 public AbstractCommand(Spittoon spitt) {
48 this(null, spitt);
50 public AbstractCommand(String commandName, Spittoon spitt) {
52 spittoon = spitt;
53 if(commandName != null) {
55 this.setName(commandName);
57 ISystem bsystem = (GSystem)Bob.loadS( CreateCommand.class.getResourceAsStream("/bookkeeping.system.xml"), BookkeepingSystemProperties.instance().getProperty("bob.def") );
58 IBkell bkell = bsystem.findBkellById("bkell.main");
59 ITokens tokens = bkell.findTokensById("tokens.def");
60 ICommands commands = bkell.findCommandsById("commands.def");
62 ICommand command = commands.findCommandByName(commandName);
63 ITokens possibleTokens = (ITokens)command.allTokens().get(0);
65 // set the list of possible tokens
66 this.setTokens(possibleTokens);
69 this.setTokenLiterals(new GTokenLiterals());
70 tokenMap = new HashMap();
71 tokenMap.put("debit", "com.interrupt.bookkeeping.account.GDebit");
72 tokenMap.put("credit", "com.interrupt.bookkeeping.account.GCredit");
73 tokenMap.put("entry", "com.interrupt.bookkeeping.journal.Entry");
74 tokenMap.put("entries", "com.interrupt.bookkeeping.journal.Entries");
75 tokenMap.put("journal", "com.interrupt.bookkeeping.journal.GJournal");
76 tokenMap.put("account", "com.interrupt.bookkeeping.account.Account");
77 tokenMap.put("transaction", "com.interrupt.bookkeeping.journal.Transaction");
79 tokenMap.put("system", "com.interrupt.bookkeeping.System");
80 tokenMap.put("journals", "com.interrupt.bookkeeping.journal.Journals");
81 tokenMap.put("accounts", "com.interrupt.bookkeeping.account.Accounts");
82 tokenMap.put("user", "com.interrupt.bookkeeping.users.User");
83 tokenMap.put("users", "com.interrupt.bookkeeping.users.Users");
84 tokenMap.put("group", "com.interrupt.bookkeeping.users.Group");
85 tokenMap.put("groups", "com.interrupt.bookkeeping.users.Groups");
87 _options = new Options();
92 public Spittoon getSpittoon() { return spittoon; }
93 public void setSpittoon(Spittoon spittoon) { this.spittoon = spittoon; }
94 public String getContextUrl() { return contextUrl; }
95 public void setContextUrl(String contextUrl) { this.contextUrl = contextUrl; }
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;
178 /* EXECUTE this command
180 public IResult execute(IUser user) {
182 // check that the user is authorized to perform this operation
184 boolean authorised = this.aauthentication.authorised(user, this);
185 if(!authorised) {
186 throw new AuthorisationException("User is either not Authenticated or Authorised to perform this action");
189 return null;
193 /**
194 * a command can only be executed on a given token or set of tokens
197 public boolean validateCommandToken(IToken token) {
199 boolean valid = false;
200 String tokenName = token.getName();
202 List possibleTokens = this.getTokens().allToken();
203 Iterator oiter = possibleTokens.iterator();
204 Token eacho = null;
205 while( oiter.hasNext() ) {
207 eacho = (Token)oiter.next();
208 String each_n = eacho.getName();
210 if(tokenName.equals(each_n)) {
211 valid = true;
212 break;
216 return valid;
220 public List isolateSearchList(Attributes attributes, List possibleResults) {
222 List isolatedList = possibleResults;
223 Map isolatedMap = new HashMap();
226 //** putting a match count for each attribute
227 Iterator iter = possibleResults.iterator();
228 IBob eachBob = null;
229 while(iter.hasNext()) {
231 eachBob = (IBob)iter.next();
232 //logger.debug("");
233 //logger.debug("");
234 //logger.debug("NEW Iteration");
236 // 2. isolate on attributes
237 int matchCount = 0;
238 for( int i=0; i < attributes.getLength(); i++ ) { // COMPARE Attributes
240 //logger.debug("");
241 String compareValue = attributes.getValue(i);
242 if((compareValue != null) && (compareValue.trim().length() > 0)) {
244 Attributes eachAttributes = eachBob.getAttributes();
245 for (int j = 0; j < eachAttributes.getLength(); j++) { // EACH Attributes
247 String bobValue = eachAttributes.getValue(j);
248 //logger.debug("compareValue["+compareValue+"] / bobValue["+bobValue+"]");
249 if(compareValue.equals(bobValue)) {
251 // add even if match only only 1 attribute
252 matchCount += 1;
253 //logger.debug("MATCH!!! count["+matchCount+"]");
258 if(matchCount > 0) {
259 isolatedMap.put(eachBob, new Integer(matchCount));
269 //return new ArrayList(isolatedMap.keySet());
271 int latestMatch = 0;
272 int highestMatch = 0;
274 Set kset = isolatedMap.keySet();
275 Iterator iter2 = kset.iterator();
276 IBob eachBob2 = null;
277 Integer eachInteger = null;
278 while(iter2.hasNext()) {
280 eachBob2 = (IBob)iter2.next();
281 eachInteger = (Integer)isolatedMap.get(eachBob2);
282 latestMatch = eachInteger.intValue();
284 //logger.debug("AbstractCommand.isolateSearchList / LatestMatch["+latestMatch+"] / HighestMatch["+highestMatch+"]");
286 if(latestMatch == highestMatch) {
287 isolatedList.add(eachBob2);
289 else if(latestMatch > highestMatch) {
291 highestMatch = latestMatch;
292 isolatedList = new ArrayList();
293 isolatedList.add(eachBob2);
299 return isolatedList;
304 public String tokenToFQClassName(String token) {
307 if(token == null || token.trim().length() == 0) {
309 throw new CommandException( "token name is NULL" );
312 String value = null;
313 Set keys = tokenMap.keySet();
314 if(!keys.contains(token)) {
316 throw new CommandException( "token["+token+"] does not exist" );
319 Iterator kiter = keys.iterator();
320 while(kiter.hasNext()) {
322 String each = (String)kiter.next();
323 if(token.trim().equals(each)) {
325 value = (String)tokenMap.get(each);
326 break;
331 return value;
335 /* there should be 1 set of possible token.literals for any given command
337 public ITokenLiterals getTokenLiterals() {
339 List allTokenLiterals = this.allTokenLiterals();
340 if(allTokenLiterals.isEmpty()) {
341 return null;
343 return (ITokenLiterals)this.allTokenLiterals().get(0);
345 public void setTokenLiterals(ITokenLiterals oset) {
346 this.addTokenLiterals(oset);
348 public void addTokenLiterals(ITokenLiterals oset) {
349 this.removeAllTokenLiterals();
350 super.addTokenLiterals(oset);
354 /* I want to be able to add token.literals from the command itself
356 public void addTokenLiteral(IBob addition) {
357 this.getTokenLiterals().addChild( addition );
361 public void removeAllTokenLiteral() {
363 ITokenLiterals tokenLiterals = this.getTokenLiterals();
364 if(tokenLiterals == null) {
365 return;
368 tokenLiterals.removeAllChildren();
372 /* Add / Remove / Reverse commands have a reciever on which a command applies
373 * token.literals
375 public IBob getTokenReciever() {
377 return this._tokenReciever;
380 public void setTokenReciever(IBob treciever) {
382 this.removeChild(this._tokenReciever);
384 this.addChild(treciever);
385 this._tokenReciever = treciever;