|
@@ -1,128 +1,128 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.aokeyhole.objects.factories; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.aokeyhole.objects.*; |
5 |
|
- |
import org.litesoft.aokeyhole.objects.attributes.*; |
6 |
|
- |
import org.litesoft.aokeyhole.objects.support.*; |
7 |
|
- |
import org.litesoft.aokeyhole.toolkit.*; |
8 |
|
- |
|
9 |
|
- |
import java.util.*; |
10 |
|
- |
|
11 |
|
- |
public class AttributeFactoryImpl implements AttributeFactory { |
12 |
|
- |
private PropertyMetaDataDefinitionAccessor mPropertyMetaDataDefinitionAccessor; |
13 |
|
- |
private IdentifierValidator mAttributeIdentifierValidator; |
14 |
|
- |
private String mTypesForError; |
15 |
|
- |
private Map<String, AttributeSet> mSetsByType = new HashMap<String, AttributeSet>(); |
16 |
|
- |
|
17 |
|
- |
public AttributeFactoryImpl( PropertyMetaDataDefinitionAccessor pPropertyMetaDataDefinitionAccessor, IdentifierValidator pAttributeIdentifierValidator ) { |
18 |
|
- |
mPropertyMetaDataDefinitionAccessor = pPropertyMetaDataDefinitionAccessor; |
19 |
|
- |
mAttributeIdentifierValidator = pAttributeIdentifierValidator; |
20 |
|
- |
|
21 |
|
- |
List<String> zTypes = new ArrayList<String>(); |
22 |
|
- |
for ( AttributeSet zSet : getAttributeSets() ) { |
23 |
|
- |
zSet.addTypeOptions( zTypes ); |
24 |
|
- |
mSetsByType.put( zSet.getType().toString(), zSet ); |
25 |
|
- |
} |
26 |
|
- |
if ( zTypes.size() == 1 ) { |
27 |
|
- |
mTypesForError = ": " + zTypes.get( 0 ); |
28 |
|
- |
} else { |
29 |
|
- |
mTypesForError = " one of: " + zTypes.get( 0 ); |
30 |
|
- |
for ( int i = 1; i < zTypes.size(); i++ ) { |
31 |
|
- |
mTypesForError += ", " + zTypes.get( i ); |
32 |
|
- |
} |
33 |
|
- |
} |
34 |
|
- |
} |
35 |
|
- |
|
36 |
|
- |
@Override |
37 |
|
- |
public String getTypesForError() { |
38 |
|
- |
return mTypesForError; |
39 |
|
- |
} |
40 |
|
- |
|
41 |
|
- |
@Override |
42 |
|
- |
public AttributeSet[] getAttributeSets() { |
43 |
|
- |
return mPropertyMetaDataDefinitionAccessor.getAttributeSets(); |
44 |
|
- |
} |
45 |
|
- |
|
46 |
|
- |
@Override |
47 |
|
- |
public AttributeMetaData createAttributeMetaDataSimple( ObjectSet pObjectSet, Mode pMode, boolean pEditable, AttributeType pType, boolean pVirtual, |
48 |
|
- |
String pName ) { |
49 |
|
- |
AttributeSet zAttributeSet = mSetsByType.get( pType.toString() ); |
50 |
|
- |
ASet zASet = pVirtual ? zAttributeSet.getVirtual() : zAttributeSet.getPersisted(); |
51 |
|
- |
AttributeMetaDataSimple zAMD = |
52 |
|
- |
new AttributeMetaDataSimple( zAttributeSet, pMode, pEditable, pVirtual, adjust( pObjectSet, pType, zASet ), mAttributeIdentifierValidator, |
53 |
|
- |
pName ); |
54 |
|
- |
handleCallBack( zASet, zAMD ); |
55 |
|
- |
return zAMD; |
56 |
|
- |
} |
57 |
|
- |
|
58 |
|
- |
@Override |
59 |
|
- |
public AttributeMetaData createAttributeMetaDataID( ObjectSet pObjectSet, Mode pMode, boolean pEditable, String pName ) { |
60 |
|
- |
AttributeType zType = A_ID.TYPE; |
61 |
|
- |
AttributeSet zAttributeSet = mSetsByType.get( zType.toString() ); |
62 |
|
- |
ASet zASet = zAttributeSet.getPersisted(); |
63 |
|
- |
AttributeMetaData zAMD = |
64 |
|
- |
new AttributeMetaDataSimpleID( zAttributeSet, pMode, pEditable, adjust( pObjectSet, zType, zASet ), mAttributeIdentifierValidator, pName ); |
65 |
|
- |
handleCallBack( zASet, zAMD ); |
66 |
|
- |
return zAMD; |
67 |
|
- |
} |
68 |
|
- |
|
69 |
|
- |
@Override |
70 |
|
- |
public AttributeMetaData createAttributeMetaDataValidOptions( ObjectSet pObjectSet, Mode pMode, boolean pEditable, AttributeType pType, boolean pVirtual, |
71 |
|
- |
String pName, String... pOptions ) { |
72 |
|
- |
AttributeSet zAttributeSet = mSetsByType.get( pType.toString() ); |
73 |
|
- |
ASet zASet = pVirtual ? zAttributeSet.getVirtual() : zAttributeSet.getPersisted(); |
74 |
|
- |
AttributeMetaData zAMD = |
75 |
|
- |
new AttributeMetaDataValidOptions( zAttributeSet, pMode, pEditable, pVirtual, adjust( pObjectSet, pType, zASet ), mAttributeIdentifierValidator, |
76 |
|
- |
pName, pOptions ); |
77 |
|
- |
handleCallBack( zASet, zAMD ); |
78 |
|
- |
return zAMD; |
79 |
|
- |
} |
80 |
|
- |
|
81 |
|
- |
@Override |
82 |
|
- |
public AttributeMetaDataRelatedPersisted createAttributeMetaDataRelatedPersisted( ObjectSet pObjectSet, Mode pMode, boolean pEditable, AttributeType pType, |
83 |
|
- |
String pName ) { |
84 |
|
- |
AttributeSet zAttributeSet = mSetsByType.get( pType.toString() ); |
85 |
|
- |
ASet zASet = zAttributeSet.getPersisted(); |
86 |
|
- |
AttributeMetaDataRelatedPersisted zAMD = |
87 |
|
- |
new AttributeMetaDataRelatedPersisted( zAttributeSet, pMode, pEditable, adjust( pObjectSet, pType, zASet ), mAttributeIdentifierValidator, |
88 |
|
- |
pName ); |
89 |
|
- |
handleCallBack( zASet, zAMD ); |
90 |
|
- |
return zAMD; |
91 |
|
- |
} |
92 |
|
- |
|
93 |
|
- |
@Override |
94 |
|
- |
public AttributeMetaDataRelatedVirtual createAttributeMetaDataRelatedVirtual( ObjectSet pObjectSet, Mode pMode, boolean pEditable, AttributeType pType, |
95 |
|
- |
String pName ) { |
96 |
|
- |
AttributeSet zAttributeSet = mSetsByType.get( pType.toString() ); |
97 |
|
- |
ASet zASet = zAttributeSet.getVirtual(); |
98 |
|
- |
AttributeMetaDataRelatedVirtual zAMD = |
99 |
|
- |
new AttributeMetaDataRelatedVirtual( zAttributeSet, pMode, pEditable, adjust( pObjectSet, pType, zASet ), mAttributeIdentifierValidator, |
100 |
|
- |
pName ); |
101 |
|
- |
handleCallBack( zASet, zAMD ); |
102 |
|
- |
return zAMD; |
103 |
|
- |
} |
104 |
|
- |
|
105 |
|
- |
private void handleCallBack( ASet pASet, AttributeMetaData pAMD ) { |
106 |
|
- |
AttributeCreationCallBack zCallBack = pASet.getCreationCallBack(); |
107 |
|
- |
if ( zCallBack != null ) { |
108 |
|
- |
zCallBack.justCreated( pAMD ); |
109 |
|
- |
} |
110 |
|
- |
} |
111 |
|
- |
|
112 |
|
- |
protected PropertyMetaData[] adjust( ObjectSet pObjectSet, AttributeType pType, ASet pASet ) { |
113 |
|
- |
List<PropertyMetaData> rv = new ArrayList<PropertyMetaData>(); |
114 |
|
- |
addAll( rv, pObjectSet, pASet.getPMDs() ); |
115 |
|
- |
addAll( rv, pObjectSet, pObjectSet.getAdditionalAttributePMDs( pType ) ); |
116 |
|
- |
return rv.toArray( new PropertyMetaData[rv.size()] ); |
117 |
|
- |
} |
118 |
|
- |
|
119 |
|
- |
private void addAll( List<PropertyMetaData> pCollector, ObjectSet pObjectSet, PropertyMetaData[] pPMDs ) { |
120 |
|
- |
if ( pPMDs != null ) { |
121 |
|
- |
for ( PropertyMetaData zPMD : pPMDs ) { |
122 |
|
- |
if ( pObjectSet.acceptableAttributePMD( zPMD ) ) { |
123 |
|
- |
pCollector.add( zPMD ); |
124 |
|
- |
} |
125 |
|
- |
} |
126 |
|
- |
} |
127 |
|
- |
} |
128 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.aokeyhole.objects.factories; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.aokeyhole.objects.*; |
|
5 |
+ |
import org.litesoft.aokeyhole.objects.attributes.*; |
|
6 |
+ |
import org.litesoft.aokeyhole.objects.support.*; |
|
7 |
+ |
import org.litesoft.aokeyhole.toolkit.*; |
|
8 |
+ |
|
|
9 |
+ |
import java.util.*; |
|
10 |
+ |
|
|
11 |
+ |
public class AttributeFactoryImpl implements AttributeFactory { |
|
12 |
+ |
private PropertyMetaDataDefinitionAccessor mPropertyMetaDataDefinitionAccessor; |
|
13 |
+ |
private IdentifierValidator mAttributeIdentifierValidator; |
|
14 |
+ |
private String mTypesForError; |
|
15 |
+ |
private Map<String, AttributeSet> mSetsByType = new HashMap<String, AttributeSet>(); |
|
16 |
+ |
|
|
17 |
+ |
public AttributeFactoryImpl( PropertyMetaDataDefinitionAccessor pPropertyMetaDataDefinitionAccessor, IdentifierValidator pAttributeIdentifierValidator ) { |
|
18 |
+ |
mPropertyMetaDataDefinitionAccessor = pPropertyMetaDataDefinitionAccessor; |
|
19 |
+ |
mAttributeIdentifierValidator = pAttributeIdentifierValidator; |
|
20 |
+ |
|
|
21 |
+ |
List<String> zTypes = new ArrayList<String>(); |
|
22 |
+ |
for ( AttributeSet zSet : getAttributeSets() ) { |
|
23 |
+ |
zSet.addTypeOptions( zTypes ); |
|
24 |
+ |
mSetsByType.put( zSet.getType().toString(), zSet ); |
|
25 |
+ |
} |
|
26 |
+ |
if ( zTypes.size() == 1 ) { |
|
27 |
+ |
mTypesForError = ": " + zTypes.get( 0 ); |
|
28 |
+ |
} else { |
|
29 |
+ |
mTypesForError = " one of: " + zTypes.get( 0 ); |
|
30 |
+ |
for ( int i = 1; i < zTypes.size(); i++ ) { |
|
31 |
+ |
mTypesForError += ", " + zTypes.get( i ); |
|
32 |
+ |
} |
|
33 |
+ |
} |
|
34 |
+ |
} |
|
35 |
+ |
|
|
36 |
+ |
@Override |
|
37 |
+ |
public String getTypesForError() { |
|
38 |
+ |
return mTypesForError; |
|
39 |
+ |
} |
|
40 |
+ |
|
|
41 |
+ |
@Override |
|
42 |
+ |
public AttributeSet[] getAttributeSets() { |
|
43 |
+ |
return mPropertyMetaDataDefinitionAccessor.getAttributeSets(); |
|
44 |
+ |
} |
|
45 |
+ |
|
|
46 |
+ |
@Override |
|
47 |
+ |
public AttributeMetaData createAttributeMetaDataSimple( ObjectSet pObjectSet, Mode pMode, boolean pEditable, AttributeType pType, boolean pVirtual, |
|
48 |
+ |
String pName ) { |
|
49 |
+ |
AttributeSet zAttributeSet = mSetsByType.get( pType.toString() ); |
|
50 |
+ |
ASet zASet = pVirtual ? zAttributeSet.getVirtual() : zAttributeSet.getPersisted(); |
|
51 |
+ |
AttributeMetaDataSimple zAMD = |
|
52 |
+ |
new AttributeMetaDataSimple( zAttributeSet, pMode, pEditable, pVirtual, adjust( pObjectSet, pType, zASet ), mAttributeIdentifierValidator, |
|
53 |
+ |
pName ); |
|
54 |
+ |
handleCallBack( zASet, zAMD ); |
|
55 |
+ |
return zAMD; |
|
56 |
+ |
} |
|
57 |
+ |
|
|
58 |
+ |
@Override |
|
59 |
+ |
public AttributeMetaData createAttributeMetaDataID( ObjectSet pObjectSet, Mode pMode, boolean pEditable, String pName ) { |
|
60 |
+ |
AttributeType zType = A_ID.TYPE; |
|
61 |
+ |
AttributeSet zAttributeSet = mSetsByType.get( zType.toString() ); |
|
62 |
+ |
ASet zASet = zAttributeSet.getPersisted(); |
|
63 |
+ |
AttributeMetaData zAMD = |
|
64 |
+ |
new AttributeMetaDataSimpleID( zAttributeSet, pMode, pEditable, adjust( pObjectSet, zType, zASet ), mAttributeIdentifierValidator, pName ); |
|
65 |
+ |
handleCallBack( zASet, zAMD ); |
|
66 |
+ |
return zAMD; |
|
67 |
+ |
} |
|
68 |
+ |
|
|
69 |
+ |
@Override |
|
70 |
+ |
public AttributeMetaData createAttributeMetaDataValidOptions( ObjectSet pObjectSet, Mode pMode, boolean pEditable, AttributeType pType, boolean pVirtual, |
|
71 |
+ |
String pName, String... pOptions ) { |
|
72 |
+ |
AttributeSet zAttributeSet = mSetsByType.get( pType.toString() ); |
|
73 |
+ |
ASet zASet = pVirtual ? zAttributeSet.getVirtual() : zAttributeSet.getPersisted(); |
|
74 |
+ |
AttributeMetaData zAMD = |
|
75 |
+ |
new AttributeMetaDataValidOptions( zAttributeSet, pMode, pEditable, pVirtual, adjust( pObjectSet, pType, zASet ), mAttributeIdentifierValidator, |
|
76 |
+ |
pName, pOptions ); |
|
77 |
+ |
handleCallBack( zASet, zAMD ); |
|
78 |
+ |
return zAMD; |
|
79 |
+ |
} |
|
80 |
+ |
|
|
81 |
+ |
@Override |
|
82 |
+ |
public AttributeMetaDataRelatedPersisted createAttributeMetaDataRelatedPersisted( ObjectSet pObjectSet, Mode pMode, boolean pEditable, AttributeType pType, |
|
83 |
+ |
String pName ) { |
|
84 |
+ |
AttributeSet zAttributeSet = mSetsByType.get( pType.toString() ); |
|
85 |
+ |
ASet zASet = zAttributeSet.getPersisted(); |
|
86 |
+ |
AttributeMetaDataRelatedPersisted zAMD = |
|
87 |
+ |
new AttributeMetaDataRelatedPersisted( zAttributeSet, pMode, pEditable, adjust( pObjectSet, pType, zASet ), mAttributeIdentifierValidator, |
|
88 |
+ |
pName ); |
|
89 |
+ |
handleCallBack( zASet, zAMD ); |
|
90 |
+ |
return zAMD; |
|
91 |
+ |
} |
|
92 |
+ |
|
|
93 |
+ |
@Override |
|
94 |
+ |
public AttributeMetaDataRelatedVirtual createAttributeMetaDataRelatedVirtual( ObjectSet pObjectSet, Mode pMode, boolean pEditable, AttributeType pType, |
|
95 |
+ |
String pName ) { |
|
96 |
+ |
AttributeSet zAttributeSet = mSetsByType.get( pType.toString() ); |
|
97 |
+ |
ASet zASet = zAttributeSet.getVirtual(); |
|
98 |
+ |
AttributeMetaDataRelatedVirtual zAMD = |
|
99 |
+ |
new AttributeMetaDataRelatedVirtual( zAttributeSet, pMode, pEditable, adjust( pObjectSet, pType, zASet ), mAttributeIdentifierValidator, |
|
100 |
+ |
pName ); |
|
101 |
+ |
handleCallBack( zASet, zAMD ); |
|
102 |
+ |
return zAMD; |
|
103 |
+ |
} |
|
104 |
+ |
|
|
105 |
+ |
private void handleCallBack( ASet pASet, AttributeMetaData pAMD ) { |
|
106 |
+ |
AttributeCreationCallBack zCallBack = pASet.getCreationCallBack(); |
|
107 |
+ |
if ( zCallBack != null ) { |
|
108 |
+ |
zCallBack.justCreated( pAMD ); |
|
109 |
+ |
} |
|
110 |
+ |
} |
|
111 |
+ |
|
|
112 |
+ |
protected PropertyMetaData[] adjust( ObjectSet pObjectSet, AttributeType pType, ASet pASet ) { |
|
113 |
+ |
List<PropertyMetaData> rv = new ArrayList<PropertyMetaData>(); |
|
114 |
+ |
addAll( rv, pObjectSet, pASet.getPMDs() ); |
|
115 |
+ |
addAll( rv, pObjectSet, pObjectSet.getAdditionalAttributePMDs( pType ) ); |
|
116 |
+ |
return rv.toArray( new PropertyMetaData[rv.size()] ); |
|
117 |
+ |
} |
|
118 |
+ |
|
|
119 |
+ |
private void addAll( List<PropertyMetaData> pCollector, ObjectSet pObjectSet, PropertyMetaData[] pPMDs ) { |
|
120 |
+ |
if ( pPMDs != null ) { |
|
121 |
+ |
for ( PropertyMetaData zPMD : pPMDs ) { |
|
122 |
+ |
if ( pObjectSet.acceptableAttributePMD( zPMD ) ) { |
|
123 |
+ |
pCollector.add( zPMD ); |
|
124 |
+ |
} |
|
125 |
+ |
} |
|
126 |
+ |
} |
|
127 |
+ |
} |
|
128 |
+ |
} |