SessionHistoryManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  Creation P.Delrieu
00026  * 
00027  */
00028 package org.openmobileis.modules.common.history;
00029 
00030 import org.openmobileis.common.context.SessionContext;
00031 import org.openmobileis.common.context.SessionContextManager;
00032 import org.openmobileis.common.util.collection.Array;
00033 
00034 
00043 public class SessionHistoryManager implements HistoryManager {
00044   
00045   private String histoName;
00049   public SessionHistoryManager(String name) {
00050     super();
00051                 histoName = name;
00052   }
00053         public void addUID (String uid) {
00054                 String[] uids = this.getUidList();
00055                 int pos = this.getPosition(uid, uids);
00056                 if (pos != -1) { // does exists
00057                         this.remove(pos, uids);
00058                 }
00059          // Add the new one at the end of the array
00060                 this.addLast(uid, uids);
00061          // save the list
00062 //              this.saveProperties(uids);
00063         }
00064         
00065         private String[] getUidList()   {
00066                 SessionContext context = SessionContextManager.getManager().getSessionContext();
00067                 String[] uidList = (String[])context.getAttribute("%openmishisto%"+getHistoryName());
00068                 if (uidList == null)    {
00069                         uidList = new String[6];
00070                         context.setAttribute("%openmishisto%"+getHistoryName(), uidList);
00071                 }
00072                 return uidList;
00073         }
00074 
00075         public void removeUID (String uid) {
00076                 String[] uids = this.getUidList();
00077                 int pos = this.getPosition(uid, uids);
00078                 if (pos != -1) { // does not exists
00079                                 this.remove(pos, uids);
00080                         }
00081         //      this.saveProperties(uids);
00082         }
00083 
00084         public Array getAll() {
00085                 String[] uidsTemps = this.getUidList();
00086                 Array results = new Array(5);
00087                 for (int i=uidsTemps.length -1; i >= 0; i--) {
00088                         if (uidsTemps[i] != null) {
00089                                 results.add(uidsTemps[i]);
00090                         }
00091                 }
00092                 return results;
00093         }
00094         
00095         public String getHistoryName()  {
00096                 return histoName;
00097         }
00098 
00099         // Private Methods
00100          // return the position of the uid or -1 if it does not exist
00101          private int getPosition(String id, String uids[]) {
00102                 for (int i=0; i < uids.length; i++) {
00103                  if ((uids[i] != null) && (uids[i].equals(id))) {
00104                          return i;
00105                  }
00106                 }
00107                 return -1;
00108          }
00109 
00110          // add the element at the end, remove the first one and compact the array if the array is full
00111          private void addLast(String uid, String uids[]) {
00112                  for (int i=0; i < uids.length; i++) {
00113                          if (uids[i] == null) {
00114                                  uids[i] = uid; // not yet completed
00115                                  return;
00116                          }
00117                  } // end for
00118                 // otherwise, the array is full, remove the first one, compact and add the new one at the end
00119                  this.remove(0,uids);
00120                  uids[uids.length -1] = uid;
00121          }
00122 
00123          private void remove (int pos, String uids[]) {
00124                  for (int i=pos; i < uids.length-1; i++) {
00125                          uids[i] = uids[i+1];
00126                  }
00127                         uids[uids.length-1] = null;
00128          }
00129 
00130 }

Generated on Mon Dec 4 11:03:30 2006 for OpenMobileIS by  doxygen 1.5.1-p1