1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package com.sun.star.wizards.db; 28 29 import com.sun.star.beans.Property; 30 import com.sun.star.beans.PropertyValue; 31 import com.sun.star.beans.XPropertySet; 32 import com.sun.star.sdbc.DataType; 33 import com.sun.star.wizards.common.Properties; 34 import com.sun.star.wizards.common.PropertyNames; 35 // import com.sun.star.wizards.db.TypeInspector; 36 37 public class ColumnPropertySet 38 { 39 40 TypeInspector oTypeInspector; 41 public XPropertySet xPropertySet; 42 private int nType; 43 private String sTypeName = PropertyNames.EMPTY_STRING; 44 45 public ColumnPropertySet(TypeInspector _oTypeInspector, XPropertySet _xPropertySet) 46 { 47 xPropertySet = _xPropertySet; 48 oTypeInspector = _oTypeInspector; 49 } 50 51 private PropertyValue[] propertySet2PropertyValueArray(XPropertySet _xNewPropertySet) throws com.sun.star.uno.Exception 52 { 53 Property[] props = _xNewPropertySet.getPropertySetInfo().getProperties(); 54 PropertyValue[] ret = new PropertyValue[props.length]; 55 for (int i = 0; i < props.length; i++) 56 { 57 PropertyValue val = new PropertyValue(); 58 val.Name = props[i].Name; 59 val.Value = _xNewPropertySet.getPropertyValue(val.Name); 60 ret[i] = val; 61 } 62 return ret; 63 } 64 65 private void assignPropertyValues(String _sNewName, PropertyValue[] _aNewColPropertyValues, boolean _bsetDefaultProperties) 66 { 67 try 68 { 69 nType = ((Integer) Properties.getPropertyValue(_aNewColPropertyValues, "Type")).intValue(); 70 nType = oTypeInspector.convertDataType(nType); 71 if (Properties.hasPropertyValue(_aNewColPropertyValues, "TypeName")) 72 { 73 sTypeName = (String) Properties.getPropertyValue(_aNewColPropertyValues, "TypeName"); 74 } 75 Integer precision = null; 76 if (Properties.hasPropertyValue(_aNewColPropertyValues, "Precision")) 77 { 78 precision = (Integer) Properties.getPropertyValue(_aNewColPropertyValues, "Precision"); 79 80 } 81 if ((nType == DataType.VARCHAR) && (precision == null || precision.intValue() == 0)) 82 { 83 precision = 50; 84 } 85 if (precision != null) 86 { 87 xPropertySet.setPropertyValue("Precision", precision); 88 } 89 setType(nType, sTypeName, precision); 90 for (int i = 0; i < _aNewColPropertyValues.length; i++) 91 { 92 String sPropName = _aNewColPropertyValues[i].Name; 93 if (_sNewName != null && sPropName.equals(PropertyNames.PROPERTY_NAME)) 94 { 95 xPropertySet.setPropertyValue(PropertyNames.PROPERTY_NAME, _sNewName); 96 } 97 else if (sPropName.equals("Precision")) 98 { 99 // do nothing, see above 100 } 101 else if ((!sPropName.equals("Type")) && (!sPropName.equals("TypeName"))) 102 { 103 Object oColValue = _aNewColPropertyValues[i].Value; 104 assignPropertyValue(sPropName, oColValue); 105 } 106 } 107 if (_bsetDefaultProperties) 108 { 109 assignPropertyValue("IsNullable", new Integer(oTypeInspector.isNullable(xPropertySet))); 110 } 111 } 112 catch (Exception e) 113 { 114 e.printStackTrace(System.out); 115 } 116 117 } 118 119 public void assignPropertyValues(PropertyValue[] _aNewColPropertyValues, boolean _bsetDefaultProperties) 120 { 121 assignPropertyValues(null /* dont change the name */, _aNewColPropertyValues, _bsetDefaultProperties); 122 } 123 124 public void assignNewPropertySet(String _sNewName, XPropertySet _xNewPropertySet) 125 { 126 try 127 { 128 assignPropertyValues( 129 _sNewName, propertySet2PropertyValueArray(_xNewPropertySet), false /*setDefaultProperties*/); 130 } 131 catch (Exception e) 132 { 133 e.printStackTrace(System.out); 134 } 135 } 136 137 private int getPrecision() 138 { 139 try 140 { 141 return ((Integer) xPropertySet.getPropertyValue("Precision")).intValue(); 142 } 143 catch (Exception e) 144 { 145 e.printStackTrace(System.out); 146 return 0; 147 } 148 } 149 150 private void setType(int _nType, String _sTypeName, Integer precision) 151 { 152 if (_sTypeName.equals(PropertyNames.EMPTY_STRING)) 153 { 154 sTypeName = oTypeInspector.getDefaultTypeName(nType, precision); 155 } 156 else 157 { 158 sTypeName = _sTypeName; 159 } 160 nType = oTypeInspector.getDataType(sTypeName); 161 assignPropertyValue("Type", new Integer(nType)); 162 assignPropertyValue("TypeName", sTypeName); 163 } 164 165 private void assignPropertyValue(String _spropname, Object _oValue) 166 { 167 try 168 { 169 if (_spropname.equals("Type")) 170 { 171 nType = ((Integer) _oValue).intValue(); 172 xPropertySet.setPropertyValue("Type", new Integer(nType)); 173 } 174 else if (_spropname.equals(PropertyNames.PROPERTY_NAME)) 175 { 176 String sName = (String) _oValue; 177 if (!sName.equals(PropertyNames.EMPTY_STRING)) 178 { 179 xPropertySet.setPropertyValue(PropertyNames.PROPERTY_NAME, sName); 180 } 181 } 182 else if (_spropname.equals("Scale")) 183 { 184 int nScale = ((Integer) _oValue).intValue(); 185 nScale = oTypeInspector.getScale(xPropertySet); 186 xPropertySet.setPropertyValue("Scale", new Integer(nScale)); 187 } 188 else if (_spropname.equals("IsNullable")) 189 { 190 int nNullability = ((Integer) _oValue).intValue(); 191 nNullability = oTypeInspector.getNullability(xPropertySet, nNullability); 192 xPropertySet.setPropertyValue("IsNullable", new Integer(nNullability)); 193 } 194 else if (_spropname.equals("TypeName")) 195 { 196 String sTypeName = (String) _oValue; 197 xPropertySet.setPropertyValue("TypeName", sTypeName); 198 } 199 else 200 { 201 xPropertySet.setPropertyValue(_spropname, _oValue); 202 } 203 } 204 catch (Exception e) 205 { 206 e.printStackTrace(System.out); 207 } 208 } 209 210 private int getType() 211 { 212 return nType; 213 } 214 } 215