svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / complements / relatedfeatures / RelatedFeaturesFactory.java @ 46874
History | View | Annotate | Download (5.07 KB)
1 |
package org.gvsig.fmap.dal.complements.relatedfeatures; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Arrays; |
5 |
import java.util.Collections; |
6 |
import java.util.List; |
7 |
import org.apache.commons.lang3.ArrayUtils; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.fmap.dal.DALLocator; |
10 |
import org.gvsig.fmap.dal.StoresRepository; |
11 |
import org.gvsig.fmap.dal.complements.RelatedFeatures; |
12 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
13 |
import org.gvsig.fmap.dal.feature.FeatureType; |
14 |
import org.gvsig.tools.ToolsLocator; |
15 |
import org.gvsig.tools.complement.AbstractComplementFactory; |
16 |
import org.gvsig.tools.complement.Complement; |
17 |
import org.gvsig.tools.complement.ComplementsManager; |
18 |
import org.gvsig.tools.dynobject.AbstractDynMethod; |
19 |
import org.gvsig.tools.dynobject.DynMethod; |
20 |
import org.gvsig.tools.dynobject.DynObject; |
21 |
import org.gvsig.tools.dynobject.DynObjectManager; |
22 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
23 |
import org.gvsig.tools.dynobject.Tagged; |
24 |
import org.gvsig.tools.dynobject.Tags; |
25 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
26 |
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException; |
27 |
|
28 |
/**
|
29 |
*
|
30 |
* @author jjdelcerro
|
31 |
*/
|
32 |
public class RelatedFeaturesFactory extends AbstractComplementFactory<Tagged>{ |
33 |
|
34 |
public RelatedFeaturesFactory() {
|
35 |
super(RelatedFeatures.COMPLEMENT_MANE);
|
36 |
} |
37 |
|
38 |
@Override
|
39 |
public Complement<Tagged> createComplement(Tagged object) {
|
40 |
return new RelatedFeaturesImpl(this, object); |
41 |
} |
42 |
|
43 |
private static class RelatedTableTagValues extends AbstractDynMethod { |
44 |
|
45 |
public RelatedTableTagValues() {
|
46 |
super("RelatedTableTagValues"); |
47 |
} |
48 |
|
49 |
@Override
|
50 |
public Object invoke(DynObject self, Object[] args) throws DynMethodException { |
51 |
StoresRepository repository = DALLocator.getDataManager().getStoresRepository(); |
52 |
List<String> tableNames = new ArrayList<>(); |
53 |
for (String tableName : repository.keySet()) { |
54 |
tableNames.add(tableName); |
55 |
} |
56 |
Collections.sort(tableNames);
|
57 |
DynObjectValueItem[] values = new DynObjectValueItem[tableNames.size()]; |
58 |
for (int i = 0; i < tableNames.size(); i++) { |
59 |
values[i] = new DynObjectValueItem(tableNames.get(i),tableNames.get(i));
|
60 |
} |
61 |
return values;
|
62 |
} |
63 |
} |
64 |
|
65 |
|
66 |
private static class RelatedUniqueFieldNameTagValues extends AbstractDynMethod { |
67 |
|
68 |
public RelatedUniqueFieldNameTagValues() {
|
69 |
super("RelatedUniqueFieldNameTagValues"); |
70 |
} |
71 |
|
72 |
@Override
|
73 |
public Object invoke(DynObject self, Object[] args) throws DynMethodException { |
74 |
if( ArrayUtils.isEmpty(args) ) {
|
75 |
return null; |
76 |
} |
77 |
if( !(args[0] instanceof Tags) ) { |
78 |
return null; |
79 |
} |
80 |
Tags tags = (Tags) args[0];
|
81 |
String tableName = tags.getString(RelatedFeatures.DAL_RELATED_TABLE, null); |
82 |
if( StringUtils.isBlank(tableName) ) {
|
83 |
return null; |
84 |
} |
85 |
StoresRepository repository = DALLocator.getDataManager().getStoresRepository(); |
86 |
FeatureType ft = repository.getFeatureType(tableName); |
87 |
if( ft == null ) { |
88 |
return null; |
89 |
} |
90 |
List<String> attrNames = new ArrayList<>(); |
91 |
for (FeatureAttributeDescriptor attr : ft) {
|
92 |
attrNames.add(attr.getName()); |
93 |
} |
94 |
Collections.sort(attrNames);
|
95 |
DynObjectValueItem[] values = new DynObjectValueItem[attrNames.size()]; |
96 |
for (int i = 0; i < attrNames.size(); i++) { |
97 |
values[i] = new DynObjectValueItem(attrNames.get(i),attrNames.get(i));
|
98 |
} |
99 |
return values;
|
100 |
} |
101 |
} |
102 |
|
103 |
|
104 |
public static void selfRegister() { |
105 |
ComplementsManager complementsManager = ToolsLocator.getComplementsManager(); |
106 |
complementsManager.registerComplement(new RelatedFeaturesFactory());
|
107 |
|
108 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
109 |
dynObjectManager.registerTag( |
110 |
RelatedFeatures.DAL_RELATED_COLUMNS, |
111 |
"Part of a table of related features definition, stores the name of the fields to be shown in the table separated by two points."
|
112 |
); |
113 |
dynObjectManager.registerTag( |
114 |
RelatedFeatures.DAL_RELATED_TABLE, |
115 |
"Part of a table of related features definition, store the table name of features.",
|
116 |
new RelatedTableTagValues()
|
117 |
); |
118 |
dynObjectManager.registerTag( |
119 |
RelatedFeatures.DAL_RELATED_UNIQUE_FIELD_NAME, |
120 |
"Part of a table of related features definition, store the name of the field that is a unique key in the features table.",
|
121 |
new RelatedUniqueFieldNameTagValues()
|
122 |
); |
123 |
// dynObjectManager.registerTag(
|
124 |
// RelatedFeatures.DAL_RELATED_FOREING_KEY_NAME,
|
125 |
// "Part of a table of related features definition, store the name of the field that is the foreing key in the features table."
|
126 |
// );
|
127 |
} |
128 |
} |