Skip to content

Commit 48232b9

Browse files
committed
Merge pull request #1284 Feature/db ignore unknown columns refactoring
2 parents c29b0e8 + eb2bc36 commit 48232b9

File tree

6 files changed

+152
-234
lines changed

6 files changed

+152
-234
lines changed

modules/ImportAPI/src/main/java/org/gephi/io/importer/api/PropertiesAssociations.java

Lines changed: 35 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -38,121 +38,69 @@ Development and Distribution License("CDDL") (collectively, the
3838
Contributor(s):
3939
4040
Portions Copyrighted 2011 Gephi Consortium.
41-
*/
41+
*/
4242
package org.gephi.io.importer.api;
4343

4444
import java.io.Serializable;
45-
import java.util.LinkedList;
46-
import java.util.List;
45+
import java.util.HashMap;
46+
import java.util.Map;
4747

48-
/**
49-
*
50-
* @author Mathieu Bastian
51-
*/
5248
public final class PropertiesAssociations implements Serializable {
5349

5450
public enum NodeProperties {
55-
56-
X, Y, Z, R, G, B, COLOR, SIZE, ID, LABEL, FIXED, START, END, START_OPEN, END_OPEN
51+
X, Y, Z, R, G, B, COLOR, SIZE, ID, LABEL, FIXED, START, END, START_OPEN, END_OPEN;
5752
}
5853

5954
public enum EdgeProperties {
60-
61-
R, G, B, COLOR, WEIGHT, ID, LABEL, ALPHA, SOURCE, TARGET, START, END, START_OPEN, END_OPEN
55+
R, G, B, COLOR, WEIGHT, ID, LABEL, ALPHA, SOURCE, TARGET, START, END, START_OPEN, END_OPEN;
6256
}
63-
//PropertiesAssociations association
64-
private final List<PropertyAssociation<NodeProperties>> nodePropertyAssociations = new LinkedList<>();
65-
private final List<PropertyAssociation<EdgeProperties>> edgePropertyAssociations = new LinkedList<>();
6657

67-
public void addEdgePropertyAssociation(EdgeProperties property, String title) {
68-
PropertyAssociation<EdgeProperties> association = new PropertyAssociation<>(property, title);
69-
/*if (edgePropertyAssociations.contains(association)) {
70-
return;
71-
}
72-
//Avoid any double
73-
for (Iterator<PropertyAssociation<EdgeProperties>> itr = edgePropertyAssociations.iterator(); itr.hasNext();) {
74-
PropertyAssociation<EdgeProperties> p = itr.next();
75-
if (p.getTitle().equalsIgnoreCase(association.getTitle())) {
76-
itr.remove();
77-
} else if (p.getProperty().equals(association.getProperty())) {
78-
itr.remove();
79-
}
80-
}*/
81-
edgePropertyAssociations.add(association);
82-
}
58+
private final Map<String, NodeProperties> titleToNodeProperty = new HashMap<>();
59+
private final Map<String, EdgeProperties> titleToEdgeProperty = new HashMap<>();
8360

8461
public void addNodePropertyAssociation(NodeProperties property, String title) {
85-
PropertyAssociation<NodeProperties> association = new PropertyAssociation<>(property, title);
86-
/*if (nodePropertyAssociations.contains(association)) {
87-
return;
88-
}
89-
//Avoid any double
90-
for (Iterator<PropertyAssociation<NodeProperties>> itr = nodePropertyAssociations.iterator(); itr.hasNext();) {
91-
PropertyAssociation<NodeProperties> p = itr.next();
92-
if (p.getTitle().equalsIgnoreCase(association.getTitle())) {
93-
itr.remove();
94-
} else if (p.getProperty().equals(association.getProperty())) {
95-
itr.remove();
96-
}
97-
}*/
98-
nodePropertyAssociations.add(association);
62+
titleToNodeProperty.put(title, property);
9963
}
10064

101-
PropertyAssociation<EdgeProperties>[] getEdgePropertiesAssociation() {
102-
return edgePropertyAssociations.toArray(new PropertyAssociation[0]);
103-
}
104-
105-
PropertyAssociation<NodeProperties>[] getNodePropertiesAssociation() {
106-
return nodePropertyAssociations.toArray(new PropertyAssociation[0]);
65+
public void addEdgePropertyAssociation(EdgeProperties property, String title) {
66+
titleToEdgeProperty.put(title, property);
10767
}
10868

10969
public NodeProperties getNodeProperty(String title) {
110-
for (PropertyAssociation<NodeProperties> p : nodePropertyAssociations) {
111-
if (p.getTitle().equalsIgnoreCase(title)) {
112-
return p.getProperty();
113-
}
70+
if (titleToNodeProperty.containsKey(title)) {
71+
return titleToNodeProperty.get(title);
72+
} else {
73+
return null;
11474
}
115-
return null;
11675
}
11776

11877
public EdgeProperties getEdgeProperty(String title) {
119-
for (PropertyAssociation<EdgeProperties> p : edgePropertyAssociations) {
120-
if (p.getTitle().equalsIgnoreCase(title)) {
121-
return p.getProperty();
122-
}
123-
}
124-
return null;
125-
}
126-
127-
public String getNodePropertyTitle(NodeProperties property) {
128-
for (PropertyAssociation<NodeProperties> p : nodePropertyAssociations) {
129-
if (p.getProperty().equals(property)) {
130-
return p.getTitle();
131-
}
132-
}
133-
return null;
134-
}
135-
136-
public String getEdgePropertyTitle(EdgeProperties property) {
137-
for (PropertyAssociation<EdgeProperties> p : edgePropertyAssociations) {
138-
if (p.getProperty().equals(property)) {
139-
return p.getTitle();
140-
}
78+
if (titleToEdgeProperty.containsKey(title)) {
79+
return titleToEdgeProperty.get(title);
80+
} else {
81+
return null;
14182
}
142-
return null;
14383
}
14484

14585
public String getInfos() {
146-
String res = "***Node Properties Associations***\n";
147-
for (PropertyAssociation<NodeProperties> p : nodePropertyAssociations) {
148-
res += "Property " + p.getProperty().toString() + " = " + p.getTitle() + " Column\n";
86+
StringBuilder builder = new StringBuilder("***Node Properties Associations***\n");
87+
for (Map.Entry<String, NodeProperties> entry : titleToNodeProperty.entrySet()) {
88+
builder.append("Property ")
89+
.append(entry.getValue().name())
90+
.append(" = ")
91+
.append(entry.getKey())
92+
.append(" Column\n");
14993
}
150-
res += "*********************************\n";
151-
res = "***Edge Properties Associations***\n";
152-
for (PropertyAssociation<EdgeProperties> p : edgePropertyAssociations) {
153-
res += "Property " + p.getProperty().toString() + " = " + p.getTitle() + " Column\n";
94+
builder.append("*********************************\n");
95+
builder.append("***Edge Properties Associations***\n");
96+
for (Map.Entry<String, EdgeProperties> entry : titleToEdgeProperty.entrySet()) {
97+
builder.append("Property ")
98+
.append(entry.getValue().name())
99+
.append(" = ")
100+
.append(entry.getKey())
101+
.append(" Column\n");
154102
}
155-
res += "*********************************\n";
156-
return res;
103+
builder.append("*********************************\n");
104+
return builder.toString();
157105
}
158106
}

modules/ImportAPI/src/main/java/org/gephi/io/importer/api/PropertyAssociation.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

modules/ImportAPI/src/main/java/org/gephi/io/importer/impl/ElementFactoryImpl.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ Development and Distribution License("CDDL") (collectively, the
4141
*/
4242
package org.gephi.io.importer.impl;
4343

44-
import java.util.concurrent.atomic.AtomicInteger;
4544
import org.gephi.io.importer.api.ElementDraft;
4645
import org.gephi.io.importer.api.Issue;
4746
import org.openide.util.NbBundle;
4847

48+
import java.util.concurrent.atomic.AtomicInteger;
49+
4950
public class ElementFactoryImpl implements ElementDraft.Factory {
5051

5152
protected final ImportContainerImpl container;
@@ -58,8 +59,7 @@ public ElementFactoryImpl(ImportContainerImpl container) {
5859

5960
@Override
6061
public NodeDraftImpl newNodeDraft() {
61-
NodeDraftImpl node = new NodeDraftImpl(container, String.valueOf(NODE_IDS.getAndIncrement()));
62-
return node;
62+
return new NodeDraftImpl(container, String.valueOf(NODE_IDS.getAndIncrement()));
6363
}
6464

6565
@Override
@@ -68,14 +68,12 @@ public NodeDraftImpl newNodeDraft(String id) {
6868
String message = NbBundle.getMessage(ElementFactoryImpl.class, "ElementFactoryException_NullNodeId");
6969
container.getReport().logIssue(new Issue(message, Issue.Level.CRITICAL));
7070
}
71-
NodeDraftImpl node = new NodeDraftImpl(container, id);
72-
return node;
71+
return new NodeDraftImpl(container, id);
7372
}
7473

7574
@Override
7675
public EdgeDraftImpl newEdgeDraft() {
77-
EdgeDraftImpl edge = new EdgeDraftImpl(container, String.valueOf(EDGE_IDS.getAndIncrement()));
78-
return edge;
76+
return new EdgeDraftImpl(container, String.valueOf(EDGE_IDS.getAndIncrement()));
7977
}
8078

8179
@Override
@@ -84,7 +82,6 @@ public EdgeDraftImpl newEdgeDraft(String id) {
8482
String message = NbBundle.getMessage(ElementFactoryImpl.class, "ElementFactoryException_NullEdgeId");
8583
container.getReport().logIssue(new Issue(message, Issue.Level.CRITICAL));
8684
}
87-
EdgeDraftImpl edge = new EdgeDraftImpl(container, id);
88-
return edge;
85+
return new EdgeDraftImpl(container, id);
8986
}
9087
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.gephi.io.importer.plugin.database;
2+
3+
import org.gephi.io.importer.api.EdgeDraft;
4+
import org.gephi.io.importer.api.ElementDraft;
5+
import org.gephi.io.importer.api.NodeDraft;
6+
import org.gephi.io.importer.api.PropertiesAssociations;
7+
8+
import java.sql.ResultSet;
9+
import java.sql.ResultSetMetaData;
10+
import java.sql.SQLException;
11+
12+
class EdgeColumns {
13+
14+
int findIdIndex(final ResultSetMetaData metaData, final PropertiesAssociations properties) throws SQLException {
15+
int result = -1;
16+
for (int i = 1; i <= metaData.getColumnCount(); ++i) {
17+
String columnLabel = metaData.getColumnLabel(i);
18+
PropertiesAssociations.EdgeProperties p = properties.getEdgeProperty(columnLabel);
19+
if (PropertiesAssociations.EdgeProperties.ID.equals(p)) {
20+
result = i;
21+
break;
22+
}
23+
}
24+
return result;
25+
}
26+
27+
EdgeDraft getEdgeDraft(final ElementDraft.Factory factory, final ResultSet rs, final int idColumn) throws SQLException {
28+
String id = getIdValue(rs, idColumn);
29+
30+
final EdgeDraft edge;
31+
if (id == null) {
32+
edge = factory.newEdgeDraft();
33+
} else {
34+
edge = factory.newEdgeDraft(id);
35+
}
36+
return edge;
37+
}
38+
39+
private String getIdValue(final ResultSet rs, final int idColumn) throws SQLException {
40+
if (idColumn == -1) return null;
41+
42+
return rs.getString(idColumn);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)