1*a1b4a26bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a1b4a26bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a1b4a26bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a1b4a26bSAndrew Rist  * distributed with this work for additional information
6*a1b4a26bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a1b4a26bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a1b4a26bSAndrew Rist  * "License"); you may not use this file except in compliance
9*a1b4a26bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a1b4a26bSAndrew Rist  *
11*a1b4a26bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a1b4a26bSAndrew Rist  *
13*a1b4a26bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a1b4a26bSAndrew Rist  * software distributed under the License is distributed on an
15*a1b4a26bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a1b4a26bSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a1b4a26bSAndrew Rist  * specific language governing permissions and limitations
18*a1b4a26bSAndrew Rist  * under the License.
19*a1b4a26bSAndrew Rist  *
20*a1b4a26bSAndrew Rist  *************************************************************/
21*a1b4a26bSAndrew Rist 
22*a1b4a26bSAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.wizards.db;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.Property;
26cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
27cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
28cdf0e10cSrcweir import com.sun.star.sdbc.DataType;
29cdf0e10cSrcweir import com.sun.star.wizards.common.Properties;
30cdf0e10cSrcweir import com.sun.star.wizards.common.PropertyNames;
31cdf0e10cSrcweir // import com.sun.star.wizards.db.TypeInspector;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir public class ColumnPropertySet
34cdf0e10cSrcweir {
35cdf0e10cSrcweir 
36cdf0e10cSrcweir     TypeInspector oTypeInspector;
37cdf0e10cSrcweir     public XPropertySet xPropertySet;
38cdf0e10cSrcweir     private int nType;
39cdf0e10cSrcweir     private String sTypeName = PropertyNames.EMPTY_STRING;
40cdf0e10cSrcweir 
ColumnPropertySet(TypeInspector _oTypeInspector, XPropertySet _xPropertySet)41cdf0e10cSrcweir     public ColumnPropertySet(TypeInspector _oTypeInspector, XPropertySet _xPropertySet)
42cdf0e10cSrcweir     {
43cdf0e10cSrcweir         xPropertySet = _xPropertySet;
44cdf0e10cSrcweir         oTypeInspector = _oTypeInspector;
45cdf0e10cSrcweir     }
46cdf0e10cSrcweir 
propertySet2PropertyValueArray(XPropertySet _xNewPropertySet)47cdf0e10cSrcweir     private PropertyValue[] propertySet2PropertyValueArray(XPropertySet _xNewPropertySet) throws com.sun.star.uno.Exception
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         Property[] props = _xNewPropertySet.getPropertySetInfo().getProperties();
50cdf0e10cSrcweir         PropertyValue[] ret = new PropertyValue[props.length];
51cdf0e10cSrcweir         for (int i = 0; i < props.length; i++)
52cdf0e10cSrcweir         {
53cdf0e10cSrcweir             PropertyValue val = new PropertyValue();
54cdf0e10cSrcweir             val.Name = props[i].Name;
55cdf0e10cSrcweir             val.Value = _xNewPropertySet.getPropertyValue(val.Name);
56cdf0e10cSrcweir             ret[i] = val;
57cdf0e10cSrcweir         }
58cdf0e10cSrcweir         return ret;
59cdf0e10cSrcweir     }
60cdf0e10cSrcweir 
assignPropertyValues(String _sNewName, PropertyValue[] _aNewColPropertyValues, boolean _bsetDefaultProperties)61cdf0e10cSrcweir     private void assignPropertyValues(String _sNewName, PropertyValue[] _aNewColPropertyValues, boolean _bsetDefaultProperties)
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         try
64cdf0e10cSrcweir         {
65cdf0e10cSrcweir             nType = ((Integer) Properties.getPropertyValue(_aNewColPropertyValues, "Type")).intValue();
66cdf0e10cSrcweir             nType = oTypeInspector.convertDataType(nType);
67cdf0e10cSrcweir             if (Properties.hasPropertyValue(_aNewColPropertyValues, "TypeName"))
68cdf0e10cSrcweir             {
69cdf0e10cSrcweir                 sTypeName = (String) Properties.getPropertyValue(_aNewColPropertyValues, "TypeName");
70cdf0e10cSrcweir             }
71cdf0e10cSrcweir             Integer precision = null;
72cdf0e10cSrcweir             if (Properties.hasPropertyValue(_aNewColPropertyValues, "Precision"))
73cdf0e10cSrcweir             {
74cdf0e10cSrcweir                 precision = (Integer) Properties.getPropertyValue(_aNewColPropertyValues, "Precision");
75cdf0e10cSrcweir 
76cdf0e10cSrcweir             }
77cdf0e10cSrcweir             if ((nType == DataType.VARCHAR) && (precision == null || precision.intValue() == 0))
78cdf0e10cSrcweir             {
79cdf0e10cSrcweir                 precision = 50;
80cdf0e10cSrcweir             }
81cdf0e10cSrcweir             if (precision != null)
82cdf0e10cSrcweir             {
83cdf0e10cSrcweir                 xPropertySet.setPropertyValue("Precision", precision);
84cdf0e10cSrcweir             }
85cdf0e10cSrcweir             setType(nType, sTypeName, precision);
86cdf0e10cSrcweir             for (int i = 0; i < _aNewColPropertyValues.length; i++)
87cdf0e10cSrcweir             {
88cdf0e10cSrcweir                 String sPropName = _aNewColPropertyValues[i].Name;
89cdf0e10cSrcweir                 if (_sNewName != null && sPropName.equals(PropertyNames.PROPERTY_NAME))
90cdf0e10cSrcweir                 {
91cdf0e10cSrcweir                     xPropertySet.setPropertyValue(PropertyNames.PROPERTY_NAME, _sNewName);
92cdf0e10cSrcweir                 }
93cdf0e10cSrcweir                 else if (sPropName.equals("Precision"))
94cdf0e10cSrcweir                 {
95cdf0e10cSrcweir                     // do nothing, see above
96cdf0e10cSrcweir                 }
97cdf0e10cSrcweir                 else if ((!sPropName.equals("Type")) && (!sPropName.equals("TypeName")))
98cdf0e10cSrcweir                 {
99cdf0e10cSrcweir                     Object oColValue = _aNewColPropertyValues[i].Value;
100cdf0e10cSrcweir                     assignPropertyValue(sPropName, oColValue);
101cdf0e10cSrcweir                 }
102cdf0e10cSrcweir             }
103cdf0e10cSrcweir             if (_bsetDefaultProperties)
104cdf0e10cSrcweir             {
105cdf0e10cSrcweir                 assignPropertyValue("IsNullable", new Integer(oTypeInspector.isNullable(xPropertySet)));
106cdf0e10cSrcweir             }
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir         catch (Exception e)
109cdf0e10cSrcweir         {
110cdf0e10cSrcweir             e.printStackTrace(System.out);
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir 
assignPropertyValues(PropertyValue[] _aNewColPropertyValues, boolean _bsetDefaultProperties)115cdf0e10cSrcweir     public void assignPropertyValues(PropertyValue[] _aNewColPropertyValues, boolean _bsetDefaultProperties)
116cdf0e10cSrcweir     {
117cdf0e10cSrcweir         assignPropertyValues(null /* dont change the name */, _aNewColPropertyValues, _bsetDefaultProperties);
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir 
assignNewPropertySet(String _sNewName, XPropertySet _xNewPropertySet)120cdf0e10cSrcweir     public void assignNewPropertySet(String _sNewName, XPropertySet _xNewPropertySet)
121cdf0e10cSrcweir     {
122cdf0e10cSrcweir         try
123cdf0e10cSrcweir         {
124cdf0e10cSrcweir             assignPropertyValues(
125cdf0e10cSrcweir                     _sNewName, propertySet2PropertyValueArray(_xNewPropertySet), false /*setDefaultProperties*/);
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir         catch (Exception e)
128cdf0e10cSrcweir         {
129cdf0e10cSrcweir             e.printStackTrace(System.out);
130cdf0e10cSrcweir         }
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
getPrecision()133cdf0e10cSrcweir     private int getPrecision()
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         try
136cdf0e10cSrcweir         {
137cdf0e10cSrcweir             return ((Integer) xPropertySet.getPropertyValue("Precision")).intValue();
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir         catch (Exception e)
140cdf0e10cSrcweir         {
141cdf0e10cSrcweir             e.printStackTrace(System.out);
142cdf0e10cSrcweir             return 0;
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir 
setType(int _nType, String _sTypeName, Integer precision)146cdf0e10cSrcweir     private void setType(int _nType, String _sTypeName, Integer precision)
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         if (_sTypeName.equals(PropertyNames.EMPTY_STRING))
149cdf0e10cSrcweir         {
150cdf0e10cSrcweir             sTypeName = oTypeInspector.getDefaultTypeName(nType, precision);
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir         else
153cdf0e10cSrcweir         {
154cdf0e10cSrcweir             sTypeName = _sTypeName;
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir         nType = oTypeInspector.getDataType(sTypeName);
157cdf0e10cSrcweir         assignPropertyValue("Type", new Integer(nType));
158cdf0e10cSrcweir         assignPropertyValue("TypeName", sTypeName);
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir 
assignPropertyValue(String _spropname, Object _oValue)161cdf0e10cSrcweir     private void assignPropertyValue(String _spropname, Object _oValue)
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         try
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             if (_spropname.equals("Type"))
166cdf0e10cSrcweir             {
167cdf0e10cSrcweir                 nType = ((Integer) _oValue).intValue();
168cdf0e10cSrcweir                 xPropertySet.setPropertyValue("Type", new Integer(nType));
169cdf0e10cSrcweir             }
170cdf0e10cSrcweir             else if (_spropname.equals(PropertyNames.PROPERTY_NAME))
171cdf0e10cSrcweir             {
172cdf0e10cSrcweir                 String sName = (String) _oValue;
173cdf0e10cSrcweir                 if (!sName.equals(PropertyNames.EMPTY_STRING))
174cdf0e10cSrcweir                 {
175cdf0e10cSrcweir                     xPropertySet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName);
176cdf0e10cSrcweir                 }
177cdf0e10cSrcweir             }
178cdf0e10cSrcweir             else if (_spropname.equals("Scale"))
179cdf0e10cSrcweir             {
180cdf0e10cSrcweir                 int nScale = ((Integer) _oValue).intValue();
181cdf0e10cSrcweir                 nScale = oTypeInspector.getScale(xPropertySet);
182cdf0e10cSrcweir                 xPropertySet.setPropertyValue("Scale", new Integer(nScale));
183cdf0e10cSrcweir             }
184cdf0e10cSrcweir             else if (_spropname.equals("IsNullable"))
185cdf0e10cSrcweir             {
186cdf0e10cSrcweir                 int nNullability = ((Integer) _oValue).intValue();
187cdf0e10cSrcweir                 nNullability = oTypeInspector.getNullability(xPropertySet, nNullability);
188cdf0e10cSrcweir                 xPropertySet.setPropertyValue("IsNullable", new Integer(nNullability));
189cdf0e10cSrcweir             }
190cdf0e10cSrcweir             else if (_spropname.equals("TypeName"))
191cdf0e10cSrcweir             {
192cdf0e10cSrcweir                 String sTypeName = (String) _oValue;
193cdf0e10cSrcweir                 xPropertySet.setPropertyValue("TypeName", sTypeName);
194cdf0e10cSrcweir             }
195cdf0e10cSrcweir             else
196cdf0e10cSrcweir             {
197cdf0e10cSrcweir                 xPropertySet.setPropertyValue(_spropname, _oValue);
198cdf0e10cSrcweir             }
199cdf0e10cSrcweir         }
200cdf0e10cSrcweir         catch (Exception e)
201cdf0e10cSrcweir         {
202cdf0e10cSrcweir             e.printStackTrace(System.out);
203cdf0e10cSrcweir         }
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir 
getType()206cdf0e10cSrcweir     private int getType()
207cdf0e10cSrcweir     {
208cdf0e10cSrcweir         return nType;
209cdf0e10cSrcweir     }
210cdf0e10cSrcweir }
211