1 16 17 package com.opensourcestrategies.financials.integration; 18 19 import java.io.BufferedWriter ; 20 import java.io.FileNotFoundException ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 import java.io.OutputStreamWriter ; 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 26 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 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 54 55 public class GLExportServices { 56 57 public static String module = GLExportServices.class.getName(); 58 59 65 public static Map exportGLToFile(DispatchContext dctx, Map context) { 66 LocalDispatcher dispatcher = dctx.getDispatcher(); 67 GenericDelegator delegator = dctx.getDelegator(); 68 Locale locale = (Locale ) context.get("locale"); 69 List allTransactions = (List ) context.get("valuesToCreate"); 70 allTransactions.addAll((List ) context.get("valuesToStore")); 71 72 String fileName = UtilProperties.getPropertyValue("GLExport.properties", "file.name"); 74 String templateName = UtilProperties.getPropertyValue("GLExport.properties", "template.name"); 75 Debug.logInfo("templateName = " + templateName, module); 76 77 StringBuffer exportedGL = new StringBuffer (); 78 79 try { 80 allTransactions = EntityUtil.filterByAnd(allTransactions, UtilMisc.toMap("isPosted", "Y")); 82 83 Map glAccountMapping = new HashMap (); Map externalAccountParties = new HashMap (); 86 for (Iterator aTi = allTransactions.iterator(); aTi.hasNext(); ) { 87 GenericValue acctgTrans = (GenericValue) aTi.next(); 88 List acctgTransEntries = acctgTrans.getRelated("AcctgTransEntry", UtilMisc.toList("debitCreditFlag DESC", "acctgTransEntrySeqId")); 90 for (Iterator aTEi = acctgTransEntries.iterator(); aTEi.hasNext(); ) { 92 GenericValue acctgTransEntry = (GenericValue) aTEi.next(); 93 94 if (glAccountMapping.get(acctgTransEntry.getString("glAccountId")) == null) { 95 String mappedGlAccountId = UtilProperties.getPropertyValue("GLExport.properties", "glAccountId." + acctgTransEntry.getString("glAccountId")); 97 98 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 externalPartyId = UtilProperties.getPropertyValue("GLExport.properties", "party." + mappedGlAccountId); 104 if ((externalPartyId != null) && !(externalPartyId.equals(""))) { 105 externalAccountParties.put(mappedGlAccountId, externalPartyId); 106 } 107 } 108 } 109 } 110 111 Map 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 outWriter = new StringWriter (); 116 Map tmpResult = ContentWorker.renderContentAsText(delegator, templateName, outWriter, inContext, 117 null, locale, "text/plain"); 118 Debug.logInfo("output: " + outWriter.toString(), module); 119 120 exportedGL.append(outWriter.toString()); 122 } 123 124 PrintWriter out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (new FileOutputStream (fileName)))); 126 out.println(exportedGL.toString()); 127 out.close(); 128 129 Map result = ServiceUtil.returnSuccess(); 131 result.put("toCreateInserted", new Long (allTransactions.size())); 132 result.put("toCreateUpdated", new Long (0)); 133 result.put("toCreateNotUpdated", new Long (0)); 134 result.put("toStoreInserted", new Long (0)); 135 result.put("toStoreUpdated", new Long (0)); 136 result.put("toStoreNotUpdated", new Long (0)); 137 result.put("toRemoveDeleted", new Long (0)); 138 result.put("toRemoveAlreadyDeleted", new Long (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 ex) { 147 return ServiceUtil.returnError(ex.getMessage()); 148 } catch (IOException ex) { 149 return ServiceUtil.returnError(ex.getMessage()); 150 } 151 152 } 153 } 154 | Popular Tags |