KickJava   Java API By Example, From Geeks To Geeks.

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


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 Smart 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-2003 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.text.DateFormat JavaDoc;
19
20 import org.compiere.util.*;
21
22 /**
23  * Product Attribute Set Instance
24  *
25  * @author Jorg Janke
26  * @version $Id: MAttributeSetInstance.java,v 1.4 2003/10/21 05:31:40 jjanke Exp $
27  */

28 public class MAttributeSetInstance extends X_M_AttributeSetInstance
29 {
30     /**
31      * Get Attribute Set Instance from ID or Product
32      * @param ctx context
33      * @param M_AttributeSetInstance_ID id or 0
34      * @param M_Product_ID required if id is 0
35      * @return Attribute Set Instance or null
36      */

37     public static MAttributeSetInstance get (Properties ctx, int M_AttributeSetInstance_ID, int M_Product_ID)
38     {
39         MAttributeSetInstance retValue = null;
40         // Load Instance if not 0
41
if (M_AttributeSetInstance_ID != 0)
42         {
43             s_log.debug("get - from M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID);
44             return new MAttributeSetInstance (ctx, M_AttributeSetInstance_ID);
45         }
46         // Get new from Product
47
s_log.debug("get - from M_Product_ID=" + M_Product_ID);
48         if (M_Product_ID == 0)
49             return null;
50         String JavaDoc sql = "SELECT M_AttributeSet_ID, M_AttributeSetInstance_ID "
51             + "FROM M_Product "
52             + "WHERE M_Product_ID=?";
53         PreparedStatement pstmt = null;
54         try
55         {
56             pstmt = DB.prepareStatement(sql);
57             pstmt.setInt(1, M_Product_ID);
58             ResultSet rs = pstmt.executeQuery();
59             if (rs.next())
60             {
61                 int M_AttributeSet_ID = rs.getInt(1);
62             // M_AttributeSetInstance_ID = rs.getInt(2); // needed ?
63
//
64
retValue = new MAttributeSetInstance (ctx, 0, M_AttributeSet_ID);
65             }
66             rs.close();
67             pstmt.close();
68             pstmt = null;
69         }
70         catch (SQLException ex)
71         {
72             s_log.error("get", ex);
73         }
74         try
75         {
76             if (pstmt != null)
77                 pstmt.close();
78         }
79         catch (SQLException ex1)
80         {
81         }
82         pstmt = null;
83         //
84
return retValue;
85     } // get
86

87     private static Logger s_log = Logger.getCLogger (MAttributeSetInstance.class);
88
89     /*************************************************************************/
90
91     /**
92      * Standard Constructor
93      * @param ctx context
94      * @param M_AttributeSetInstance_ID id
95      */

96     public MAttributeSetInstance (Properties ctx, int M_AttributeSetInstance_ID)
97     {
98         super (ctx, M_AttributeSetInstance_ID);
99     } // MAttributeSetInstance
100

101     /**
102      * Load Constructor
103      * @param ctx context
104      * @param rs result set
105      */

106     public MAttributeSetInstance (Properties ctx, ResultSet rs)
107     {
108         super (ctx, rs);
109     } // MAttributeSetInstance
110

111     /**
112      * Standard Constructor
113      * @param ctx context
114      * @param M_AttributeSetInstance_ID id
115      * @param M_AttributeSet_ID attribute set
116      */

117     public MAttributeSetInstance (Properties ctx, int M_AttributeSetInstance_ID, int M_AttributeSet_ID)
118     {
119         super (ctx, M_AttributeSetInstance_ID);
120         setM_AttributeSet_ID(M_AttributeSet_ID);
121     } // MAttributeSetInstance
122

123     /** Attribute Set */
124     private MAttributeSet m_mas = null;
125     /** Date Format */
126     private DateFormat JavaDoc m_dateFormat = DisplayType.getDateFormat(DisplayType.Date);
127
128     /**
129      * Set Attribute Set
130      * @param mas attribute set
131      */

132     public void setMAttributeSet (MAttributeSet mas)
133     {
134         m_mas = mas;
135         setM_AttributeSet_ID(mas.getM_AttributeSet_ID());
136     } // setAttributeSet
137

138     /**
139      * Get Attribute Set
140      * @return Attrbute Set or null
141      */

142     public MAttributeSet getMAttributeSet()
143     {
144         if (m_mas == null && getM_AttributeSet_ID() != 0)
145             m_mas = new MAttributeSet (getCtx(), getM_AttributeSet_ID());
146         return m_mas;
147     } // getMAttributeSet
148

149     /**
150      * Set Description.
151      * - SerNo = #123
152      * - Lot = L123
153      * - GuaranteeDate = 10/25/2003
154      * - Instance Values
155      * - Product Values
156      */

157     public void setDescription()
158     {
159         // Make sure we have a Attribute Set
160
getMAttributeSet();
161         if (m_mas == null)
162         {
163             setDescription ("??");
164             return;
165         }
166         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
167         // SerNo
168
if (m_mas.isSerNo() && getSerNo() != null)
169             sb.append("#").append(getSerNo());
170         // Lot
171
if (m_mas.isLot() && getLot() != null)
172         {
173             if (sb.length() > 0)
174                 sb.append("_");
175             sb.append("\u013D").append(getLot ());
176         }
177         // GuaranteeDate
178
if (m_mas.isGuaranteeDate() && getGuaranteeDate() != null)
179         {
180             if (sb.length() > 0)
181                 sb.append("_");
182             sb.append (m_dateFormat.format (getGuaranteeDate()));
183         }
184         // Instance Attribute Values
185
MAttribute[] attributes = m_mas.getMAttributes(false);
186         for (int i = 0; i < attributes.length; i++)
187         {
188             if (sb.length() > 0 && i == 0)
189                 sb.append("_");
190             if (i != 0)
191                 sb.append("-");
192             MAttributeInstance mai = attributes[i].getMAttributeInstance(getM_AttributeSetInstance_ID());
193             if (mai != null && mai.getValue() != null)
194                 sb.append(mai.getValue());
195         }
196         // Product Attribute Values
197
attributes = m_mas.getMAttributes(true);
198         for (int i = 0; i < attributes.length; i++)
199         {
200             if (sb.length() > 0 && i == 0)
201                 sb.append("_");
202             if (i != 0)
203                 sb.append("-");
204             MAttributeInstance mai = attributes[i].getMAttributeInstance(getM_AttributeSetInstance_ID());
205             if (mai != null && mai.getValue() != null)
206                 sb.append(mai.getValue());
207         }
208         //
209
// log.debug("setDescription - " + sb.toString());
210
setDescription (sb.toString());
211     } // setDescription
212

213     
214
215     /**
216      * Get Guarantee Date
217      * @param getNew if true calculates/sets guarantee date
218      * @return guarantee date
219      */

220     public Timestamp getGuaranteeDate(boolean getNew)
221     {
222         if (getNew)
223         {
224             int days = getMAttributeSet().getGuaranteeDays();
225             Timestamp ts = TimeUtil.addDays(new Timestamp(System.currentTimeMillis()), days);
226             setGuaranteeDate(ts);
227         }
228         return getGuaranteeDate();
229     } // getGuaranteeDate
230

231     /**
232      * Get Lot No
233      * @param getNew if true create/set new lot
234      * @param M_Product_ID product used if new
235      * @return lot
236      */

237     public String JavaDoc getLot (boolean getNew, int M_Product_ID)
238     {
239         if (getNew)
240         {
241             int M_LotCtl_ID = getMAttributeSet().getM_LotCtl_ID();
242             if (M_LotCtl_ID != 0)
243             {
244                 MLotCtl ctl = new MLotCtl (getCtx(), M_LotCtl_ID);
245                 MLot lot = ctl.createLot(M_Product_ID);
246                 setM_Lot_ID (lot.getM_Lot_ID());
247                 setLot (lot.getName());
248             }
249         }
250         return getLot();
251     } // getLot
252

253     /**
254      * Get Serial No
255      * @param getNew if true create/set new Ser No
256      * @return
257      */

258     public String JavaDoc getSerNo(boolean getNew)
259     {
260         if (getNew)
261         {
262             int M_SerNoCtl_ID = getMAttributeSet().getM_SerNoCtl_ID();
263             if (M_SerNoCtl_ID != 0)
264             {
265                 MSerNoCtl ctl = new MSerNoCtl (getCtx(), M_SerNoCtl_ID);
266                 setSerNo(ctl.createSerNo());
267             }
268         }
269         return getSerNo();
270     } // getSerNo
271

272 } // MAttributeSetInstance
273
Popular Tags