KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 import org.compiere.util.*;
20
21 /**
22  * Product Attribute
23  *
24  * @author Jorg Janke
25  * @version $Id: MAttribute.java,v 1.1 2003/08/26 17:30:35 jjanke Exp $
26  */

27 public class MAttribute extends X_M_Attribute
28 {
29     /**
30      * Standard Constructor
31      * @param ctx context
32      * @param M_Attribute_ID id
33      */

34     public MAttribute (Properties ctx, int M_Attribute_ID)
35     {
36         super (ctx, M_Attribute_ID);
37         /** if (M_Attribute_ID == 0)
38         {
39         setIsInstanceAttribute (false);
40         setIsMandatory (false);
41         setM_Attribute_ID (0);
42         setName (null);
43         }
44         **/

45     } // MAttribute
46

47     /**
48      * Load Constructor
49      * @param ctx context
50      * @param rs result set
51      */

52     public MAttribute (Properties ctx, ResultSet rs)
53     {
54         super (ctx, rs);
55     } // MAttribute
56

57     /** Values */
58     private MAttributeValue[] m_values = null;
59
60     /**
61      * Get Values if List
62      * @return Values or null if not list
63      */

64     public MAttributeValue[] getMAttributeValues()
65     {
66         if (m_values == null && isList())
67         {
68             ArrayList list = new ArrayList();
69             if (!isMandatory())
70                 list.add (null);
71             //
72
String JavaDoc sql = "SELECT * FROM M_AttributeValue "
73                 + "WHERE M_Attribute_ID=? "
74                 + "ORDER BY Value";
75             PreparedStatement pstmt = null;
76             try
77             {
78                 pstmt = DB.prepareStatement(sql);
79                 pstmt.setInt(1, getM_Attribute_ID());
80                 ResultSet rs = pstmt.executeQuery();
81                 while (rs.next())
82                     list.add(new MAttributeValue (getCtx(), rs));
83                 rs.close();
84                 pstmt.close();
85                 pstmt = null;
86             }
87             catch (SQLException ex)
88             {
89                 log.error("getValues", ex);
90             }
91             try
92             {
93                 if (pstmt != null)
94                     pstmt.close();
95             }
96             catch (SQLException ex1)
97             {
98             }
99             pstmt = null;
100             m_values = new MAttributeValue[list.size()];
101             list.toArray(m_values);
102         }
103         return m_values;
104     } // getValues
105

106     /*************************************************************************/
107
108     /**
109      * Get Attribute Instance
110      * @param M_AttributeSetInstance_ID attribute set instance
111      * @return Attribute Instance or null
112      */

113     public MAttributeInstance getMAttributeInstance (int M_AttributeSetInstance_ID)
114     {
115         MAttributeInstance retValue = null;
116         String JavaDoc sql = "SELECT * "
117             + "FROM M_AttributeInstance "
118             + "WHERE M_Attribute_ID=? AND M_AttributeSetInstance_ID=?";
119         PreparedStatement pstmt = null;
120         try
121         {
122             pstmt = DB.prepareStatement (sql);
123             pstmt.setInt (1, getM_Attribute_ID());
124             pstmt.setInt(2, M_AttributeSetInstance_ID);
125             ResultSet rs = pstmt.executeQuery ();
126             if (rs.next ())
127                 retValue = new MAttributeInstance (getCtx(), rs);
128             rs.close ();
129             pstmt.close ();
130             pstmt = null;
131         }
132         catch (SQLException ex)
133         {
134             log.error ("getAttributeInstance", ex);
135         }
136         try
137         {
138             if (pstmt != null)
139                 pstmt.close ();
140         }
141         catch (SQLException ex1)
142         {
143         }
144         pstmt = null;
145
146         return retValue;
147     } // getAttributeInstance
148

149     /**
150      * Set Attribute Instance
151      * @param value value
152      * @param M_AttributeSetInstance_ID id
153      */

154     public void setMAttributeInstance (int M_AttributeSetInstance_ID, MAttributeValue value)
155     {
156         MAttributeInstance instance = getMAttributeInstance(M_AttributeSetInstance_ID);
157         if (instance == null)
158         {
159             if (value != null)
160                 instance = new MAttributeInstance (getCtx (), getM_Attribute_ID (),
161                     M_AttributeSetInstance_ID, value.getM_AttributeValue_ID (),
162                     value.getName ()); // Cached !!
163
else
164                 instance = new MAttributeInstance (getCtx(), getM_Attribute_ID(),
165                     M_AttributeSetInstance_ID, 0, null);
166         }
167         else
168         {
169             if (value != null)
170             {
171                 instance.setM_AttributeValue_ID (value.getM_AttributeValue_ID ());
172                 instance.setValue (value.getName()); // Cached !!
173
}
174             else
175             {
176                 instance.setM_AttributeValue_ID (0);
177                 instance.setValue (null);
178             }
179         }
180         instance.save();
181     } // setAttributeInstance
182

183     /**
184      * Set Attribute Instance
185      * @param value value
186      * @param M_AttributeSetInstance_ID id
187      */

188     public void setMAttributeInstance (int M_AttributeSetInstance_ID, String JavaDoc value)
189     {
190         MAttributeInstance instance = getMAttributeInstance(M_AttributeSetInstance_ID);
191         if (instance == null)
192             instance = new MAttributeInstance (getCtx(), getM_Attribute_ID(), M_AttributeSetInstance_ID, value);
193         else
194             instance.setValue(value);
195         instance.save();
196     } // setAttributeInstance
197

198 } // MAttribute
199
Popular Tags