KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > marketing > report > ReportHelper


1 /*
2  * $Id: ReportHelper.java 6395 2005-12-21 21:35:48 mujinsong $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.marketing.report;
25
26 import org.ofbiz.base.util.UtilMisc;
27 import org.ofbiz.base.util.Debug;
28 import org.ofbiz.entity.GenericValue;
29 import org.ofbiz.entity.util.EntityUtil;
30 import org.ofbiz.marketing.tracking.TrackingCodeEvents;
31
32 import java.util.*;
33
34 /**
35  * Marketing Report Helper
36  *
37  * @author <a HREF="mailto:mujinsong@gmail.com">Mu Jinsong</a>
38  * @author <a HREF="mailto:sichen@opensourcestrategies.com">Si Chen</a>
39  * @version $Rev: 6395
40  * @since 3.2
41  */

42 public class ReportHelper {
43
44     public static final String JavaDoc module = ReportHelper.class.getName();
45     
46 /**
47  * Calculate conversion rates based on a List of visits and orders. Designed to be used for reporting on
48  * tracking code or marketing campaigns
49  * @param visits
50  * @param orders
51  * @param keyFieldName - name of key field for visits and orders Lists, ie "trackingCodeId" or "marketingCampaignId"
52  * @return a List of Maps with keys (${keyFieldName}, visits - # visits, orders - # orders, orderAmount - total amount of orders,
53  * conversionRate - # orders/# visits
54  */

55     public static List calcConversionRates(List visits, List orders, String JavaDoc keyFieldName) {
56         List conversionRates = new ArrayList();
57         
58         // loop through all the visits
59
for (Iterator vit = visits.iterator(); vit.hasNext(); ) {
60             GenericValue visit = (GenericValue) vit.next();
61             Map reportValue = new HashMap();
62             reportValue.put(keyFieldName, visit.getString(keyFieldName));
63             reportValue.put("visits", visit.getLong("visitId")); // actually # of visits
64

65             // find the matching entry in orders for the given key field
66
List ordersForThisKey = EntityUtil.filterByAnd(orders, UtilMisc.toMap(keyFieldName, visit.getString(keyFieldName)));
67             
68             // if there are matching orders, then calculate orders, order amount, and conversion rate
69
if ((ordersForThisKey != null) && (ordersForThisKey.size() > 0)) {
70                 // note: there should be only one line of order stats per key, so .get(0) should work
71
GenericValue orderValue = (GenericValue) ordersForThisKey.get(0);
72
73                 reportValue.put("orders", orderValue.getLong("orderId")); // # of orders
74
if (orderValue.getDouble("grandTotal") == null) {
75                     reportValue.put("orderAmount", new Double JavaDoc(0));
76                 } else {
77                     reportValue.put("orderAmount", orderValue.getDouble("grandTotal"));
78                 }
79                 if ((orderValue.getLong("orderId") == null) || (visit.getLong("visitId") == null) ||
80                     (visit.getLong("visitId").intValue() == 0)) {
81                     reportValue.put("conversionRate", new Double JavaDoc(0));
82                 } else {
83                     reportValue.put("conversionRate", new Double JavaDoc(orderValue.getLong("orderId").doubleValue() / visit.getLong("visitId").doubleValue()));
84                 }
85             } else {
86                 // no matching orders - all those values are zeroes
87
reportValue.put("orders", new Long JavaDoc(0));
88                 reportValue.put("orderAmount", new Double JavaDoc(0));
89                 reportValue.put("conversionRate", new Double JavaDoc(0));
90             }
91             
92             conversionRates.add(reportValue);
93         }
94         
95         return conversionRates;
96     }
97 }
98
99
100
101
102
Popular Tags