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