Skip to content

Commit dd10de2

Browse files
committed
Fix attribute delegate providers type mismatch mechanism. (Issue gephi#707)
1 parent 930a4d3 commit dd10de2

File tree

2 files changed

+134
-120
lines changed

2 files changed

+134
-120
lines changed

modules/AttributesImpl/src/main/java/org/gephi/data/attributes/AttributeFactoryImpl.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,19 @@ public AttributeValue newValue(AttributeColumn column, Object value) {
6868
if (value == null) {
6969
return new AttributeValueImpl((AttributeColumnImpl) column, null);
7070
}
71-
72-
AttributeType targetType = column.getType();
73-
if (!value.getClass().equals(targetType.getType())) {
74-
try {
75-
value = targetType.parse(value.toString());//Try to convert to target type
76-
} catch (Exception ex) {
77-
return new AttributeValueImpl((AttributeColumnImpl) column, null);//Could not parse
71+
72+
//If the column is not a delegate (wrong type value allowed), try to convert value to correct type if necessary
73+
if(!column.getOrigin().equals(AttributeOrigin.DELEGATE)){
74+
AttributeType targetType = column.getType();
75+
if (!value.getClass().equals(targetType.getType())) {
76+
try {
77+
value = targetType.parse(value.toString());//Try to convert to target type
78+
} catch (Exception ex) {
79+
return new AttributeValueImpl((AttributeColumnImpl) column, null);//Could not parse
80+
}
7881
}
7982
}
83+
8084

8185
Object managedValue = value;
8286
if (!column.getOrigin().equals(AttributeOrigin.PROPERTY)) {
Lines changed: 123 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,123 @@
1-
/*
2-
Copyright 2008-2010 Gephi
3-
Authors : Mathieu Bastian <[email protected]>, Martin Škurla <[email protected]>
4-
Website : http://www.gephi.org
5-
6-
This file is part of Gephi.
7-
8-
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
9-
10-
Copyright 2011 Gephi Consortium. All rights reserved.
11-
12-
The contents of this file are subject to the terms of either the GNU
13-
General Public License Version 3 only ("GPL") or the Common
14-
Development and Distribution License("CDDL") (collectively, the
15-
"License"). You may not use this file except in compliance with the
16-
License. You can obtain a copy of the License at
17-
http://gephi.org/about/legal/license-notice/
18-
or /cddl-1.0.txt and /gpl-3.0.txt. See the License for the
19-
specific language governing permissions and limitations under the
20-
License. When distributing the software, include this License Header
21-
Notice in each file and include the License files at
22-
/cddl-1.0.txt and /gpl-3.0.txt. If applicable, add the following below the
23-
License Header, with the fields enclosed by brackets [] replaced by
24-
your own identifying information:
25-
"Portions Copyrighted [year] [name of copyright owner]"
26-
27-
If you wish your version of this file to be governed by only the CDDL
28-
or only the GPL Version 3, indicate your decision by adding
29-
"[Contributor] elects to include this software in this distribution
30-
under the [CDDL or GPL Version 3] license." If you do not indicate a
31-
single choice of license, a recipient has the option to distribute
32-
your version of this file under either the CDDL, the GPL Version 3 or
33-
to extend the choice of license to its licensees as provided above.
34-
However, if you add GPL Version 3 code and therefore, elected the GPL
35-
Version 3 license, then the option applies only if the new code is
36-
made subject to such option by the copyright holder.
37-
38-
Contributor(s):
39-
40-
Portions Copyrighted 2011 Gephi Consortium.
41-
*/
42-
package org.gephi.data.attributes;
43-
44-
import org.gephi.data.attributes.api.AttributeOrigin;
45-
import org.gephi.data.attributes.api.AttributeValue;
46-
import org.gephi.data.attributes.spi.AttributeValueDelegateProvider;
47-
48-
/**
49-
*
50-
* @author Mathieu Bastian
51-
* @author Martin Škurla
52-
*/
53-
public final class AttributeValueImpl implements AttributeValue {
54-
55-
private final AttributeColumnImpl column;
56-
private final Object value;
57-
58-
public AttributeValueImpl(AttributeColumnImpl column, Object value) {
59-
this.column = column;
60-
this.value = value;
61-
}
62-
63-
public AttributeColumnImpl getColumn() {
64-
return column;
65-
}
66-
67-
public Object getValue() {
68-
if (column.getOrigin() != AttributeOrigin.DELEGATE) {
69-
return value;
70-
}
71-
else {
72-
if (value == null)
73-
return null;
74-
75-
AttributeValueDelegateProvider attributeValueDelegateProvider = column.getProvider();
76-
77-
Object result;
78-
if (AttributeUtilsImpl.getDefault().isEdgeColumn(column))
79-
result = attributeValueDelegateProvider.getEdgeAttributeValue(value, column);
80-
else if (AttributeUtilsImpl.getDefault().isNodeColumn(column))
81-
result = attributeValueDelegateProvider.getNodeAttributeValue(value, column);
82-
else
83-
throw new AssertionError();
84-
85-
// important for Neo4j and in future also for other storing engines
86-
// the conversion can be necessary because of types mismatch
87-
// for Neo4j return type can be array of primitive type which must be
88-
// converted into List type
89-
if (result.getClass().isArray())
90-
result = ListFactory.fromArray(result);
91-
92-
return result;
93-
}
94-
}
95-
96-
@Override
97-
public boolean equals(Object obj) {
98-
if (obj != null && obj instanceof AttributeValue) {
99-
if (this == obj) {
100-
return true;
101-
}
102-
Object thisVal = this.getValue();
103-
Object objVal = ((AttributeValue) obj).getValue();
104-
if (thisVal == null && objVal == null) {
105-
return true;
106-
}
107-
if (thisVal != null && objVal != null && thisVal.equals(objVal)) {
108-
return true;
109-
}
110-
}
111-
return false;
112-
}
113-
}
1+
/*
2+
Copyright 2008-2010 Gephi
3+
Authors : Mathieu Bastian <[email protected]>, Martin Škurla <[email protected]>
4+
Website : http://www.gephi.org
5+
6+
This file is part of Gephi.
7+
8+
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
9+
10+
Copyright 2011 Gephi Consortium. All rights reserved.
11+
12+
The contents of this file are subject to the terms of either the GNU
13+
General Public License Version 3 only ("GPL") or the Common
14+
Development and Distribution License("CDDL") (collectively, the
15+
"License"). You may not use this file except in compliance with the
16+
License. You can obtain a copy of the License at
17+
http://gephi.org/about/legal/license-notice/
18+
or /cddl-1.0.txt and /gpl-3.0.txt. See the License for the
19+
specific language governing permissions and limitations under the
20+
License. When distributing the software, include this License Header
21+
Notice in each file and include the License files at
22+
/cddl-1.0.txt and /gpl-3.0.txt. If applicable, add the following below the
23+
License Header, with the fields enclosed by brackets [] replaced by
24+
your own identifying information:
25+
"Portions Copyrighted [year] [name of copyright owner]"
26+
27+
If you wish your version of this file to be governed by only the CDDL
28+
or only the GPL Version 3, indicate your decision by adding
29+
"[Contributor] elects to include this software in this distribution
30+
under the [CDDL or GPL Version 3] license." If you do not indicate a
31+
single choice of license, a recipient has the option to distribute
32+
your version of this file under either the CDDL, the GPL Version 3 or
33+
to extend the choice of license to its licensees as provided above.
34+
However, if you add GPL Version 3 code and therefore, elected the GPL
35+
Version 3 license, then the option applies only if the new code is
36+
made subject to such option by the copyright holder.
37+
38+
Contributor(s):
39+
40+
Portions Copyrighted 2011 Gephi Consortium.
41+
*/
42+
package org.gephi.data.attributes;
43+
44+
import org.gephi.data.attributes.api.AttributeOrigin;
45+
import org.gephi.data.attributes.api.AttributeValue;
46+
import org.gephi.data.attributes.spi.AttributeValueDelegateProvider;
47+
48+
/**
49+
*
50+
* @author Mathieu Bastian
51+
* @author Martin Škurla
52+
*/
53+
public final class AttributeValueImpl implements AttributeValue {
54+
55+
private final AttributeColumnImpl column;
56+
private final Object value;
57+
58+
public AttributeValueImpl(AttributeColumnImpl column, Object value) {
59+
this.column = column;
60+
this.value = value;
61+
}
62+
63+
public AttributeColumnImpl getColumn() {
64+
return column;
65+
}
66+
67+
public Object getValue() {
68+
if (column.getOrigin() != AttributeOrigin.DELEGATE) {
69+
return value;
70+
} else {
71+
if (value == null) {
72+
return null;
73+
}
74+
75+
AttributeValueDelegateProvider attributeValueDelegateProvider = column.getProvider();
76+
77+
Object result;
78+
if (AttributeUtilsImpl.getDefault().isEdgeColumn(column)) {
79+
result = attributeValueDelegateProvider.getEdgeAttributeValue(value, column);
80+
} else if (AttributeUtilsImpl.getDefault().isNodeColumn(column)) {
81+
result = attributeValueDelegateProvider.getNodeAttributeValue(value, column);
82+
} else {
83+
throw new AssertionError();
84+
}
85+
86+
if(result != null && result.getClass() != column.getType().getType()){
87+
//Try to parse to correct column type if the delegate provides a wrong type value:
88+
Object convertedValue = column.getType().parse(value.toString());
89+
if(convertedValue != null){
90+
result = convertedValue;
91+
}
92+
}
93+
94+
// important for Neo4j and in future also for other storing engines
95+
// the conversion can be necessary because of types mismatch
96+
// for Neo4j return type can be array of primitive type which must be
97+
// converted into List type
98+
if (result != null && result.getClass().isArray()) {
99+
result = ListFactory.fromArray(result);
100+
}
101+
102+
return result;
103+
}
104+
}
105+
106+
@Override
107+
public boolean equals(Object obj) {
108+
if (obj != null && obj instanceof AttributeValue) {
109+
if (this == obj) {
110+
return true;
111+
}
112+
Object thisVal = this.getValue();
113+
Object objVal = ((AttributeValue) obj).getValue();
114+
if (thisVal == null && objVal == null) {
115+
return true;
116+
}
117+
if (thisVal != null && objVal != null && thisVal.equals(objVal)) {
118+
return true;
119+
}
120+
}
121+
return false;
122+
}
123+
}

0 commit comments

Comments
 (0)