001     
002    /*****************************************************************************
003    
004        jBudget.
005        Copyright (C) 2006  Dmitri Petrov
006    
007        This program is free software: you can redistribute it and/or modify
008        it under the terms of the GNU General Public License as published by
009        the Free Software Foundation, either version 3 of the License, or
010        (at your option) any later version.
011    
012        This program is distributed in the hope that it will be useful,
013        but WITHOUT ANY WARRANTY; without even the implied warranty of
014        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015        GNU General Public License for more details.
016    
017        You should have received a copy of the GNU General Public License
018        along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     
020    /*****************************************************************************
021    
022    /*
023     * StorageInterface.java
024     *
025     * Created on May 16, 2006, 11:35 AM
026     */
027    
028    package org.jbudget.io;
029    
030    import java.io.IOException;
031    import org.jdom.JDOMException;
032    import org.jbudget.Core.Account;
033    import org.jbudget.Core.Budget;
034    import org.jbudget.Core.Month;
035    import org.jbudget.Core.SharedPreferences;
036    import org.jbudget.Core.Transaction;
037    import org.jbudget.Core.User;
038    import org.jbudget.gui.user.UserPreferences;
039    
040    /**
041     * An interface that describes common save/read operations.
042     *
043     * @author petrov
044     */
045    public interface StorageInterface
046    {
047      /** Returns a {@link DataIndex DataIndex} object that describes the stored
048       *  data. The DataIndex object returned by this function is in indexing only
049       *  mode. It cannot be used to get any Month, Budget or other objects. Use 
050       *  corresponding functions to achieve that.
051       */ 
052      public DataIndex getDataIndex();
053      
054      /** Returns true if a new data index file was created when the Storage object
055       * was constructed. This usually means that a new data directory was 
056       * initialized */
057      public boolean isNewDataInidex();
058      
059      //=============================== MONTHS ===================================//
060      
061      /** Returns a stored Month object. 
062       *  @throws an IllegalArguementException if there is no such month
063       *  @throws an IOException if there is a problem reading the Month object.
064       */
065      public Month getMonth(int montn, int year) throws IOException;
066      
067      /** Stores a Month object. 
068       * @return true when the Month was successfully saved. For networked
069       *          storage that means after a confirmation from server was 
070       *          received.
071       */
072      public boolean storeMonth(Month month);
073      
074      /** removes a Month object
075       * @return true when the operation has completed successfully.
076       * @throws IllegalArgumentException if there is no such Month object
077       */
078      public boolean removeMonth(Month month);
079      
080      /** removes a Month object
081       * @return true when the operation has completed successfully.
082       * @throws IllegalArgumentException if there is no such Month object
083       */
084      public boolean removeMonth(int cMonth, int cYear);
085      
086      
087    //=============================== BUDGETS ===================================//
088      
089      
090      /** Returns a budget object associated with the given budget id.
091       *  @throws IllegalArgumentException if no budget with such an id exists.
092       */
093      public Budget getBudget(int id) throws IOException;
094      
095      /** Stores a given budget.
096       * @return true when the operation has completed successfully.
097       */
098      public boolean storeBudget(Budget budget);
099      
100      /** removes a budget.
101       * @return true when the operation has completed successfully.
102       *  @throws IllegalArgumentException if no such budget exists.
103       */
104      public boolean removeBudget(Budget budget);
105      
106      /** removes a budget.
107       * @return true when the operation has completed successfully.
108       * @throws IllegalArgumentException if no such budget exists.
109       */
110      public boolean removeBudget(int id);
111      
112      
113    //============================== ACCOUNTS ===================================//
114      
115      
116      /** Returns an account object associated with a given account id.
117       *  @throws IllegalArgumentException if no account with such an id exists.
118       */
119      public Account getAccount(int id) throws IOException;
120      
121      /** Stores a given account.
122       * @return true when the operation has completed successfully.
123       */
124      public boolean storeAccount(Account account);
125      
126      /** removes an account.
127       * @return true when the operation has completed successfully.
128       * @throws IllegalArgumentException if no such account exists.
129       */
130      public boolean removeAccount(Account account);
131      
132      /** removes an account.
133       * @return true when the operation has completed successfully.
134       * @throws IllegalArgumentException if no such account exists.
135       */
136      public boolean removeAccount(int id);
137      
138      
139    //================================ USERS ====================================//
140      
141      
142      /** Returns a user object associated with a given user id.
143       *  @throws IllegalArgumentException if no user with such an id exists.
144       */
145      public User getUser(int uid) throws IOException;
146      
147      /** Stores a given user.
148       * @return true when the operation has completed successfully.
149       */
150      public boolean storeUser(User user);
151      
152      /** removes an user.
153       * @return true when the operation has completed successfully.
154       * @throws IllegalArgumentException if no such user exists.
155       */
156      public boolean removeUser(User user);
157      
158      /** removes an user.
159       * @return true when the operation has completed successfully.
160       * @throws IllegalArgumentException if no such user exists.
161       */
162      public boolean removeUser(int uid);
163      
164      /** Returns a UserPreference object associated with a given uid */
165      public UserPreferences getUserPreferences(int uid) throws IOException ;
166      
167      /** Saves a UserPreferences object to permanent storage. */
168      public boolean storeUserPreferences(int uid, UserPreferences preference);
169      
170      
171    //============================= TRANSACTIONS ================================//
172      
173      /** saves a Transaction.
174       * @return true when the operation has completed successfully.
175       * @throws IllegalArgumentException if Month in which the transaction
176       *         has happened does not exist.
177       */
178      public boolean addTransaction(
179          Transaction transaction, int cMonth, int cYear) throws IOException,
180                                                                 JDOMException;
181      
182      /** removes a Transaction
183       * @return true when the operation has completed successfully.
184       * @throws IllegalArgumentException if Month in which the transaction
185       *         has happened does not exist or there is no record of this
186       *         transaction
187       */
188      public boolean removeTransaction(
189          long transactionId, int cMonth, int cYear) throws IOException,
190                                                            JDOMException;
191      
192    //============================= ALLOCATIONS ================================//
193      
194      /** saves an Allocation.
195       * @return true when the operation has completed successfully.
196       * @throws IllegalArgumentException if Month in which the allocation
197       *         has happened does not exist.
198       */
199      public boolean addAllocation(
200          Transaction alloation, int cMonth, int cYear) throws IOException,
201                                                               JDOMException;
202      
203      /** removes an Allocation
204       * @return true when the operation has completed successfully.
205       * @throws IllegalArgumentException if Month in which the allocation
206       *         has happened does not exist or there is no record of this
207       *         allocation
208       */
209      public boolean removeAllocation(
210          long allocationId, int cMonth, int cYear) throws IOException,
211                                                           JDOMException;
212      
213     //============================= PREFERENCES ================================//
214      
215      /** Returns the SharedPreference object. */
216      public SharedPreferences getSharedPreferences();
217      
218      //=========================================================================//
219      
220      /** Writes all cached objects to permanent storage. It is imperative to 
221       *  call this fununction before the program is terminated. If this is not 
222       * done, all cached values will be lost.
223       */
224      public void commit() throws IOException, JDOMException;
225      
226      /** Returns string ID of this instance of the storage interface. */
227      public String getIdString();
228    
229      //========================== Generating IDs=============================//
230      
231      /** Creates a new numeric budget id and increases the counter. */
232      public int generateBudgetID();
233      
234      /** Creates a new numeric account id and increases the counter. */
235      public int generateAccountID();
236      
237      /** Creates a new numeric id for an expense category 
238       * and increases the counter. */
239      public int generateExpenseCategoryID();
240      
241      /** Creates a new numeric id for an income source 
242       * and increases the counter. */
243      public int generateIncomeSourceID();
244      
245      /** Creates a new uid for a new user 
246       * and increases the counter. */
247      public int generateUID();
248      
249      /** Creates a new numeric transaction id for 
250       * and increases the counter. */
251      public long generateTransactionID();
252      
253    }