finished basic account add
[Bookkeeping.git] / src / com / interrupt / bookkeeping / System.java
blobbbfc7db6a67791f097681e94f7ecaee1fe0088ed
1 package com.interrupt.bookkeeping;
3 import java.util.List;
5 import org.apache.log4j.Logger;
7 import com.interrupt.bob.base.Bob;
8 import com.interrupt.bob.base.BobException;
9 import com.interrupt.bob.base.IBob;
10 import com.interrupt.bob.base.IVisitor;
11 import com.interrupt.bookkeeping.cc.bkell.aauth.Aauthentication;
12 import com.interrupt.bookkeeping.cc.bkell.aauth.AauthenticationMediator;
13 import com.interrupt.bookkeeping.cc.bkell.aauth.IAauthentication;
14 import com.interrupt.bookkeeping.cc.bkell.command.IResult;
15 import com.interrupt.bookkeeping.exception.AuthorisationException;
16 import com.interrupt.bookkeeping.system.BookkeepingSystemProperties;
17 import com.interrupt.bookkeeping.users.Group;
18 import com.interrupt.bookkeeping.users.IGroup;
19 import com.interrupt.bookkeeping.users.IGroups;
20 import com.interrupt.bookkeeping.users.Groups;
21 import com.interrupt.bookkeeping.users.IUser;
22 import com.interrupt.bookkeeping.users.IUserSession;
23 import com.interrupt.bookkeeping.users.User;
24 import com.interrupt.spittoon.Spittoon;
27 public class System extends com.interrupt.bookkeeping.GSystem {
30 private Logger logger = Logger.getLogger(com.interrupt.bookkeeping.System.class);
31 private AauthenticationMediator aauthenticationMediator = null;
32 private Spittoon spittoon = null;
34 public System() {}
36 public Spittoon getSpittoon() {
37 return spittoon;
39 public void setSpittoon(Spittoon spittoon) {
40 this.spittoon = spittoon;
44 public void initialise() {
46 if(spittoon == null) {
48 spittoon = new Spittoon();
49 spittoon.initialise();
51 //1. initiliase and set your aauthentication
52 //Aauthentication.instance().initialise();
53 //this.setAauthentication(Aauthentication.instance());
54 String dburl = spittoon.getAauthDbUrl();
55 String authXpath = "/system[ @id='main.system' ]/aauthentication[ @id='main.authentication' ]";
56 Aauthentication aauthentication = (Aauthentication)spittoon.retrieve(dburl, authXpath, true);
57 aauthentication.setSpittoon(spittoon);
58 this.setAauthentication(aauthentication);
59 logger.debug("System.initialise:: Adding aauthentication["+aauthentication.getId()+"] to System");
62 //1.5 setup AauthenticationMediator
63 aauthenticationMediator = new AauthenticationMediator();
64 this.setAauthenticationMediator(aauthenticationMediator);
66 //BookkeepingSystemProperties bsproperties = BookkeepingSystemProperties.instance();
68 //2. initialise and set your main groups
69 String gdburl = spittoon.getGroupsDbUrl();
70 String groupsXpath = "/system[ @id='main.system' ]/groups[ @id='main.groups' ]";
71 Groups mgroups = (Groups)spittoon.retrieve(gdburl, groupsXpath, false);
73 this.setGroups(mgroups);
76 //3. initialise the group attic
77 /*groupAttic = (Groups)Spittoon.instance().getDocument(
78 bsproperties.getProperty("db.url") + "system/groups/",
79 "groups",
80 "group.attic");
82 IResult groupAtticList = null; //Spittoon.instance().getCollectionBobs(bsproperties.getProperty("db.url") + "system/groups.attic/");
83 Groups groupAttic = new Groups();
84 groupAttic.setId("group.attic");
85 groupAttic.setChildren(groupAtticList.allChildren());
86 this.setGroupAttic(groupAttic);
91 public AauthenticationMediator getAauthenticationMediator() {
92 return aauthenticationMediator;
94 public void setAauthenticationMediator(AauthenticationMediator aauthenticationMediator) {
96 this.aauthenticationMediator = aauthenticationMediator;
97 this.aauthenticationMediator.setAauth(this.getAauthentication());
98 this.aauthenticationMediator.setSystem(this);
99 this.getAauthentication().setAauthenticationMediator(aauthenticationMediator);
102 public String authenticate(String groupid, IUser user) {
104 IUserSession usession = this.getAauthentication().authenticate(groupid, user);
105 return usession.getId();
107 public boolean authenticated(IUser user) {
109 return this.getAauthentication().authenticated(user);
113 // find 'User'
114 public IUser findUser(String sessionid) {
116 IUserSession usession = (IUserSession)this.getAauthentication().getUsers().find("userSession", sessionid);
117 if(usession == null) {
118 throw new AuthorisationException("There is no session associated with id["+sessionid+"]");
120 logger.debug("System.findUser:: usession: "+ usession.toXML());
122 IUser userResult = (IUser)usession.getParent();
123 logger.debug("System.findUser:: userResult: "+ userResult.toXML());
125 return userResult;
127 public IUser findUser(IUser user) {
128 return this.getAauthentication().findUser(user.getId());
132 // find 'Group'
133 public IGroup findGroup(String sessionid) {
135 IUserSession usession = (IUserSession)this.getAauthentication().getUsers().find("userSession", sessionid);
136 if(usession == null) {
137 throw new AuthorisationException("There is no session associated with id["+sessionid+"]");
139 logger.debug("System.findGroup:: usession: "+ usession.toXML());
141 String gid = usession.getGroupid();
142 IGroup groupResult = this.getAauthentication().getGroups().findGroupById(gid);
143 logger.debug("System.findGroup:: groupResult: "+ groupResult.toXML());
145 return groupResult;
147 public IGroup findGroup(IGroup group) {
148 return this.getAauthentication().findGroup(group.getId());
152 // find 'Bookkeeping'
153 public IBookkeeping findBookkeeping(String sessionid) {
155 IUserSession usession = (IUserSession)this.getAauthentication().getUsers().find("userSession", sessionid);
156 if(usession == null) {
157 throw new AuthorisationException("There is no session associated with id["+sessionid+"]");
159 String groupid = usession.getGroupid();
160 return this._findBookkeeping(groupid);
162 public IBookkeeping findBookkeeping(IGroup group) {
163 return this._findBookkeeping(group.getId());
165 private IBookkeeping _findBookkeeping(String groupid) {
167 return this.getGroups().findGroupById(groupid).findBookkeepingById("main.bookkeeping");
171 // Aauthentication
172 public Aauthentication getAauthentication() {
173 return (Aauthentication)this.findAauthenticationById("main.authentication");
175 public void setAauthentication(Aauthentication aauth) {
177 aauth.setId("main.authentication");
178 this.removeAllAauthentication();
179 super.addAauthentication(aauth);
181 public void addAauthentication(com.interrupt.bookkeeping.cc.bkell.aauth.Aauthentication aauth) {
183 this.removeAllAauthentication();
184 super.addAauthentication(aauth);
188 // Groups
189 public Groups getGroups() {
190 return (Groups)this.findGroupsById("main.groups");
192 public void setGroups(Groups groups) {
194 groups.setId("main.groups");
195 this.removeGroupsById(groups.getId());
196 super.addGroups(groups);
198 public void addGroups(Groups groups) {
200 if(spittoon == null)
201 this.initialise();
202 this.removeGroupsById(groups.getId());
203 super.addGroups(groups);
207 // Group Attic
208 public Groups getGroupAttic() {
209 return (Groups)this.findGroupsById("group.attic");
211 public void setGroupAttic(Groups groups) {
213 groups.setId("group.attic");
214 this.removeGroupsById(groups.getId());
215 super.addGroups(groups);
217 public void addGroupAttic(Groups groups) {
218 this.removeGroupsById(groups.getId());
219 super.addGroups(groups);
223 public void addGroup(Group group) {
224 this.getAauthentication().addGroup(group);
226 public void addUser(User user) {
228 if(spittoon == null)
229 this.initialise();
231 try {
232 if( this.aauthenticationMediator == null ) {
234 aauthenticationMediator = new AauthenticationMediator();
235 this.setAauthenticationMediator(aauthenticationMediator);
238 logger.debug("System.addUser: "+ user);
239 logger.debug("System.addUser USERS: "+
240 ((Aauthentication)this.findAauthenticationById("main.authentication")).getUsers() );
241 this.getAauthentication().addUser(user);
243 catch(RuntimeException t) {
245 //Throwable cause = this.getRootCause(t);
246 //cause.printStackTrace();
247 t.printStackTrace();
251 private Throwable getRootCause(Throwable t) {
253 Throwable cause = t.getCause();
254 if(cause != null) {
255 return this.getRootCause(t);
257 return t;
260 public void addGroupSetAssociatedBookkeeping(Group newGroup) {
262 logger.debug("System.addGroupSetAssociatedBookkeeping: "+ newGroup);
263 this.getAauthentication().addGroup(newGroup);
265 //** create a new 'bookkeeping' entity
266 Bookkeeping defaultBookkeeping =
267 (Bookkeeping)Bob.loadS(System.class.getResourceAsStream("/default.bookkeeping.xml"),
268 BookkeepingSystemProperties.instance().getProperty("bob.def"));
270 Group correspondingbk = newGroup;
271 correspondingbk.removeAllUser();
272 correspondingbk.addBookkeeping(defaultBookkeeping);
273 this.getGroups().addGroup(correspondingbk);
275 String gurl = spittoon.getGroupsDbUrl();
276 logger.debug("BEFORE group add and associated bookkeeping > gurl["+gurl+"]");
278 spittoon.createR(gurl,
279 "/system[ @id='main.system' ]/groups[ @id='main.groups' ]/group[ @id='"+correspondingbk.getId()+"' ]",
280 correspondingbk.toXML(false));
282 logger.debug("AFTER group add and associated bookkeeping");
287 public void accept(IVisitor visitor) throws BobException {
289 /*if(visitor instanceof PersistableVisitor) {
291 // let Aauthentication save individually
292 PersistableVisitor aauthSaveVisitor = this.getAauthentication().getSavePersistableVisitor();
293 aauthSaveVisitor.setSpittoon(((PersistableVisitor)visitor).getSpittoon());
294 aauthSaveVisitor.setBaseDir(((PersistableVisitor)visitor).getBaseDir());
295 this.getAauthentication().accept(aauthSaveVisitor);
297 // let Groups save individually
298 PersistableVisitor groupsSaveVisitor = this.getGroups().getSavePersistableVisitor();
299 groupsSaveVisitor.setSpittoon(((PersistableVisitor)visitor).getSpittoon());
300 groupsSaveVisitor.setBaseDir(((PersistableVisitor)visitor).getBaseDir());
301 this.getGroups().accept(groupsSaveVisitor);
304 else {
306 // otherwise, roll the 'accept' as normal
307 super.accept(visitor);
313 class SavePersistor {
315 private String baseDir = null;
316 private String groupDir = "system/aauthentication/groups/";
317 private String usersDir = "system/aauthentication/users/";
318 private BookkeepingSystemProperties bsprops = null;
319 private Spittoon spittoon = null;
321 public SavePersistor() {
322 bsprops = BookkeepingSystemProperties.instance();
324 public void setSpittoon(Spittoon spit) {
325 spittoon = spit;
327 public Spittoon getSpittoon() {
328 return spittoon;
330 public void setBaseDir(String bdir) {
331 baseDir = bdir;
333 public String getBaseDir() {
334 return baseDir;
336 public void visit(IBob bob) {
338 //** just set off the aauthentication and groups visitors
339 if(bob instanceof Aauthentication) {
341 /*PersistableVisitor pv = ((Aauthentication)bob).getSavePersistableVisitor();
342 pv.setBaseDir(baseDir);
343 pv.setSpittoon(spittoon);
345 bob.accept(pv);
349 else if(bob instanceof Groups &&
350 (((Groups)bob).getId().equals("main.groups") ||
351 ((Groups)bob).getId().equals("group.attic"))
352 ) {
354 /*PersistableVisitor pv = ((Aauthentication)bob).getSavePersistableVisitor();
355 pv.setBaseDir(baseDir);
356 pv.setSpittoon(spittoon);
358 bob.accept(pv);
369 * ** DELETE: the code below is deprecated and should be deleted after the
370 * rest of the system is working
372 // Bkell
373 public com.interrupt.bookkeeping.cc.bkell.IBkell getBkell() {
374 List allBkell = this.allBkell();
375 if(allBkell.size() > 0) {
376 return (com.interrupt.bookkeeping.cc.bkell.IBkell)allBkell.get(0);
378 return null;
380 public void setBkell(com.interrupt.bookkeeping.cc.bkell.IBkell bkell) {
381 this.addBkell(bkell);
383 public void addBkell(com.interrupt.bookkeeping.cc.bkell.Bkell bkell) {
384 this.removeAllBkell();
385 super.addBkell(bkell);
387 public com.interrupt.bookkeeping.cc.bkell.IBkell findBkellById(String value) {
388 return super.findBkellById(value);
391 // Users
392 public com.interrupt.bookkeeping.users.IUsers getUsers() {
393 List allUsers = this.allUsers();
394 if(allUsers.size() > 0) {
395 return (com.interrupt.bookkeeping.users.IUsers)allUsers.get(0);
397 return null;
399 public void setUsers(com.interrupt.bookkeeping.users.IUsers users) {
400 this.addUsers(users);
402 public void addUsers(com.interrupt.bookkeeping.users.IUsers users) {
403 this.removeAllUsers();
404 super.addUsers(users);
408 // Bookkeeping
409 public com.interrupt.bookkeeping.IBookkeeping getBookkeeping() {
410 List allBookkeeping = this.allBookkeeping();
411 if(allBookkeeping.size() > 0) {
412 return (com.interrupt.bookkeeping.IBookkeeping)allBookkeeping.get(0);
414 return null;
416 public void setBookkeeping(com.interrupt.bookkeeping.IBookkeeping bookkeeping) {
417 this.addBookkeeping(bookkeeping);
419 public void addBookkeeping(com.interrupt.bookkeeping.IBookkeeping bookkeeping) {
420 this.removeAllBookkeeping();
421 super.addBookkeeping(bookkeeping);