Project

General

Profile

« Previous | Next » 

Revision 34760

mavenizing refactoring project

View differences:

modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/BibTeXGenerator.java
1
package gr.uoa.di.web.utils;
2

  
3
import eu.dnetlib.domain.data.Document;
4
import eu.dnetlib.domain.enabling.Vocabulary;
5
import eu.dnetlib.domain.functionality.DocumentField;
6
import eu.dnetlib.domain.functionality.SwitchDocumentField;
7
import gr.uoa.di.web.utils.bibtex.BibTeX;
8

  
9
import java.util.List;
10
import java.util.Locale;
11

  
12
import org.apache.log4j.Logger;
13

  
14
public class BibTeXGenerator {
15

  
16
	private static Logger logger = Logger.getLogger(BibTeXGenerator.class);
17
	
18
	public static BibTeX generateBibTeX(List<DocumentField> fields, Document document, Vocabulary bibTeXVocabulary) {
19
		BibTeX bibTeX = new BibTeX();
20
		
21
		//Create entry
22
		List<String> entryValues = document.getMap().get("CobjCategory");
23
		if (entryValues != null && !entryValues.isEmpty()) {
24
			String entry = bibTeXVocabulary.getEnglishName(entryValues.get(0));
25
			if (entry == null) { bibTeX.setEntry("article"); } 
26
			else { bibTeX.setEntry(entry); }
27
			
28
		} else {
29
			bibTeX.setEntry("article");
30
		}
31
		
32
		bibTeX.setKey(generateBibTeXKey(fields, document));
33
		
34
		for (DocumentField field: fields) {			
35
			if (field instanceof SwitchDocumentField) {
36
				String conditionField = ((SwitchDocumentField) field).getConditionField();				
37
				String conditionFieldValue = null;
38
				
39
				/* if there is no value in the document for the condition field, consider it 'default' */
40
				if ( document.getMap().get(conditionField) == null || document.getMap().get(conditionField).isEmpty() ) {
41
					conditionFieldValue = "default";
42
					
43
				} else { //else get the one from the document
44
					conditionFieldValue = document.getMap().get(conditionField).get(0);
45
				}
46
							
47
				/* in case where there is no field for the given conditionFieldValue 
48
				 * because this was not part of the configuration use "default". If 
49
				 * there is no field for "default" just continue   */
50
				
51
				if (((SwitchDocumentField) field).getDocumentField(conditionFieldValue) != null ) {
52
					field = ((SwitchDocumentField) field).getDocumentField(conditionFieldValue);
53
					
54
				} else if (((SwitchDocumentField) field).getDocumentField("default") != null) {
55
					field =  ((SwitchDocumentField) field).getDocumentField("default");
56
					 
57
				} else {
58
					continue;
59
				}
60
		}
61
			
62
			String fieldDescription = LocaleDescriptionUtil.getFieldDescription(field.getDescriptionMap(), Locale.ENGLISH);
63
			String value = generate(field.getName(), document);
64
			if (!value.isEmpty() && !value.equals("\"\"")) {
65
				bibTeX.getFields().put(fieldDescription, value);
66
			} 
67
		}
68
		
69
		return bibTeX;
70
	}
71
	
72
	public static String generate(String indexField, Document document) {
73
		List<String> values = document.getFieldValues(indexField);
74
		
75
		if (values == null) return "";
76
				
77
		StringBuffer buffer = new StringBuffer();
78
		buffer.append("\"");
79
		int i = 0;
80
		
81
		for (String value:values) {
82
			if ( i > 0 ) { buffer.append(" and "); }
83
			buffer.append(escapeCharacters(value));
84
			i++;
85
		}
86
		
87
		buffer.append("\"");
88
		
89
		return buffer.toString();
90
	}
91
	
92
	public static String escapeCharacters(String value) {
93
		return value.replace("\"", "\\\"").replace("{", "\\{").replace("}", "\\}").replace("$", "\\$");
94
	}
95

  
96
	public static String generateBibTeXKey(List<DocumentField> fields, Document document) {
97
		
98
		String index4author = null;
99
		//Get the index field for author BibTeX field
100
		for ( DocumentField field:fields ) {
101
			if(field.getDescriptionMap().get(new Locale("en", "GB")) != null) {
102
				if (field.getDescriptionMap().get(new Locale("en", "GB")).equals("author")) {
103
					index4author = field.getName();
104
					break;
105
				}
106
			}
107
		}
108
		
109
		StringBuffer buffer = new StringBuffer();
110

  
111
		if (index4author != null) {
112
			List<String> authornames = document.getFieldValues(index4author);
113
		
114
			if (authornames != null && !authornames.isEmpty()) {
115
				for (String fullname: authornames) {
116
					String parts[] = fullname.split(",");
117
					String part = parts[0].replace(" ", "");
118
					if (authornames.size() > 1){
119
						if(part.length() > 4){
120
							buffer.append(part.substring(0, 4));
121
						} else {
122
							buffer.append(part);
123
						}
124
					} else {
125
						buffer.append(part);
126
					}
127
				}
128
			}	
129
		}
130
	
131
		//TODO: reconsider following the same procedure as for author to get the index field name
132
		if (document.getMap().get("publicationyear") != null && !document.getMap().get("publicationyear").isEmpty()) {
133
			String publicationYear = document.getMap().get("publicationyear").get(0);
134
			if (publicationYear.length() > 3) {
135
				buffer.append(publicationYear.substring(publicationYear.length()-2, publicationYear.length()));
136
			} else {
137
				buffer.append(publicationYear);
138
			}					
139
		}
140
		
141
		return buffer.toString();			
142
		
143
	}
144
	
145
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/entity/Entity.java
1
package gr.uoa.di.web.utils.ep.entity;
2

  
3
import java.util.Set;
4

  
5
public class Entity {	
6
	private String name;
7
	private Set<Field> fields;
8
	private String idField;
9
	private String labelField;
10
	private String label;
11
	private String color;
12
	private Shape shape;
13
	private boolean internal;
14
	private String typeField;
15
	private String type;
16
	
17
	public Entity(String name, Set<Field> fields, String idField, String labelField, String label, String color, Shape shape) {
18
		this.name = name;
19
		this.fields = fields;
20
		this.idField = idField;
21
		this.labelField = labelField;
22
		this.label = label;
23
		this.color = color;
24
		this.shape = shape;
25
		internal = true;
26
		typeField = null;
27
		type = null;
28
	}
29
	
30
	public Entity(String name, Set<Field> fields, String idField, String labelField, String label, String color, Shape shape, String typeField, String type) {
31
		this.name = name;
32
		this.fields = fields;
33
		this.idField = idField;
34
		this.labelField = labelField;
35
		this.label = label;
36
		this.color = color;
37
		this.shape = shape;
38
		this.typeField = typeField;
39
		this.type = type;		
40
		internal = false;
41
	}
42
		
43
	public String getName() {
44
		return name;
45
	}
46
	
47
	public Set<Field> getFields() {
48
		return fields;
49
	}
50
	
51
	public String getIdField() {
52
		return idField;
53
	}
54
	
55
	public String getLabelField() {
56
		return labelField;
57
	}
58

  
59
	public String getLabel() {
60
		return label;
61
	}
62
	
63
	public String getColor() {
64
		return color;
65
	}
66
	
67
	public Shape getShape() {
68
		return shape;
69
	}
70
		
71
	public boolean getInternal() {
72
		return internal;
73
	}
74
	
75
	public String getTypeField() {
76
		return typeField;
77
	}
78
	
79
	public String getType() {
80
		return type;
81
	}
82
	
83
	@Override
84
	public int hashCode() {
85
		return name.hashCode();
86
	}
87
	
88
	@Override
89
	public boolean equals(Object object) {
90
		if (object == this)
91
			return true;
92
		if (!(object instanceof Entity))
93
			return false;
94
		Entity entity = (Entity) object;
95
		return name.equals(entity.getName());
96
	}
97
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/entity/Shape.java
1
package gr.uoa.di.web.utils.ep.entity;
2

  
3
public enum Shape {
4
	BLOCK("block"),
5
	CIRCLE("circle"),
6
	DIAMOND("diamond"),
7
	SQUARE("square"),
8
	TRIANGLE_DOWN("triangleDown"),
9
	TRIANGLE_LEFT("triangleLeft"),
10
	TRIANGLE_RIGHT("triangleRight"),
11
	TRIANGLE_UP("triangleUp");
12
	
13
	private final String shape;
14
	
15
	private Shape(String shape) {
16
		this.shape = shape;
17
	}
18
	
19
	public static Shape parseShape(String shape) {
20
		for (Shape s : Shape.class.getEnumConstants()) {
21
			if (shape.equals(s.shape))
22
				return s;
23
		}
24
		return null;
25
	}
26

  
27
	@Override
28
	public String toString() {
29
		return shape;
30
	}
31
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/entity/Field.java
1
package gr.uoa.di.web.utils.ep.entity;
2

  
3
public class Field {
4
	private String name;
5
	private String label;
6
	private boolean collection;
7
	private boolean editable;
8
	private String defaultValue;
9
	
10
	public Field(String name, String label, boolean collection, boolean editable, String defaultValue) {
11
		this.name = name;
12
		this.label = label;
13
		this.collection = collection;
14
		this.editable = editable;
15
		this.defaultValue = defaultValue;
16
	}
17
	
18
	public String getName() {
19
		return name;
20
	}
21
	
22
	public String getLabel() {
23
		return label;
24
	}
25
	
26
	public boolean getCollection() {
27
		return collection;
28
	}
29
	
30
	public boolean getEditable() {
31
		return editable;
32
	}
33
	
34
	public String getDefaultValue() {
35
		return defaultValue;
36
	}
37
	
38
	public String toString() {
39
		return "field " + label;
40
	}
41
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/relation/Relation.java
1
package gr.uoa.di.web.utils.ep.relation;
2

  
3
import java.util.Set;
4

  
5
public class Relation {
6
	private String name;
7
	private Set<String> sourceEntities;
8
	private Set<String> targetEntities;
9
	private Cardinality cardinality;
10
	private Partiality partiality;
11
	private String label;
12
	private int width;
13
	private String color;
14
	
15
	public Relation(String name, Set<String> sourceEntities, Set<String> targetEntities, Cardinality cardinality,
16
			Partiality partiality, String label, int width, String color) {
17
		this.name = name;
18
		this.sourceEntities = sourceEntities;
19
		this.targetEntities = targetEntities;
20
		this.cardinality = cardinality;
21
		this.partiality = partiality;
22
		this.label = label;
23
		this.width = width;
24
		this.color = color;
25
	}
26
	
27
	public String getName() {
28
		return name;
29
	}
30
	
31
	public Set<String> getSourceEntities() {
32
		return sourceEntities;
33
	}
34
	
35
	public Set<String> getTargetEntities() {
36
		return targetEntities;
37
	}
38
	
39
	public Cardinality getCardinality() {
40
		return cardinality;
41
	}
42
	
43
	public Partiality getPartiality() {
44
		return partiality;
45
	}
46
	
47
	public String getLabel() {
48
		return label;
49
	}
50
	
51
	public int getWidth() {
52
		return width;
53
	}
54
	
55
	public String getColor() {
56
		return color;
57
	}
58
	
59
	@Override
60
	public int hashCode() {
61
		return name.hashCode();
62
	}
63
	
64
	@Override
65
	public boolean equals(Object object) {
66
		if (object == this)
67
			return true;
68
		if (!(object instanceof Relation))
69
			return false;
70
		Relation relation = (Relation) object;
71
		return name.equals(relation.getName());
72
	}
73
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/relation/Partiality.java
1
package gr.uoa.di.web.utils.ep.relation;
2

  
3
public enum Partiality {
4
	PARTIAL_TO_PARTIAL("part_part"),
5
	PARTIAL_TO_TOTAL("part_tot"),
6
	TOTAL_TO_PARTIAL("tot_part"),
7
	TOTAL_TO_TOTAL("tot_tot");
8
	
9
	private final String partiality;
10
	
11
	private Partiality(String partiality) {
12
		this.partiality = partiality;
13
	}
14
	
15
	public static Partiality parsePartiality(String partiality) {
16
		for (Partiality p : Partiality.class.getEnumConstants()) {
17
			if (partiality.equals(p.partiality))
18
				return p;
19
		}
20
		return null;
21
	}
22
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/relation/Cardinality.java
1
package gr.uoa.di.web.utils.ep.relation;
2

  
3
public enum Cardinality {		
4
	ONE_TO_ONE("one_to_one"),
5
	ONE_TO_MANY("one_to_many"),
6
	MANY_TO_ONE("many_to_one"),
7
	MANY_TO_MANY("many_to_many");
8
	
9
	private final String cardinality;
10
	
11
	private Cardinality(String cardinality) {
12
		this.cardinality = cardinality;
13
	}
14

  
15
	public static Cardinality parseCardinality(String cardinality) {
16
		for (Cardinality c : Cardinality.class.getEnumConstants()) {
17
			if (cardinality.equals(c.cardinality))
18
				return c;
19
		}
20
		return null;
21
	}
22
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/EPManager.java
1
package gr.uoa.di.web.utils.ep;
2

  
3
import eu.dnetlib.domain.data.Document;
4
import eu.dnetlib.domain.functionality.UserProfile;
5
import gr.uoa.di.web.utils.ep.domain.Entity;
6
import gr.uoa.di.web.utils.ep.domain.Graph;
7
import gr.uoa.di.web.utils.ep.domain.Node;
8
import gr.uoa.di.web.utils.ep.domain.RelatedNode;
9
import gr.uoa.di.web.utils.ep.domain.Relation;
10
import gr.uoa.di.web.utils.search.DocumentPage;
11

  
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Set;
15

  
16
/**
17
 * This interface describes a manager for enhanced publications.
18
 * @author thanos
19
 *
20
 */
21
public interface EPManager {
22
	/**
23
	 * Get the available entities.
24
	 * @return a set containing the available entities
25
	 * @throws EPManagerException if any errors occur
26
	 */
27
	public Set<Entity> getEntities() throws EPManagerException;
28
	
29
	/**
30
	 * Get the available relations.
31
	 * @return a set containing the available relations
32
	 * @throws EPManagerException if any errors occur
33
	 */
34
	public Set<Relation> getRelations() throws EPManagerException;
35
	
36
	/**
37
	 * Get the dropbox contents of a user
38
	 * @param userProfile the user to get dropbox contents of
39
	 * @return a set containing the documents of the user
40
	 * @throws EPManagerException if any errors occur
41
	 */
42
	public Set<Map<String, List<String>>> getDropboxContents(UserProfile userProfile) throws EPManagerException;
43
	
44
	/**
45
	 * Get a graph by its root node id.
46
	 * @param rootId the id of the root node
47
	 * @return a graph containing all the ancestors of the root node, its immediate parents and the edges connecting them or null if no such root node exists
48
	 * @throws EPManagerException if any errors occur
49
	 */
50
	public Graph getGraph(String rootId) throws EPManagerException;
51
	
52
	/**
53
	 * Save a graph.
54
	 * @param userProfile the user profile of the user attempting to save the graph
55
	 * @param rootId the id of the root node  
56
	 * @param commands the commands to execute in order to save the graph
57
	 * @return the (possibly) new id of the root node
58
	 * @throws EPManagerException if any errors occur
59
	 */
60
	public void saveGraph(UserProfile userProfile, String [] commands) throws EPManagerException;
61
	
62
	/**
63
	 * Save an enhanced publication.
64
	 * @param userProfile the user profile of the user that requests the creation
65
	 * @param creator the creator of the enhanced publication (if null or empty will be the aforementioned user)
66
	 * @param description the description of the enhanced publication
67
	 * @param language the language of the enhanced publication
68
	 * @param subject the subject of the enhanced publication
69
	 * @param title the title of the enhanced publication
70
	 * @param ePrintId the id of the e-Print of the enhanced publication
71
	 * @param nonEPrintIds the ids of the other contents (non-e-Prints) of the enhanced publication
72
	 * @return the id of the created enhanced publication
73
	 * @throws EPManagerException if any errors occur
74
	 */
75
	public String saveEnhancedPublication(UserProfile userProfile, String description, String language, String subject, String title, String ePrintId, Set<String> nonEPrintIds) throws EPManagerException;
76
	
77
	/**
78
	 * Save an aggregation object.
79
	 * @param userProfile the user profile of the user that requests the creation
80
	 * @param creator the creator of the aggregation object (if null or empty will be the aforementioned user)
81
	 * @param description the description of the aggregation object
82
	 * @param language the language of the aggregation object
83
	 * @param subject the subject of the aggregation object
84
	 * @param title the title of the aggregation object
85
	 * @param contentIds the ids of the contents of the aggregation object
86
	 * @return the id of the created aggregation object
87
	 * @throws EPManagerException if any errors occur
88
	 */
89
	public String saveAggregationObject(UserProfile userProfile, String description, String language, String subject, String title, Set<String> contentIds) throws EPManagerException;
90
	
91
	/**
92
	 * Get available enhanced publications.
93
	 * @param pageSize the page size to use
94
	 * @param pageNumber the number of the page to fetch
95
	 * @return a document page containing the corresponding subset of available enhanced publications 
96
	 * @throws EPManagerException if any errors occur
97
	 */
98
	public DocumentPage getEnhancedPublications(int pageSize, int pageNumber) throws EPManagerException;
99
	
100
	/**
101
	 * Get an existing enhanced publication.
102
	 * @param id the id of the enhanced publication to fetch
103
	 * @return the enhanced publication with this id or null if none is found
104
	 * @throws EPManagerException if any errors occur
105
	 */
106
	public Document getEnhancedPublication(String id) throws EPManagerException;
107
	
108
	/**
109
	 * Get an existing node.
110
	 * @param id the id of the node to fetch
111
	 * @return the node with the given id or null if none is found
112
	 * @throws EPManagerException if any errors occur
113
	 */
114
	public Node getNode(String id) throws EPManagerException;
115
	
116
	/**
117
	 * Get the parents of a node.	
118
	 * @param node the node to fetch parents of
119
	 * @return a set containing all the parents as related nodes
120
	 * @throws EPManagerException if any errors occur
121
	 */
122
	public Set<RelatedNode> getParents(Node node) throws EPManagerException;
123
	
124
	/**
125
	 * Get the children of a node.
126
	 * @param node the node to fetch children of
127
	 * @return a set containing all the children as related nodes
128
	 * @throws EPManagerException if any errors occur
129
	 */
130
	public Set<RelatedNode> getChildren(Node node) throws EPManagerException;
131
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/Graph.java
1
package gr.uoa.di.web.utils.ep;
2

  
3
import java.util.Set;
4

  
5
public class Graph {
6
	private Set<Node> nodes;
7
	private Set<Edge> edges;
8
	
9
	public Graph(Set<Node> nodes, Set<Edge> edges) {
10
		this.nodes = nodes;
11
		this.edges = edges;
12
	}
13
	
14
	public Set<Node> getNodes() {
15
		return nodes;
16
	}
17
	
18
	public Set<Edge> getEdges() {
19
		return edges;
20
	}
21
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/Edge.java
1
package gr.uoa.di.web.utils.ep;
2

  
3
public class Edge {
4
	private String type;
5
	private long source;
6
	private long target;
7
	
8
	public Edge(String type, long source, long target) {
9
		this.type = type;
10
		this.source = source;
11
		this.target = target;
12
	}
13
	
14
	public String getType() {
15
		return type;
16
	}
17
	
18
	public long getSource() {
19
		return source;
20
	}
21
	
22
	public long getTarget() {
23
		return target;
24
	}
25
	
26
	@Override
27
	public int hashCode() {
28
		return (int) (type.hashCode() + source + target);
29
	}
30
	
31
	@Override
32
	public boolean equals(Object object) {
33
		if (object == this)
34
			return true;
35
		if (!(object instanceof Edge))
36
			return false;
37
		Edge edge = (Edge) object;
38
		return type.equals(edge.getType()) && (source == edge.getSource()) && (target == edge.getTarget());
39
	}
40
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/RelatedObject.java
1
package gr.uoa.di.web.utils.ep;
2

  
3
import gr.uoa.di.web.utils.webdocument.WebDocument;
4

  
5
public class RelatedObject {
6

  
7
	public String relation = null;
8
	public WebDocument object = null;
9
	
10
	public RelatedObject(String relation, WebDocument object) {
11
		super();
12
		this.relation = relation;
13
		this.object = object;
14
	}
15

  
16
	public String getRelation() {
17
		return relation;
18
	}
19

  
20
	public WebDocument getobject() {
21
		return object;
22
	}
23

  
24
	@Override
25
	public int hashCode() {
26
		final int prime = 31;
27
		int result = 1;
28
		
29
		result = prime * result
30
				+ ((object == null) ? 0 : object.getId().hashCode());
31
		
32
		result = prime * result + ((relation == null) ? 0 : relation.hashCode());
33
		
34
		return result;
35
	}
36

  
37
	@Override
38
	public boolean equals(Object obj) {
39
		if (this == obj)
40
			return true;
41
		if (obj == null)
42
			return false;
43
		if (getClass() != obj.getClass())
44
			return false;
45
		RelatedObject other = (RelatedObject) obj;
46
		
47
		if (object == null) {
48
			if (other.object != null)
49
				return false;
50
		} else if (!object.getId().equals(other.object.getId()))
51
			return false;
52
		
53
		if (relation == null) {
54
			if (other.relation != null)
55
				return false;
56
		} else if (!relation.equals(other.relation))
57
			return false;
58
		return true;
59
	}
60
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/EPManagerMock.java
1
package gr.uoa.di.web.utils.ep;
2

  
3

  
4
import edu.emory.mathcs.backport.java.util.Collections;
5
import eu.dnetlib.api.data.PublisherService;
6
import eu.dnetlib.api.data.PublisherServiceException;
7
import eu.dnetlib.domain.data.Document;
8
import eu.dnetlib.domain.functionality.UserProfile;
9
import gr.uoa.di.driver.util.ServiceLocator;
10
import gr.uoa.di.web.utils.ep.domain.Edge;
11
import gr.uoa.di.web.utils.ep.domain.Entity;
12
import gr.uoa.di.web.utils.ep.domain.Field;
13
import gr.uoa.di.web.utils.ep.domain.Graph;
14
import gr.uoa.di.web.utils.ep.domain.Node;
15
import gr.uoa.di.web.utils.ep.domain.RelatedNode;
16
import gr.uoa.di.web.utils.ep.domain.Relation;
17
import gr.uoa.di.web.utils.search.DocumentPage;
18
import gr.uoa.di.web.utils.search.DocumentReader;
19

  
20
import java.io.UnsupportedEncodingException;
21
import java.net.URLDecoder;
22
import java.util.ArrayList;
23
import java.util.HashMap;
24
import java.util.HashSet;
25
import java.util.Iterator;
26
import java.util.List;
27
import java.util.Map;
28
import java.util.Set;
29
import java.util.UUID;
30

  
31
import org.apache.log4j.Logger;
32

  
33
public class EPManagerMock implements EPManager {
34
	private static final String FORMAT = "DMF";
35
	private static final String COMMAND = "command";
36
	private static final String CREATE = "create";
37
	private static final String IMPORT = "import";
38
	private static final String EDIT = "edit";
39
	private static final String DELETE ="delete";
40
	private static final String ADD = "add";
41
	private static final String REMOVE = "remove";
42
	private static final String ID = "id";
43
	private static final String ENTITY = "entity";
44
	private static final String RELATION = "relation";
45
	private static final String SOURCE = "source";
46
	private static final String TARGET = "target";
47
	private static final String DOCUMENT_PREFIX = "document.";
48
	private static final String PARAMETER_DELIMITER = "&";
49
	private static final String UTF8 = "utf8";
50
	private static final String VALUE_DELIMITER = "=";
51
	private static final String USER_PLACEHOLDER = "$user";
52
	private static final String TEMP_ID_PREFIX = "temporaryId";
53
	
54
	private static Logger logger = Logger.getLogger(EPManagerMock.class);
55
	
56
	private ServiceLocator<PublisherService> publisherServiceLocator;
57
	private DocumentReader documentReader;
58
	private Field description;
59
	private Field language;
60
	private Field subject;
61
	private Field title;
62
	private Entity aggregationObjects;
63
	private Entity ePrints;
64
	private Entity ePubs;
65
	private Entity nonEPrints;
66
	private Relation aggregates;
67
	private Relation composedBy;
68
	private Relation hasEPrint;
69
	private Set<Entity> entities;
70
	private Set<Relation> relations;
71
	private Graph graph;
72
	private Map<String, Map<String, String>> temporaryIds;
73
	
74
	@SuppressWarnings("unchecked")
75
	public EPManagerMock() {
76
		Field cObjCategory = new Field("CobjCategory", "Compound Object Category", false, false, null);
77
		Field cObjContentSynthesis = new Field("CobjContentSynthesis", "Compound Object Content Synthesis", false, false, null);
78
		Field cObjDescriptionSynthesis = new Field("CobjDescriptionSynthesis", "Compound Object Description Synthesis", false, false, null);
79
		Field cObjIdentifier = new Field("id", "Compound Object Identifier", false, false, null);
80
		Field cObjMDFormats = new Field("CobjMDFormats", "Compound Object Metadata Formats", false, false, null);
81
		Field cObjModel = new Field("CobjModel", "Compound Object Model", false, false, null);
82
		Field cObjTypologyCompoundObject = new Field("CobjTypology", "Compound Object Typology", false, false, "Compound Object");
83
		Field cObjTypologyTextual = new Field("CobjTypology", "Compound Object Typology", false, false, "Textual");
84
		Field cObjTypologyDataSet = new Field("CobjTypology", "Compound Object Typology", false, false, "Data Set");
85
		Field contributor = new Field("contributor", "Contributor", true, false, null);
86
		Field creator = new Field("creator", "Creator", true, false, "$user");
87
		Field dateAccepted = new Field("dateAccepted", "Date Accepted", false, false, null);
88
		Field dateOfCollection = new Field("dateOfCollection", "Date Of Collection", false, false, null);
89
		description = new Field("description", "Description", false, true, null);
90
		Field identifier = new Field("identifier", "Identifier", false, false, null);
91
		Field itemIdentifier = new Field("itemIdentifier", "Item Identifier", false, false, null);
92
		language = new Field("language", "Language", false, true, null);
93
		Field mdFormat = new Field("mdFormat", "Metadata Format", false, false, "DMF");
94
		Field mdFormatInterpretation = new Field("mdFormatInterpretation", "Metadata Format Interpretation", false, false, null);
95
		Field objIdentifier = new Field("objIdentifier", "Object Identifier", false, false, null);
96
		Field objectIdentifier = new Field("objectIdentifier", "Object Identifier", false, false, null);
97
		Field publisher = new Field("publisher", "Publisher", false, false, null);
98
		Field recordIdentifier = new Field("recordIdentifier", "Record Identifier", false, false, null);
99
		Field relation = new Field("relation", "Relation", true, false, null);
100
		Field repositoryCountry = new Field("repositoryCountry", "Repository Country", false, false, null);
101
		Field repositoryId = new Field("repositoryId", "Repository Identifier", false, false, null);
102
		Field repositoryInstitution = new Field("repositoryInstitution", "RepositoryInstitution", false, false, "Driver");
103
		Field repositoryLink = new Field("repositoryLink", "Repository Link", false, false, "http://search.driver.research-infrastructures.eu/");
104
		Field repositoryName = new Field("repositoryName", "Repository Name", false, false, "Driver");
105
		Field source = new Field("source", "Source", false, false, null);
106
		subject = new Field("subject", "Subject", true, true, null);
107
		title = new Field("title", "Title", true, true, null);
108
		Field sequenceNo = new Field("sequenceNo", "Sequence Number", false, true, null);
109
		
110
		Set<Field> commonFields = new HashSet<Field>();
111
		commonFields.add(cObjCategory);
112
		commonFields.add(cObjContentSynthesis);
113
		commonFields.add(cObjDescriptionSynthesis);
114
		commonFields.add(cObjIdentifier);
115
		commonFields.add(cObjMDFormats);
116
		commonFields.add(cObjModel);
117
		commonFields.add(contributor);
118
		commonFields.add(creator);
119
		commonFields.add(dateAccepted);
120
		commonFields.add(dateOfCollection);
121
		commonFields.add(description);
122
		commonFields.add(identifier);
123
		commonFields.add(itemIdentifier);
124
		commonFields.add(language);
125
		commonFields.add(mdFormat);
126
		commonFields.add(mdFormatInterpretation);
127
		commonFields.add(objIdentifier);
128
		commonFields.add(objectIdentifier);
129
		commonFields.add(publisher);
130
		commonFields.add(recordIdentifier);
131
		commonFields.add(relation);
132
		commonFields.add(repositoryCountry);
133
		commonFields.add(repositoryId);
134
		commonFields.add(repositoryInstitution);
135
		commonFields.add(repositoryLink);
136
		commonFields.add(repositoryName);
137
		commonFields.add(source);
138
		commonFields.add(subject);
139
		commonFields.add(title);
140
		
141
		Set<Field> compoundObjectFields = new HashSet<Field>(commonFields);
142
		compoundObjectFields.add(cObjTypologyCompoundObject);
143
		Set<Field> textualFields = new HashSet<Field>(commonFields);
144
		textualFields.add(cObjTypologyTextual);
145
		Set<Field> dataSetFields = new HashSet<Field>(commonFields);
146
		dataSetFields.add(cObjTypologyDataSet);
147
		
148
		aggregationObjects = new Entity("AggregationObjects", compoundObjectFields, cObjIdentifier.getName(), cObjTypologyCompoundObject.getName(), title.getName(), true, "Aggregation Object", 0xffa9a9ff, "diamond");
149
		ePrints = new Entity("ePrints", textualFields, cObjIdentifier.getName(), cObjTypologyTextual.getName(), title.getName(), false, "E-Print", 0xffdd7a00, "circle");
150
		ePubs = new Entity("ePubs", compoundObjectFields, cObjIdentifier.getName(), cObjTypologyCompoundObject.getName(), title.getName(), true, "E-Pub", 0xffa13ba4, "square");
151
		nonEPrints = new Entity("nonEPrints", textualFields, cObjIdentifier.getName(), cObjTypologyTextual.getName(), title.getName(), false, "Non-E-Print", 0xff3a9db8, "circle");
152
		Entity researchData = new Entity("ResearchData", dataSetFields, cObjIdentifier.getName(), cObjTypologyDataSet.getName(), title.getName(), false, "Research Data", 0xff8aac46, "circle");
153
		Entity sequence = new Entity("Sequence", Collections.singleton(sequenceNo), null, null, sequenceNo.getName(), true, "Sequence", 0xff7f7f7f, "circle"); 
154
		
155
		entities = new HashSet<Entity>();
156
		entities.add(aggregationObjects);
157
		entities.add(ePrints);
158
		entities.add(ePubs);
159
		entities.add(nonEPrints);
160
		entities.add(researchData);
161
		entities.add(sequence);
162

  
163
		Set<String> components = new HashSet<String>();
164
		components.add(aggregationObjects.getName());
165
		components.add(ePrints.getName());
166
		components.add(nonEPrints.getName());
167
		components.add(researchData.getName());
168
		
169
		aggregates = new Relation("aggregates", Collections.singleton(aggregationObjects.getName()), components, "many_to_many", "part_part", "aggregates", 2, 0xff7f7f7f); 
170
		Relation cites = new Relation("cites", Collections.singleton(ePrints.getName()), Collections.singleton(ePrints.getName()), "many_to_many", "part_part", "cites", 2, 0xff7f7f7f);
171
		composedBy = new Relation("composedBy", Collections.singleton(ePubs.getName()), components, "many_to_many", "part_part", "composed by", 2, 0xff7f7f7f);
172
		Relation generatedBy = new Relation("generatedBy", Collections.singleton(researchData.getName()), Collections.singleton(researchData.getName()), "many_to_many", "part_part", "generated by", 2, 0xff7f7f7f);
173
		hasEPrint = new Relation("hasEPrint", Collections.singleton(ePubs.getName()), Collections.singleton(ePrints.getName()), "many_to_one", "tot_part", "has e-print", 3, 0xffa13ba4);
174
		Relation order = new Relation("order", Collections.singleton(aggregates.getName()), Collections.singleton(sequence.getName()), "one_to_one", "part_tot", "order", 2, 0xff7f7f7f);
175
		Relation relatedWith = new Relation("relatedWith", Collections.singleton(ePubs.getName()), Collections.singleton(ePubs.getName()), "many_to_many", "part_part", "related with", 2, 0xff7f7f7f);
176
		
177
		
178
		relations = new HashSet<Relation>();
179
		relations.add(aggregates);
180
		relations.add(cites);
181
		relations.add(composedBy);
182
		relations.add(generatedBy);
183
		relations.add(hasEPrint);
184
		relations.add(order);
185
		relations.add(relatedWith);
186

  
187
		graph = new Graph();
188
		temporaryIds = new HashMap<String, Map<String, String>>();
189
		logger.debug("EPManagerMock initialized successfully");
190
	}
191

  
192
	public void setPublisherServiceLocator(ServiceLocator<PublisherService> publisherServiceLocator) {
193
		this.publisherServiceLocator = publisherServiceLocator;
194
	}
195
	
196
	
197
	public void setDocumentReader(DocumentReader documentReader) {
198
		this.documentReader = documentReader;
199
	}
200
	
201
	
202
	@Override
203
	public Set<Entity> getEntities() {
204
		logger.debug("Retrieved entities");
205
		return entities;
206
	}
207
	
208
	
209
	@Override
210
	public Set<Relation> getRelations() {
211
		logger.debug("Retrieved relations");
212
		return relations;
213
	}
214
	
215

  
216
	@Override
217
	public Set<Map<String, List<String>>> getDropboxContents(UserProfile userProfile) throws EPManagerException {
218
		Set<Map<String, List<String>>> dropboxContents = new HashSet<Map<String, List<String>>>();
219
		try {
220
			for (String documentId : userProfile.getDocumentIds()) {
221
				String xml = publisherServiceLocator.getService().getResourceById(documentId, FORMAT);
222
				Map<String, List<String>> document = null;
223
				if (xml != null)
224
					document = documentReader.read(xml).getMap();
225
				else
226
					document = getNode(documentId).getDocumentMap();
227
				if (document != null)
228
					dropboxContents.add(document);
229
			}
230
		} catch (PublisherServiceException e) {
231
			logger.error("Error retrieving dropbox contents of user " + userProfile, e);
232
			throw new EPManagerException("Error retrieving dropbox contents of user " + userProfile, e);
233
		}
234
		logger.debug("Retrieved dropbox contents of user " + userProfile);
235
		return dropboxContents;
236
	}
237

  
238
	@Override
239
	public Graph getGraph(String rootId) {
240
		Graph graph = new Graph();
241
		Node root = getNode(rootId);
242
		if (root == null) {
243
			logger.debug("No graph with root " + rootId + " exists");
244
			return null;
245
		}
246
		graph.getNodes().add(root);
247
		for (RelatedNode parent : getParents(root)) {
248
			Node node = parent.getNode();
249
			graph.getNodes().add(node);
250
			graph.getEdges().add(new Edge(parent.getRelation(), node.getDocumentMap().get(getEntity(node.getEntity()).getIdField()).get(0), root.getDocumentMap().get(getEntity(root.getEntity()).getIdField()).get(0)));
251
		}
252
		getDescendants(root, graph);
253
		logger.debug("Retrieved graph with root " + rootId);
254
		return graph;
255
	}
256
	
257
	
258
	@Override
259
	public void saveGraph(UserProfile userProfile, String [] commands) throws EPManagerException {
260
		for (String command : commands)
261
			executeCommand(userProfile, command);
262
		temporaryIds.put(userProfile.getResourceId(), null);
263
		logger.debug("User " + userProfile + " saved graph");
264
	}
265
	
266
	
267
	@Override
268
	public String saveEnhancedPublication(UserProfile userProfile, String description, String language, String subject, String title, String ePrintId, Set<String> nonEPrintIds) throws EPManagerException {
269
		Map<String, List<String>> document = new HashMap<String, List<String>>();
270
		document.put(DOCUMENT_PREFIX + this.description.getName(), new ArrayList<String>());
271
		document.get(DOCUMENT_PREFIX + this.description.getName()).add(description);
272
		document.put(DOCUMENT_PREFIX + this.language.getName(), new ArrayList<String>());
273
		document.get(DOCUMENT_PREFIX + this.language.getName()).add(language);
274
		document.put(DOCUMENT_PREFIX + this.subject.getName(), new ArrayList<String>());
275
		document.get(DOCUMENT_PREFIX + this.subject.getName()).add(subject);
276
		document.put(DOCUMENT_PREFIX + this.title.getName(), new ArrayList<String>());
277
		document.get(DOCUMENT_PREFIX + this.title.getName()).add(title);
278
		String id = createNode(userProfile, TEMP_ID_PREFIX + Math.round(Math.random() * Long.MAX_VALUE), ePubs.getName());
279
		editNode(userProfile, id, document);
280
		importNode(userProfile, ePrintId, ePrints.getName());
281
		addEdge(userProfile, hasEPrint.getName(), id, ePrintId);
282
		for (String nonEPrintId : nonEPrintIds) {
283
			importNode(userProfile, nonEPrintId, nonEPrints.getName());
284
			addEdge(userProfile, composedBy.getName(), id, nonEPrintId);
285
		}
286
		logger.debug("User " + userProfile + " saved enhanced publication " + id);
287
		return id;
288
	}
289

  
290
	@Override
291
	public String saveAggregationObject(UserProfile userProfile, String description, String language, String subject, String title, Set<String> contentIds) throws EPManagerException {
292
		Map<String, List<String>> document = new HashMap<String, List<String>>();
293
		document.put(DOCUMENT_PREFIX + this.description.getName(), new ArrayList<String>());
294
		document.get(DOCUMENT_PREFIX + this.description.getName()).add(description);
295
		document.put(DOCUMENT_PREFIX + this.language.getName(), new ArrayList<String>());
296
		document.get(DOCUMENT_PREFIX + this.language.getName()).add(language);
297
		document.put(DOCUMENT_PREFIX + this.subject.getName(), new ArrayList<String>());
298
		document.get(DOCUMENT_PREFIX + this.subject.getName()).add(subject);
299
		document.put(DOCUMENT_PREFIX + this.title.getName(), new ArrayList<String>());
300
		document.get(DOCUMENT_PREFIX + this.title.getName()).add(title);		
301
		String id = createNode(userProfile, TEMP_ID_PREFIX + Math.round(Math.random() * Long.MAX_VALUE), aggregationObjects.getName());
302
		editNode(userProfile, id, document);
303
		for (String contentId : contentIds) {
304
			importNode(userProfile, contentId, nonEPrints.getName());
305
			addEdge(userProfile, aggregates.getName(), id, contentId);
306
		}
307
		logger.debug("User " + userProfile + " saved aggregation object " + id);
308
		return id;
309
	}
310
	
311
	@Override
312
	public DocumentPage getEnhancedPublications(int pageSize, int pageNumber) {
313
		List<Document> documents = new ArrayList<Document>();
314
		for (Node node : graph.getNodes()) {
315
			if (node.getEntity().equals(ePubs.getName()))
316
				documents.add(new Document(node.getDocumentMap()));
317
		} 
318
		int numberOfDocuments = documents.size();
319
		int numberOfPages = numberOfDocuments / pageSize;
320
		if ((numberOfDocuments % pageSize) > 0)
321
			numberOfPages++;
322
		int start = (pageNumber - 1) * pageSize;
323
		if (start < 0)
324
			start = 0;
325
		int end = pageNumber * pageSize;
326
		if (end > numberOfDocuments)
327
			end = numberOfDocuments;
328
		documents = documents.subList(start, end);
329
		logger.debug("Retrieved enhanced publications");
330
		return new DocumentPage(documents, pageSize, pageNumber, numberOfDocuments, numberOfPages);
331
	}
332
	
333
	
334
	@Override
335
	public Document getEnhancedPublication(String id) {
336
		Node node = getNode(id);
337
		if ((node == null) || (!node.getEntity().equals(ePubs.getName()))) {
338
			logger.debug("Enhanced publication " + id + " not found");
339
			return null;
340
		} else {
341
			logger.debug("Retrieved enhanced publication " + id);
342
			return new Document(node.getDocumentMap());
343
		}
344
	}
345
	
346

  
347
	@Override
348
	public Node getNode(String id) {
349
		for (Node node : graph.getNodes()) {
350
			if (node.getDocumentMap().get(getEntity(node.getEntity()).getIdField()).get(0).equals(id)) {
351
				logger.debug("Retrieved node " + id);
352
				return node;
353
			}
354
		}
355
		logger.debug("Node " + id + " not found");
356
		return null;
357
	}
358

  
359
	
360
	@Override
361
	public Set<RelatedNode> getParents(Node node) {
362
		Set<RelatedNode> parents = new HashSet<RelatedNode>();
363
		String id = node.getDocumentMap().get(getEntity(node.getEntity()).getIdField()).get(0);
364
		for (Edge edge : graph.getEdges()) {
365
			if (edge.getTarget().equals(id))
366
				parents.add(new RelatedNode(edge.getRelation(), getNode(edge.getSource())));
367
		}
368
		logger.debug("Retrieved parents of node " + id);
369
		return parents;
370
	}
371
	
372

  
373
	@Override
374
	public Set<RelatedNode> getChildren(Node node) {
375
		Set<RelatedNode> children = new HashSet<RelatedNode>();
376
		String id = node.getDocumentMap().get(getEntity(node.getEntity()).getIdField()).get(0);
377
		for (Edge edge : graph.getEdges()) {
378
			if (edge.getSource().equals(id))
379
				children.add(new RelatedNode(edge.getRelation(), getNode(edge.getTarget())));
380
		}
381
		logger.debug("Retrieved children of node " + id);
382
		return children;
383
	}
384
	
385
		
386
	private void getDescendants(Node root, Graph graph){
387
		String rootId = root.getDocumentMap().get(getEntity(root.getEntity()).getIdField()).get(0);
388
		for (RelatedNode child : getChildren(root)) {
389
			Node node = child.getNode();
390
			graph.getNodes().add(node);
391
			graph.getEdges().add(new Edge(child.getRelation(), rootId, node.getDocumentMap().get(getEntity(node.getEntity()).getIdField()).get(0)));
392
			getDescendants(node, graph);
393
		}
394
		logger.debug("Retrieved descendants of node " + rootId);
395
	}
396
	
397
	
398
	private void executeCommand(UserProfile userProfile, String command) throws EPManagerException {
399
		Map<String, List<String>> arguments = parseCommand(command);
400
		if (arguments.get(COMMAND).get(0).equals(CREATE)) {
401
			createNode(userProfile, arguments.get(ID).get(0), arguments.get(ENTITY).get(0));
402
		} else if (arguments.get(COMMAND).get(0).equals(IMPORT)) {
403
			importNode(userProfile, arguments.get(ID).get(0), arguments.get(ENTITY).get(0));
404
		} else if (arguments.get(COMMAND).get(0).equals(EDIT)) {
405
			editNode(userProfile, arguments.get(ID).get(0), arguments);
406
		} else if (arguments.get(COMMAND).get(0).equals(DELETE)) {
407
			deleteNode(userProfile, arguments.get(ID).get(0));
408
		} else if (arguments.get(COMMAND).get(0).equals(ADD)) {
409
			addEdge(userProfile, arguments.get(RELATION).get(0), arguments.get(SOURCE).get(0), arguments.get(TARGET).get(0));
410
		} else if (arguments.get(COMMAND).get(0).equals(REMOVE)) {
411
			removeEdge(userProfile, arguments.get(RELATION).get(0), arguments.get(SOURCE).get(0), arguments.get(TARGET).get(0));
412
		}
413
		logger.debug("User " + userProfile + " executed command " + command);
414
	}
415
	
416
	private Map<String, List<String>> parseCommand(String command) throws EPManagerException {
417
		Map<String, List<String>> arguments = new HashMap<String, List<String>>();
418
		try {
419
			for (String parameter: command.split(PARAMETER_DELIMITER)) {
420
				String [] pair = parameter.split(VALUE_DELIMITER);
421
				String key = URLDecoder.decode(pair[0], UTF8);
422
				String value = (pair.length > 1) ? URLDecoder.decode(pair[1], UTF8) : "";
423
				if (arguments.get(key) == null)
424
					arguments.put(key, new ArrayList<String>());
425
				arguments.get(key).add(value);
426
			}
427
		} catch (UnsupportedEncodingException e) {
428
			logger.error("Error parsing command " + command);
429
			throw new EPManagerException("Error parsing command " + command, e);
430
		}
431
		return arguments;
432
	}
433
	
434
	
435
	private String createNode(UserProfile userProfile, String id, String entity) {
436
		String userId = userProfile.getResourceId();
437
		String uuid = UUID.randomUUID().toString();
438
		if (temporaryIds.get(userId) == null)
439
			temporaryIds.put(userId, new HashMap<String, String>());
440
		temporaryIds.get(userId).put(id, uuid);
441
		Entity ent = getEntity(entity);
442
		Map<String, List<String>> document = new HashMap<String, List<String>>();
443
		
444
		for (Field field : ent.getFields()) {
445
			String name = field.getName();
446
			String defaultValue = field.getDefaultValue();
447
			document.put(name, new ArrayList<String>());
448
			if (name.equals(ent.getIdField()))
449
				document.get(name).add(uuid);
450
			if (defaultValue == null)
451
				continue;
452
			else if(defaultValue.equals(USER_PLACEHOLDER)) {
453
				String user = userProfile.getFirstname() + " " + userProfile.getLastname();
454
				if (user.trim().isEmpty())
455
					user = userProfile.getEmail();
456
				document.get(name).add(user);
457
			} else
458
				document.get(name).add(defaultValue);
459
		}
460
		graph.getNodes().add(new Node(entity, document));
461
		logger.debug("User " + userProfile + " created node " + id + " as " + entity + " (new id " + uuid + ")");
462
		return uuid;
463
	}
464
	
465
	private void importNode(UserProfile userProfile, String id, String entity) throws EPManagerException {
466
		Entity ent = getEntity(entity);
467
		for (Map<String, List<String>> document: getDropboxContents(userProfile)) {
468
			if (document.get(ent.getIdField()).get(0).equals(id)) {
469
				graph.getNodes().add(new Node(entity, document));
470
				logger.debug("User " + userProfile + " imported node " + id + " as " + entity);
471
				return;
472
			}
473
		}
474
		logger.debug("Document " + id + " not found in dropbox of user " + userProfile);
475
	}
476
	
477
	private void editNode(UserProfile userProfile, String id, Map<String, List<String>> document) {
478
		id = resolveId(userProfile.getResourceId(), id);
479
		for (Node node : graph.getNodes()) {
480
			Entity entity = getEntity(node.getEntity());
481
			if (node.getDocumentMap().get(entity.getIdField()).get(0).equals(id)) {
482
				for (Field field : entity.getFields()) {
483
					String name = field.getName();
484
					List<String> values = document.get(DOCUMENT_PREFIX + name);
485
					if (values != null)
486
							node.getDocumentMap().put(name, values);
487
				}
488
				logger.debug("User " + userProfile + " edited node " + id);
489
				return;
490
			}
491
		}
492
	}
493
	
494
	private void deleteNode(UserProfile userProfile, String id) {
495
		id = resolveId(userProfile.getResourceId(), id);
496
		for (Iterator<Node> i = graph.getNodes().iterator(); i.hasNext();) {
497
			Node node = i.next();
498
			if (node.getDocumentMap().get(getEntity(node.getEntity()).getIdField()).get(0).equals(id)) {
499
				for (RelatedNode parent : getParents(node)) // break relations with parents
500
					removeEdge(userProfile, parent.getRelation(), parent.getNode().getDocumentMap().get(getEntity(parent.getNode().getEntity()).getIdField()).get(0), id);
501
				for (RelatedNode child: getChildren(node)) // break relations with children
502
					removeEdge(userProfile, child.getRelation(), id, child.getNode().getDocumentMap().get(getEntity(child.getNode().getEntity()).getIdField()).get(0));
503
				i.remove(); // remove node
504
				break;
505
			}
506
		}		
507
		logger.debug("User " + userProfile + " deleted node " + id);
508
	}
509
	
510
	private void addEdge(UserProfile userProfile, String relation, String source, String target) {
511
		source = resolveId(userProfile.getResourceId(), source);
512
		target = resolveId(userProfile.getResourceId(), target);
513
		graph.getEdges().add(new Edge(relation, source, target));
514
		logger.debug("User " + userProfile + " added edge " + relation + " from node " + source + " to node " + target);
515
	}
516
	
517
	private void removeEdge(UserProfile userProfile, String relation, String source, String target) {
518
		source = resolveId(userProfile.getResourceId(), source);
519
		target = resolveId(userProfile.getResourceId(), target);
520
		for (Iterator<Edge> i = graph.getEdges().iterator(); i.hasNext();) {
521
			Edge edge = i.next();
522
			if (edge.getRelation().equals(relation) && edge.getSource().equals(source) && edge.getTarget().equals(target)) {
523
				i.remove();
524
				break;
525
			}
526
		}
527
		logger.debug("User " + userProfile + " removed edge " + relation + " from node " + source + " to node " + target);
528
	}
529
	
530
	private Entity getEntity(String name) {
531
		for (Entity entity : entities) {
532
			if (entity.getName().equals(name))
533
				return entity;
534
		}
535
		return null;
536
	}
537
	
538
	private String resolveId(String userId, String id) {
539
		return id.startsWith(TEMP_ID_PREFIX) ? temporaryIds.get(userId).get(id) : id;
540
	}
541
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/CachingEPManager.java
1
package gr.uoa.di.web.utils.ep;
2

  
3
import eu.dnetlib.domain.data.Document;
4
import eu.dnetlib.domain.functionality.UserProfile;
5
import gr.uoa.di.web.utils.ep.domain.Entity;
6
import gr.uoa.di.web.utils.ep.domain.Graph;
7
import gr.uoa.di.web.utils.ep.domain.Node;
8
import gr.uoa.di.web.utils.ep.domain.RelatedNode;
9
import gr.uoa.di.web.utils.ep.domain.Relation;
10
import gr.uoa.di.web.utils.search.DocumentPage;
11

  
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16

  
17
/**
18
 * This class implements an EPManager that caches entities and relations.
19
 * @author thanos
20
 *
21
 */
22
public class CachingEPManager implements EPManager {
23
	private EPManager epManager;
24
	private Set<Entity> entities;
25
	private Set<Relation> relations;
26
	private Map<String, Set<Map<String, List<String>>>> dropboxContents;
27
	
28
	public CachingEPManager() {
29
		epManager = null;
30
		entities = null;
31
		relations = null;
32
		dropboxContents = new HashMap<String, Set<Map<String, List<String>>>>();
33
	}
34
	
35
	public EPManager getEpManager() {
36
		return epManager;
37
	}
38

  
39
	public void setEpManager(EPManager epManager) {
40
		this.epManager = epManager;
41
	}
42
		
43
	@Override
44
	public Set<Entity> getEntities() throws EPManagerException {
45
		if (entities == null)
46
			entities = epManager.getEntities();
47
		return entities;
48
	}
49
		
50
	@Override
51
	public Set<Relation> getRelations() throws EPManagerException {
52
		if (relations == null)
53
			relations = epManager.getRelations();
54
		return relations;
55
	}
56
	
57
	@Override
58
	public Set<Map<String, List<String>>> getDropboxContents(UserProfile userProfile) throws EPManagerException {
59
		if (dropboxContents.get(userProfile.getResourceId()) == null)
60
			dropboxContents.put(userProfile.getResourceId(), epManager.getDropboxContents(userProfile));
61
		return dropboxContents.get(userProfile.getResourceId());
62
	}
63
	
64
	@Override
65
	public Graph getGraph(String rootDriverId) throws EPManagerException {
66
		return epManager.getGraph(rootDriverId); // can not cache graphs since may be edited by other user
67
	}
68
	
69
	@Override
70
	public void saveGraph(UserProfile userProfile, String [] commands) throws EPManagerException {
71
		epManager.saveGraph(userProfile, commands);
72
	}
73

  
74
	@Override
75
	public String saveEnhancedPublication(UserProfile userProfile, String description, String language, String subject, String title, String ePrintId, Set<String> nonEPrintIds) throws EPManagerException {
76
		return epManager.saveEnhancedPublication(userProfile, description, language, subject, title, ePrintId, nonEPrintIds);
77
	}
78
	
79
	@Override
80
	public String saveAggregationObject(UserProfile userProfile, String description, String language, String subject, String title, Set<String> contentIds) throws EPManagerException {
81
		return epManager.saveAggregationObject(userProfile, description, language, subject, title, contentIds);
82
	}
83

  
84
	@Override
85
	public DocumentPage getEnhancedPublications(int pageSize, int pageNumber) throws EPManagerException {
86
		return epManager.getEnhancedPublications(pageSize, pageNumber);
87
	}
88

  
89
	@Override
90
	public Document getEnhancedPublication(String id) throws EPManagerException {
91
		return epManager.getEnhancedPublication(id);
92
	}
93
	
94
	@Override
95
	public Node getNode(String id) throws EPManagerException {
96
		return epManager.getNode(id);
97
	}
98

  
99
	@Override
100
	public Set<RelatedNode> getParents(Node node) throws EPManagerException {
101
		return epManager.getParents(node);
102
	}
103

  
104
	@Override
105
	public Set<RelatedNode> getChildren(Node node) throws EPManagerException {
106
		return epManager.getChildren(node);
107
	}
108
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/EPManagerException.java
1
package gr.uoa.di.web.utils.ep;
2

  
3
public class EPManagerException extends Exception {
4
	private static final long serialVersionUID = 1L;
5

  
6
	public EPManagerException() {
7
		super();
8
	}
9

  
10
	public EPManagerException(String message, Throwable cause) {
11
		super(message, cause);
12
	}
13

  
14
	public EPManagerException(String message) {
15
		super(message);
16
	}
17

  
18
	public EPManagerException(Throwable cause) {
19
		super(cause);
20
	}
21
}
modules/uoa-web-utils/trunk/src/main/gr/uoa/di/web/utils/ep/DLMSEPManager.java
1
package gr.uoa.di.web.utils.ep;
2

  
3
import eu.dnetlib.domain.data.Document;
4
import eu.dnetlib.domain.functionality.UserProfile;
5
import gr.uoa.di.web.utils.ep.domain.Entity;
6
import gr.uoa.di.web.utils.ep.domain.Graph;
7
import gr.uoa.di.web.utils.ep.domain.Node;
8
import gr.uoa.di.web.utils.ep.domain.RelatedNode;
9
import gr.uoa.di.web.utils.ep.domain.Relation;
10
import gr.uoa.di.web.utils.search.DocumentPage;
11

  
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Set;
15

  
16
public class DLMSEPManager implements EPManager {
17

  
18
	@Override
19
	public Set<RelatedNode> getChildren(Node node) throws EPManagerException {
20
		// TODO Auto-generated method stub
21
		return null;
22
	}
23

  
24
	@Override
25
	public Set<Map<String, List<String>>> getDropboxContents(
26
			UserProfile userProfile) throws EPManagerException {
27
		// TODO Auto-generated method stub
28
		return null;
29
	}
30

  
31
	@Override
32
	public Document getEnhancedPublication(String id) throws EPManagerException {
33
		// TODO Auto-generated method stub
34
		return null;
35
	}
36

  
37
	@Override
38
	public DocumentPage getEnhancedPublications(int pageSize, int pageNumber)
39
			throws EPManagerException {
40
		// TODO Auto-generated method stub
41
		return null;
42
	}
43

  
44
	@Override
45
	public Set<Entity> getEntities() throws EPManagerException {
46
		// TODO Auto-generated method stub
47
		return null;
48
	}
49

  
50
	@Override
51
	public Graph getGraph(String rootId) throws EPManagerException {
52
		// TODO Auto-generated method stub
53
		return null;
54
	}
55

  
56
	@Override
57
	public Node getNode(String id) throws EPManagerException {
58
		// TODO Auto-generated method stub
59
		return null;
60
	}
61

  
62
	@Override
63
	public Set<RelatedNode> getParents(Node node) throws EPManagerException {
64
		// TODO Auto-generated method stub
65
		return null;
66
	}
67

  
68
	@Override
69
	public Set<Relation> getRelations() throws EPManagerException {
70
		// TODO Auto-generated method stub
71
		return null;
72
	}
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff