1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 package com.sun.star.wizards.table;
19 
20 import java.util.ArrayList;
21 
22 import com.sun.star.beans.PropertyValue;
23 import com.sun.star.beans.XPropertySet;
24 import com.sun.star.container.XNameAccess;
25 import com.sun.star.lang.Locale;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.wizards.common.Configuration;
29 import com.sun.star.wizards.common.Properties;
30 import com.sun.star.wizards.common.PropertyNames;
31 
32 public class FieldDescription
33 {
34     private String tablename = PropertyNames.EMPTY_STRING;
35     private XPropertySet xPropertySet;
36     private final ArrayList<PropertyValue> aPropertyValues;
37     private String Name;
38 
FieldDescription(XMultiServiceFactory _xMSF, Locale _aLocale, ScenarioSelector _curscenarioselector, String _fieldname, String keyname, int _nmaxcharCount)39     public FieldDescription(XMultiServiceFactory _xMSF, Locale _aLocale, ScenarioSelector _curscenarioselector, String _fieldname, String keyname, int _nmaxcharCount)
40     {
41         tablename = _curscenarioselector.getTableName();
42         Name = _fieldname;
43         aPropertyValues = new ArrayList<PropertyValue>();
44         XNameAccess xNameAccessTableNode = _curscenarioselector.oCGTable.xNameAccessFieldsNode;
45         XNameAccess xNameAccessFieldNode;
46         if (_curscenarioselector.bcolumnnameislimited)
47         {
48             xNameAccessFieldNode = Configuration.getChildNodebyDisplayName(_xMSF, _aLocale, xNameAccessTableNode, keyname, "ShortName", _nmaxcharCount);
49         }
50         else
51         {
52             xNameAccessFieldNode = Configuration.getChildNodebyDisplayName(_xMSF, _aLocale, xNameAccessTableNode, keyname, PropertyNames.PROPERTY_NAME, _nmaxcharCount);
53         }
54         setFieldProperties(xNameAccessFieldNode);
55     }
56 
FieldDescription(String _fieldname)57     public FieldDescription(String _fieldname)
58     {
59         Name = _fieldname;
60         aPropertyValues = new ArrayList<PropertyValue>();
61         Integer Type = Integer.valueOf(com.sun.star.sdbc.DataType.VARCHAR);
62         aPropertyValues.add(Properties.createProperty(PropertyNames.PROPERTY_NAME, _fieldname));
63         aPropertyValues.add(Properties.createProperty("Type", Type));
64     }
65 
setName(String _newfieldname)66     public void setName(String _newfieldname)
67     {
68         for (int i = 0; i < aPropertyValues.size(); i++)
69         {
70             PropertyValue aPropertyValue = aPropertyValues.get(i);
71             if (aPropertyValue.Name.equals(PropertyNames.PROPERTY_NAME))
72             {
73                 aPropertyValue.Value = _newfieldname;
74                 aPropertyValues.set(i, aPropertyValue);
75                 Name = _newfieldname;
76                 return;
77             }
78         }
79     }
80 
getName()81     public String getName()
82     {
83         return Name;
84     }
85 
gettablename()86     public String gettablename()
87     {
88         return tablename;
89     }
90 
propertyexists(String _propertyname)91     private boolean propertyexists(String _propertyname)
92     {
93         boolean bexists = false;
94         try
95         {
96             if (xPropertySet.getPropertySetInfo().hasPropertyByName(_propertyname))
97             {
98                 Object oValue = xPropertySet.getPropertyValue(_propertyname);
99                 bexists = (!com.sun.star.uno.AnyConverter.isVoid(oValue));
100             }
101         }
102         catch (Exception e)
103         {
104             e.printStackTrace(System.err);
105         }
106         return bexists;
107     }
108 
setFieldProperties(XNameAccess _xNameAccessFieldNode)109     private void setFieldProperties(XNameAccess _xNameAccessFieldNode)
110     {
111         try
112         {
113             xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, _xNameAccessFieldNode);
114             if (propertyexists(PropertyNames.PROPERTY_NAME))
115             {
116                 aPropertyValues.add(Properties.createProperty(PropertyNames.PROPERTY_NAME, Name));
117             }
118             if (propertyexists("Type"))
119             {
120                 aPropertyValues.add(Properties.createProperty("Type", xPropertySet.getPropertyValue("Type")));
121             }
122             if (propertyexists("Scale"))
123             {
124                 aPropertyValues.add(Properties.createProperty("Scale", xPropertySet.getPropertyValue("Scale")));
125             }
126             if (propertyexists("Precision"))
127             {
128                 aPropertyValues.add(Properties.createProperty("Precision", xPropertySet.getPropertyValue("Precision")));
129             }
130             if (propertyexists("DefaultValue"))
131             {
132                 aPropertyValues.add(Properties.createProperty("DefaultValue", xPropertySet.getPropertyValue("DefaultValue")));//          DefaultValue = (Boolean) xPropertySet.getPropertyValue("DefaultValue");
133             //Type =  4; // TODO where is the error?(Integer) xPropertySet.getPropertyValue("Type");
134             }
135         }
136         catch (Exception e)
137         {
138             e.printStackTrace(System.err);
139         }
140     }
141 
getPropertyValues()142     public PropertyValue[] getPropertyValues()
143     {
144         if (aPropertyValues != null)
145         {
146             PropertyValue[] aProperties = new PropertyValue[aPropertyValues.size()];
147             aPropertyValues.toArray(aProperties);
148             return aProperties;
149         }
150         return null;
151     }
152 }
153