KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > UOM


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.util.*;
17 import java.sql.*;
18 import java.math.*;
19
20 import org.compiere.util.*;
21
22 /**
23  * Unit Of Measure (R/O Value Object)
24  *
25  * @author Jorg Janke
26  * @version $Id: UOM.java,v 1.6 2003/11/02 07:49:19 jjanke Exp $
27  */

28 public class UOM extends X_C_UOM
29 {
30     /**
31      * Constructor.
32      * @param ctx context
33      * @param C_UOM_ID UOM ID
34      */

35     public UOM (Properties ctx, int C_UOM_ID)
36     {
37         super (ctx, C_UOM_ID);
38     } // UOM
39

40     /**
41      * Load Constructor.
42      * @param ctx context
43      * @param rs result set
44      */

45     public UOM (Properties ctx, ResultSet rs)
46     {
47         super (ctx, rs);
48     } // UOM
49

50     public String JavaDoc toString()
51     {
52         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("UOM[");
53         sb.append("ID=").append(getID())
54             .append(", Name=").append(getName());
55         return sb.toString();
56     }
57
58     public BigDecimal round (BigDecimal qty)
59     {
60         return qty.setScale(getStdPrecision(), BigDecimal.ROUND_HALF_UP);
61     }
62
63     public boolean isMinute()
64     {
65         return X12_MINUTE.equals(getX12DE355());
66     }
67     public boolean isHour()
68     {
69         return X12_HOUR.equals(getX12DE355());
70     }
71     public boolean isDay()
72     {
73         return X12_DAY.equals(getX12DE355());
74     }
75     public boolean isWorkDay()
76     {
77         return X12_DAY_WORK.equals(getX12DE355());
78     }
79     public boolean isWeek()
80     {
81         return X12_WEEK.equals(getX12DE355());
82     }
83     public boolean isMonth()
84     {
85         return X12_MONTH.equals(getX12DE355());
86     }
87     public boolean isWorkMonth()
88     {
89         return X12_MONTH_WORK.equals(getX12DE355());
90     }
91     public boolean isYear()
92     {
93         return X12_YEAR.equals(getX12DE355());
94     }
95
96     /*************************************************************************/
97
98     /** X12 Element 355 Code Minute */
99     static final String JavaDoc X12_MINUTE = "MJ";
100     /** X12 Element 355 Code Hour */
101     static final String JavaDoc X12_HOUR = "HR";
102     /** X12 Element 355 Code Day */
103     static final String JavaDoc X12_DAY = "DA";
104     /** X12 Element 355 Code Work Day (8 hours / 5days) */
105     static final String JavaDoc X12_DAY_WORK = "WD";
106     /** X12 Element 355 Code Week */
107     static final String JavaDoc X12_WEEK = "WK";
108     /** X12 Element 355 Code Month */
109     static final String JavaDoc X12_MONTH = "MO";
110     /** X12 Element 355 Code Work Month (20 days / 4 weeks) */
111     static final String JavaDoc X12_MONTH_WORK = "WM";
112     /** X12 Element 355 Code Year */
113     static final String JavaDoc X12_YEAR = "YR";
114
115
116     /**
117      * Get Minute C_UOM_ID
118      * @param ctx context
119      * @return C_UOM_ID for Minute
120      */

121     public static int getMinute_UOM_ID (Properties ctx)
122     {
123         if (Ini.isClient())
124         {
125             Iterator it = s_cache.values().iterator();
126             while (it.hasNext())
127             {
128                 UOM uom = (UOM)it.next();
129                 if (uom.isMinute())
130                     return uom.getC_UOM_ID();
131             }
132         }
133         // Server
134
int C_UOM_ID = 0;
135         String JavaDoc sql = "SELECT C_UOM_ID FROM C_UOM "
136             + "WHERE IsActive='Y' AND X12DE355='MJ'"; // HardCoded
137
try
138         {
139             PreparedStatement pstmt = DB.prepareStatement(sql);
140             ResultSet rs = pstmt.executeQuery();
141             if (rs.next())
142                 C_UOM_ID = rs.getInt(1);
143             rs.close();
144             pstmt.close();
145         }
146         catch (SQLException e)
147         {
148             Log.error("getMinute_UOM_ID", e);
149         }
150         return C_UOM_ID;
151     } // getMinute_UOM_ID
152

153     /*************************************************************************/
154
155     /** UOM Cache */
156     private static CCache s_cache = new CCache("uom", 30);
157
158     /**
159      * Get UOM from Cache
160      * @param ctx context
161      * @param C_UOM_ID ID
162      * @return UOM
163      */

164     public static UOM get (Properties ctx, int C_UOM_ID)
165     {
166         if (s_cache.size() == 0)
167             loadUOMs (ctx);
168         //
169
Integer JavaDoc ii = new Integer JavaDoc (C_UOM_ID);
170         UOM uom = (UOM)s_cache.get(ii);
171         if (uom != null)
172             return uom;
173         //
174
uom = new UOM (ctx, C_UOM_ID);
175         s_cache.put(new Integer JavaDoc(C_UOM_ID), uom);
176         return uom;
177     } // getUOMfromCache
178

179     /**
180      * Load All UOMs
181      * @param ctx context
182      */

183     private static void loadUOMs (Properties ctx)
184     {
185         String JavaDoc sql = MRole.getDefault(ctx, false).addAccessSQL(
186             "SELECT * FROM C_UOM "
187             + "WHERE IsActive='Y'",
188             "C_UOM", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
189         try
190         {
191             PreparedStatement pstmt = DB.prepareStatement(sql);
192             ResultSet rs = pstmt.executeQuery();
193             while (rs.next())
194             {
195                 UOM uom = new UOM(ctx, rs);
196                 s_cache.put (new Integer JavaDoc(uom.getC_UOM_ID()), uom);
197             }
198             rs.close();
199             pstmt.close();
200         }
201         catch (SQLException e)
202         {
203             Log.error("UOM.loadUOMs", e);
204         }
205     } // loadUOMs
206

207 } // UOM
208
Popular Tags