KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > financials > integration > GLExportServices


1 /*
2  * Copyright (c) 2006 - 2007 Open Source Strategies, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the Honest Public License.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * Honest Public License for more details.
11  *
12  * You should have received a copy of the Honest Public License
13  * along with this program; if not, write to Funambol,
14  * 643 Bair Island Road, Suite 305 - Redwood City, CA 94063, USA
15  */

16
17 package com.opensourcestrategies.financials.integration;
18
19 import java.io.BufferedWriter JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStreamWriter JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.base.util.GeneralException;
35 import org.ofbiz.base.util.UtilMisc;
36 import org.ofbiz.base.util.UtilProperties;
37 import org.ofbiz.content.content.ContentWorker;
38 import org.ofbiz.entity.GenericDelegator;
39 import org.ofbiz.entity.GenericEntityException;
40 import org.ofbiz.content.content.ContentWorker;
41 import org.ofbiz.entity.GenericValue;
42 import org.ofbiz.entity.util.EntityUtil;
43 import org.ofbiz.service.DispatchContext;
44 import org.ofbiz.service.GenericServiceException;
45 import org.ofbiz.service.LocalDispatcher;
46 import org.ofbiz.service.ServiceUtil;
47
48 /**
49  * Services for exporting General Ledger activities
50  * @author <a HREF="mailto:sichen@opensourcestrategies.com">Si Chen</a>
51  * @version $Rev: 553 $
52  * @since 2.2
53 */

54
55 public class GLExportServices {
56     
57     public static String JavaDoc module = GLExportServices.class.getName();
58     
59     /**
60      *
61      * @param dctx
62      * @param result
63      * @return
64      */

65     public static Map JavaDoc exportGLToFile(DispatchContext dctx, Map JavaDoc context) {
66         LocalDispatcher dispatcher = dctx.getDispatcher();
67         GenericDelegator delegator = dctx.getDelegator();
68         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
69         List JavaDoc allTransactions = (List JavaDoc) context.get("valuesToCreate");
70         allTransactions.addAll((List JavaDoc) context.get("valuesToStore"));
71
72         // name of template for generating exported GL and name of file to write it to
73
String JavaDoc fileName = UtilProperties.getPropertyValue("GLExport.properties", "file.name");
74         String JavaDoc templateName = UtilProperties.getPropertyValue("GLExport.properties", "template.name");
75         Debug.logInfo("templateName = " + templateName, module);
76         
77         StringBuffer JavaDoc exportedGL = new StringBuffer JavaDoc();
78         
79         try {
80             // only export posted transactions
81
allTransactions = EntityUtil.filterByAnd(allTransactions, UtilMisc.toMap("isPosted", "Y"));
82
83             Map JavaDoc glAccountMapping = new HashMap JavaDoc(); // map of OFBiz glAccountId to external system's gl account
84
Map JavaDoc externalAccountParties = new HashMap JavaDoc(); // map of external gl account to any required party (vendors, customers)
85

86             for (Iterator JavaDoc aTi = allTransactions.iterator(); aTi.hasNext(); ) {
87                 GenericValue acctgTrans = (GenericValue) aTi.next();
88                 // peculiar QBXML requirement - debits must come before credits
89
List JavaDoc acctgTransEntries = acctgTrans.getRelated("AcctgTransEntry", UtilMisc.toList("debitCreditFlag DESC", "acctgTransEntrySeqId"));
90                 // update the GL mapping
91
for (Iterator JavaDoc aTEi = acctgTransEntries.iterator(); aTEi.hasNext(); ) {
92                     GenericValue acctgTransEntry = (GenericValue) aTEi.next();
93                     
94                     if (glAccountMapping.get(acctgTransEntry.getString("glAccountId")) == null) {
95                         // gl account mappings are in the properties file
96
String JavaDoc mappedGlAccountId = UtilProperties.getPropertyValue("GLExport.properties", "glAccountId." + acctgTransEntry.getString("glAccountId"));
97                         
98                         // if there is no target gl account, then the service should fail so the user can fix it and try again
99
if ((mappedGlAccountId == null) || (mappedGlAccountId.equals(""))) {
100                             return ServiceUtil.returnError("No mapping for GL account " + acctgTransEntry.getString("glAccountId") + " was found. Cannot export");
101                         } else {
102                             glAccountMapping.put(acctgTransEntry.getString("glAccountId"), mappedGlAccountId);
103                             String JavaDoc externalPartyId = UtilProperties.getPropertyValue("GLExport.properties", "party." + mappedGlAccountId);
104                             if ((externalPartyId != null) && !(externalPartyId.equals(""))) {
105                                 externalAccountParties.put(mappedGlAccountId, externalPartyId);
106                             }
107                         }
108                     }
109                 }
110                 
111                 // use the template to generate an exported version of this transactions
112
Map JavaDoc inContext = UtilMisc.toMap("acctgTrans", acctgTrans, "acctgTransEntries", acctgTransEntries, "glAccountMapping", glAccountMapping,
113                                                "externalAccountParties", externalAccountParties);
114                 Debug.logInfo("acctgTransId = " + acctgTrans.getString("acctgTransId") + " with " + acctgTransEntries.size() + " entries", module);
115                 StringWriter JavaDoc outWriter = new StringWriter JavaDoc();
116                 Map JavaDoc tmpResult = ContentWorker.renderContentAsText(delegator, templateName, outWriter, inContext,
117                         null, locale, "text/plain");
118                 Debug.logInfo("output: " + outWriter.toString(), module);
119
120                 // now add it to all the other transactions so far
121
exportedGL.append(outWriter.toString());
122             }
123             
124             // write the file
125
PrintWriter JavaDoc out = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(fileName))));
126             out.println(exportedGL.toString());
127             out.close();
128             
129             // all done. put in some values to satisfy the records
130
Map JavaDoc result = ServiceUtil.returnSuccess();
131             result.put("toCreateInserted", new Long JavaDoc(allTransactions.size()));
132             result.put("toCreateUpdated", new Long JavaDoc(0));
133             result.put("toCreateNotUpdated", new Long JavaDoc(0));
134             result.put("toStoreInserted", new Long JavaDoc(0));
135             result.put("toStoreUpdated", new Long JavaDoc(0));
136             result.put("toStoreNotUpdated", new Long JavaDoc(0));
137             result.put("toRemoveDeleted", new Long JavaDoc(0));
138             result.put("toRemoveAlreadyDeleted", new Long JavaDoc(0));
139             return result;
140         } catch (GenericEntityException ex) {
141             return ServiceUtil.returnError(ex.getMessage());
142         } catch (GenericServiceException ex) {
143             return ServiceUtil.returnError(ex.getMessage());
144         } catch (GeneralException ex) {
145             return ServiceUtil.returnError(ex.getMessage());
146         } catch (FileNotFoundException JavaDoc ex) {
147             return ServiceUtil.returnError(ex.getMessage());
148         } catch (IOException JavaDoc ex) {
149             return ServiceUtil.returnError(ex.getMessage());
150         }
151         
152     }
153 }
154
Popular Tags