KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > accounting > finaccount > FinAccountServices


1 /*
2  * $Id: $
3  *
4  * Copyright 2001-2006 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7  * use this file except in compliance with the License. You may obtain a copy of
8  * the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */

18
19 package org.ofbiz.accounting.finaccount;
20
21 import java.sql.Timestamp JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.ofbiz.base.util.Debug;
25 import org.ofbiz.base.util.UtilDateTime;
26 import org.ofbiz.base.util.UtilMisc;
27 import org.ofbiz.entity.GenericDelegator;
28 import org.ofbiz.entity.GenericEntityException;
29 import org.ofbiz.entity.GenericValue;
30 import org.ofbiz.service.DispatchContext;
31 import org.ofbiz.service.GenericServiceException;
32 import org.ofbiz.service.LocalDispatcher;
33 import org.ofbiz.service.ModelService;
34 import org.ofbiz.service.ServiceUtil;
35
36 import org.ofbiz.order.finaccount.FinAccountHelper;
37
38 public class FinAccountServices {
39     
40     public static final String JavaDoc module = FinAccountServices.class.getName();
41     
42     public static Map JavaDoc createFinAccountForStore(DispatchContext dctx, Map JavaDoc context) {
43         GenericDelegator delegator = dctx.getDelegator();
44         LocalDispatcher dispatcher = dctx.getDispatcher();
45         GenericValue userLogin = (GenericValue) context.get("userLogin");
46         String JavaDoc productStoreId = (String JavaDoc) context.get("productStoreId");
47         String JavaDoc finAccountTypeId = (String JavaDoc) context.get("finAccountTypeId");
48         
49         try {
50             // get the product store id and use it to generate a unique fin account code
51
GenericValue productStoreFinAccountSetting = delegator.findByPrimaryKeyCache("ProductStoreFinActSetting", UtilMisc.toMap("productStoreId", productStoreId, "finAccountTypeId", finAccountTypeId));
52             if (productStoreFinAccountSetting == null) {
53                 return ServiceUtil.returnError("No settings found for store [" + productStoreId + "] for fin account type [" + finAccountTypeId + "]");
54             }
55             
56             Long JavaDoc accountCodeLength = productStoreFinAccountSetting.getLong("accountCodeLength");
57             Long JavaDoc accountValidDays = productStoreFinAccountSetting.getLong("accountValidDays");
58             
59             // automatically set the parameters for the create fin account service
60
ModelService createService = dctx.getModelService("createFinAccount");
61             Map JavaDoc inContext = createService.makeValid(context, "IN");
62             Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
63             // now use our values
64
inContext.put("fromDate", now);
65             inContext.put("thruDate", UtilDateTime.getDayEnd(now, accountValidDays.intValue()));
66             String JavaDoc finAccountCode = FinAccountHelper.getNewFinAccountCode(accountCodeLength.intValue(), delegator);
67             inContext.put("finAccountCode", finAccountCode);
68             inContext.put("userLogin", userLogin);
69             Map JavaDoc createResult = dispatcher.runSync("createFinAccount", inContext);
70             
71             if (ServiceUtil.isError(createResult)) {
72                 return createResult;
73             } else {
74                 Map JavaDoc result = ServiceUtil.returnSuccess();
75                 result.put("finAccountId", createResult.get("finAccountId"));
76                 result.put("finAccountCode", finAccountCode);
77                 return result;
78             }
79         } catch (GenericEntityException ex) {
80             return ServiceUtil.returnError(ex.getMessage());
81         } catch (GenericServiceException ex) {
82             return ServiceUtil.returnError(ex.getMessage());
83         }
84         
85     }
86 }
87
Popular Tags