View Javadoc

1   package com.leonarduk.clearcheckbook;
2   
3   import java.io.File;
4   import java.util.ArrayList;
5   import java.util.HashMap;
6   import java.util.Iterator;
7   import java.util.LinkedList;
8   import java.util.List;
9   import java.util.Map;
10  
11  import org.apache.log4j.Logger;
12  
13  import com.leonarduk.clearcheckbook.dto.AccountDataType;
14  import com.leonarduk.clearcheckbook.dto.CategoryDataType;
15  import com.leonarduk.clearcheckbook.dto.LimitDataType;
16  import com.leonarduk.clearcheckbook.dto.ReminderDataType;
17  import com.leonarduk.clearcheckbook.dto.TransactionDataType;
18  import com.leonarduk.clearcheckbook.file.ClearCheckBookFileHandler;
19  import com.leonarduk.clearcheckbook.file.FilePreProcessor;
20  
21  /***
22   * A class to hold helper methods around the API. This adds caching to the
23   * {@link ClearCheckBookConnection} class. It is expected client code will use
24   * this class as an interface to the API.
25   * 
26   * @author Stephen Leonard
27   * @since 6 Feb 2014
28   * 
29   * @version $Author:: $: Author of last commit
30   * @version $Rev:: $: Revision of last commit
31   * @version $Date:: $: Date of last commit
32   * 
33   */
34  public class ClearCheckBookHelper {
35  
36  	private static final Logger _logger = Logger
37  			.getLogger(ClearCheckBookHelper.class);
38  	private ClearCheckBookConnection connection;
39  	private ClearCheckBookFileHandler fileHandler;
40  
41  	private Map<Long, AccountDataType> accountsMap = null;
42  
43  	public ClearCheckBookHelper(String userName, String password) {
44  
45  		this.connection = new ClearCheckBookConnection(userName, password);
46  		this.fileHandler = new ClearCheckBookFileHandler();
47  	}
48  
49  	public File exportAccounts(String fileName, List<AccountDataType> accounts)
50  			throws ClearcheckbookException {
51  		return this.fileHandler.exportAccounts(fileName, accounts);
52  	}
53  
54  	public File exportCategories(String fileName,
55  			List<CategoryDataType> categories) throws ClearcheckbookException {
56  		return this.fileHandler.exportCategories(fileName, categories);
57  	}
58  
59  	public File exportLimits(String fileName, List<LimitDataType> limits)
60  			throws ClearcheckbookException {
61  		return this.fileHandler.exportLimits(fileName, limits);
62  	}
63  
64  	public File exportReminders(String fileName,
65  			List<ReminderDataType> reminders) throws ClearcheckbookException {
66  		return this.fileHandler.exportReminders(fileName, reminders);
67  	}
68  
69  	public File exportTransactions(String fileName,
70  			List<TransactionDataType> transactions)
71  			throws ClearcheckbookException {
72  		return this.fileHandler.exportTransactions(fileName, transactions);
73  	}
74  
75  	/***
76  	 * fetch accounts from memory cache if fetched already.
77  	 * 
78  	 * @return
79  	 * @throws ClearcheckbookException
80  	 */
81  	public List<AccountDataType> getAccounts() throws ClearcheckbookException {
82  		return new ArrayList<>(getAccountsMap().values());
83  	}
84  
85  	/***
86  	 * fetch accounts from memory cache if fetched already.
87  	 * 
88  	 * @return
89  	 * @throws ClearcheckbookException
90  	 */
91  	public Map<Long, AccountDataType> getAccountsMap()
92  			throws ClearcheckbookException {
93  		if (null == accountsMap) {
94  
95  			List<AccountDataType> accounts = this.connection.account().getAll();
96  			accountsMap = new HashMap<Long, AccountDataType>();
97  			for (Iterator<AccountDataType> iterator = accounts.iterator(); iterator
98  					.hasNext();) {
99  				AccountDataType accountDataType = iterator.next();
100 				accountsMap.put(accountDataType.getId(), accountDataType);
101 			}
102 		}
103 		return accountsMap;
104 	}
105 
106 	public List<CategoryDataType> getCategories()
107 			throws ClearcheckbookException {
108 		return this.connection.category().getAll();
109 	}
110 
111 	public List<LimitDataType> getLimits() throws ClearcheckbookException {
112 		return this.connection.limit().getAll();
113 	}
114 
115 	public List<ReminderDataType> getReminders() throws ClearcheckbookException {
116 		return this.connection.reminder().getAll();
117 	}
118 
119 	public List<TransactionDataType> getTransactions()
120 			throws ClearcheckbookException {
121 		return this.connection.transaction().getAll();
122 	}
123 
124 	public List<TransactionDataType> getTransactions(AccountDataType account)
125 			throws ClearcheckbookException {
126 		return this.connection.transaction().getAll(account);
127 	}
128 
129 	public List<AccountDataType> importAccounts(String fileName)
130 			throws ClearcheckbookException {
131 		// remove the cache
132 		this.accountsMap = null;
133 		return this.fileHandler.importAccounts(fileName);
134 	}
135 
136 	public List<TransactionDataType> importTransactions(
137 			String transactionsFileName, FilePreProcessor preprocessor)
138 			throws ClearcheckbookException {
139 		return this.fileHandler.importTransactions(transactionsFileName,
140 				preprocessor);
141 	}
142 
143 	/***
144 	 * API extension to compare the provided account id with the list of ids for
145 	 * this user.
146 	 * 
147 	 * @param accountId
148 	 * @return
149 	 * @throws ClearcheckbookException
150 	 */
151 	public boolean isAccountIdValid(Long accountId)
152 			throws ClearcheckbookException {
153 		return getAccountsMap().containsKey(accountId);
154 	}
155 
156 	public List<String> processTransactions(
157 			List<TransactionDataType> dataTypeList)
158 			throws ClearcheckbookException {
159 		return this.connection.transaction().bulkProcess(dataTypeList);
160 	}
161 
162 	public List<String> processTransactions(List<TransactionDataType> modified,
163 	List<TransactionDataType> original) throws ClearcheckbookException {
164 		List<TransactionDataType> processList = new LinkedList<>();
165 		for (TransactionDataType transcationToProcess : modified) {
166 			if(!original.contains(transcationToProcess)) {
167 				processList.add(transcationToProcess);
168 			}
169 		}
170 		return this.connection.transaction().bulkProcess(processList);
171 	}
172 
173 	public void processAccounts(List<AccountDataType> accounts)
174 			throws ClearcheckbookException {
175 		this.connection.account().bulkProcess(accounts);
176 	}
177 }