Project

General

Profile

« Previous | Next » 

Revision 45569

View differences:

modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/tools/DatabaseUtils.java
5 5

  
6 6
public class DatabaseUtils {
7 7

  
8
	public static final String ROOT_COLLECTION = "/db/DRIVER";
8
	private static final String ROOT_COLLECTION = "/db/DRIVER";
9

  
9 10
	public static final String SCHEMAS_COLLECTION = ROOT_COLLECTION + "/schemas";
10 11

  
11
	public String getCollectionAbsPath(final Document doc) {
12
		return ROOT_COLLECTION + "/" + getCollectionPath(doc);
12
	public static String getCollection(final Document doc) {
13
		return getCollection(doc.valueOf("/profile/kind").trim(), doc.valueOf("/profile/type").trim());
13 14
	}
14 15

  
15
	public static String getCollectionPath(final Document doc) {
16
		final StringBuilder buffer = new StringBuilder();
17
		buffer.append(doc.valueOf("/profile/kind").trim());
18
		buffer.append('/');
19
		buffer.append(doc.valueOf("/profile/type").trim());
20
		return buffer.toString();
16
	public static String getCollection(final String kind, final String type) {
17
		return ROOT_COLLECTION + "/" + kind + "/" + type;
21 18
	}
22 19

  
23 20
	public static String getFileName(final String resId) {
24
		return StringUtils.substringAfterLast(resId, ":");
21
		return StringUtils.substringAfterLast(resId, "/");
25 22
	}
26 23

  
27 24
	public String getFileName(final Document doc) {
28 25
		return getFileName(doc.valueOf("/profile/id").trim());
29 26
	}
30 27

  
31
	public static String getCollectionName(final String resId) {
32
		return StringUtils.substringBeforeLast(resId, ":").replaceAll(":", "/");
28
	public static String composeResourceId(final String kind, final String type, final String fileName) {
29
		return kind + "/" + type + "/" + fileName;
33 30
	}
34 31

  
35
	public static String composeResourceId(final String coll, final String fileName) {
36
		return coll.replaceAll("/", ":") + ":" + fileName;
37
	}
38

  
39 32
}
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/tools/BulkResourceImporter.java
82 82
			final Document doc = (new SAXReader()).read(res.getInputStream());
83 83

  
84 84
			final String id = doc.valueOf("/profile/id").trim();
85
			final String kind = doc.valueOf("/profile/kind").trim();
85 86
			final String type = doc.valueOf("/profile/type").trim();
86 87

  
87
			final String coll = DatabaseUtils.getCollectionPath(doc);
88
			final String coll = DatabaseUtils.getCollection(kind, type);
88 89
			final String fileName = DatabaseUtils.getFileName(id);
89 90
			final String date = new DateUtils(new Date()).getDateAsISO8601String();
90 91

  
......
92 93

  
93 94
			final String finalDoc = doc.asXML();
94 95

  
95
			if (profileValidator.isValid(type, finalDoc)) {
96
				isStore.insertXML(fileName, DatabaseUtils.ROOT_COLLECTION + "/" + coll, finalDoc);
96
			if (!id.startsWith(kind + "/" + type + "/")) {
97
				throw new DnetInformationServiceException("Invalid profile ID: " + id);
98
			} else if (!profileValidator.isValid(type, finalDoc)) {
99
				throw new DnetInformationServiceException("Invalid profile");
97 100
			} else {
98
				throw new DnetInformationServiceException("Invalid profile");
101
				isStore.insertXML(fileName, coll, finalDoc);
99 102
			}
100
			log.info("  new profile imported: " + DatabaseUtils.ROOT_COLLECTION + "/" + coll + fileName);
103

  
104
			log.info("  new profile imported: " + id);
101 105
		} catch (final Throwable e) {
102 106
			log.error("  error importing profile " + res.getFilename());
103 107
			throw new DnetInformationServiceException(e);
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/controller/InformationServiceController.java
65 65
		return isStore.deleteXML(resourceType, DatabaseUtils.SCHEMAS_COLLECTION);
66 66
	}
67 67

  
68
	@RequestMapping(value = "profile/{id}", method = RequestMethod.DELETE)
69
	public boolean deleteProfile(@PathVariable("id") final String resId) throws DnetInformationServiceException {
70
		final boolean res = isStore.deleteXML(DatabaseUtils.getFileName(resId), "/db/DRIVER/" + DatabaseUtils.getCollectionName(resId));
71

  
72
		if (!res) { throw new DnetInformationServiceException("document " + resId + " not found"); }
73

  
74
		return true;
75
	}
76

  
77 68
	@RequestMapping(value = "profile", method = RequestMethod.POST)
78 69
	public String registerProfile(@RequestBody final String profile) throws DnetInformationServiceException {
79 70
		log.debug("registering profile");
......
82 73

  
83 74
			final Document doc = (new SAXReader()).read(new StringReader(profile));
84 75

  
85
			final String coll = DatabaseUtils.getCollectionPath(doc);
76
			final String kind = doc.valueOf("/profile/kind").trim();
77
			final String type = doc.valueOf("/profile/type").trim();
86 78
			final String fileName = UUID.randomUUID().toString();
87
			final String newId = DatabaseUtils.composeResourceId(coll, fileName);
79

  
80
			final String newId = DatabaseUtils.composeResourceId(kind, type, fileName);
88 81
			final String date = new DateUtils(new Date()).getDateAsISO8601String();
89 82

  
90
			final String type = doc.valueOf("/profile/type").trim();
91 83
			doc.selectSingleNode("/profile/id").setText(newId);
92 84
			doc.selectSingleNode("/profile/date").setText(date);
93 85

  
94 86
			final String finalDoc = doc.asXML();
95 87

  
96 88
			if (profileValidator.isValid(type, finalDoc)) {
97
				isStore.insertXML(fileName, DatabaseUtils.ROOT_COLLECTION + "/" + coll, finalDoc);
89
				isStore.insertXML(fileName, DatabaseUtils.getCollection(kind, type), finalDoc);
98 90
				return newId;
99 91
			} else {
100 92
				throw new DnetInformationServiceException("Invalid profile");
......
104 96
		}
105 97
	}
106 98

  
107
	@RequestMapping(value = "profile/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
108
	public String getProfile(@PathVariable("id") final String profId) throws DnetInformationServiceException {
109
		if (StringUtils.isBlank(profId)) { throw new DnetInformationServiceException("Invalid null profile ID: " + profId); }
99
	@RequestMapping(value = "profile/{kind}/{type}/{fileName}", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
100
	public String getProfile(@PathVariable("kind") final String kind, @PathVariable("type") final String type, @PathVariable("fileName") final String fileName)
101
			throws DnetInformationServiceException {
110 102

  
111
		final String res = isStore.getXML(
112
				DatabaseUtils.getFileName(profId),
113
				DatabaseUtils.ROOT_COLLECTION + "/" + DatabaseUtils.getCollectionName(profId));
103
		final String res = isStore.getXML(fileName, DatabaseUtils.getCollection(kind, type));
114 104

  
115
		if (res == null) { throw new DnetInformationServiceException("document " + profId + " not found"); }
105
		if (StringUtils.isBlank(res)) {
106
			throw new DnetInformationServiceException("document " + DatabaseUtils.composeResourceId(kind, type, fileName) + " not found");
107
		} else {
108
			return res;
109
		}
116 110

  
117
		return res;
118

  
119 111
	}
120 112

  
121
	@RequestMapping(value = "profile/{id}", method = RequestMethod.POST)
122
	public boolean updateProfile(@PathVariable("id") final String resId, @RequestBody final String profile) throws DnetInformationServiceException {
113
	@RequestMapping(value = "profile/{kind}/{type}/{fileName}", method = RequestMethod.POST)
114
	public boolean updateProfile(@PathVariable("kind") final String kind,
115
			@PathVariable("type") final String type,
116
			@PathVariable("fileName") final String fileName,
117
			@RequestBody final String profile) throws DnetInformationServiceException {
123 118
		try {
124
			final String fileName = DatabaseUtils.getFileName(resId);
125
			final String fileColl = DatabaseUtils.ROOT_COLLECTION + "/" + DatabaseUtils.getCollectionName(resId);
126
			final String oldProfileSrc = isStore.getXML(fileName, fileColl);
119
			final String resId = DatabaseUtils.composeResourceId(kind, type, fileName);
127 120

  
121
			final String coll = DatabaseUtils.getCollection(kind, type);
122

  
123
			final String oldProfileSrc = isStore.getXML(fileName, coll);
124

  
128 125
			if (oldProfileSrc == null) { throw new DnetInformationServiceException("cannot update a non existing profile " + resId); }
129 126

  
130 127
			final Document doc = (new SAXReader()).read(new StringReader(profile));
131
			final String coll = DatabaseUtils.getCollectionPath(doc);
128

  
132 129
			final String date = new DateUtils(new Date()).getDateAsISO8601String();
133 130

  
134
			final String type = doc.valueOf("/profile/type").trim();
135 131
			doc.selectSingleNode("/profile/id").setText(resId);
136 132
			doc.selectSingleNode("/profile/date").setText(date);
137 133

  
138 134
			final String finalDoc = doc.asXML();
139 135

  
140 136
			if (profileValidator.isValid(type, finalDoc)) {
141
				isStore.insertXML(fileName, DatabaseUtils.ROOT_COLLECTION + "/" + coll, finalDoc);
137
				isStore.insertXML(fileName, coll, finalDoc);
142 138
				return true;
143 139
			} else {
144 140
				throw new DnetInformationServiceException("Invalid profile");
......
148 144
		}
149 145
	}
150 146

  
147
	@RequestMapping(value = "profile/{kind}/{type}/{fileName}", method = RequestMethod.DELETE)
148
	public boolean deleteProfile(@PathVariable("kind") final String kind,
149
			@PathVariable("type") final String type,
150
			@PathVariable("fileName") final String fileName) throws DnetInformationServiceException {
151

  
152
		final boolean res = isStore.deleteXML(fileName, DatabaseUtils.getCollection(kind, type));
153

  
154
		if (!res) { throw new DnetInformationServiceException("document " + DatabaseUtils.composeResourceId(kind, type, fileName) + " not found"); }
155

  
156
		return true;
157
	}
158

  
151 159
	@RequestMapping("xupdate")
152 160
	public boolean executeXUpdate(@RequestParam("q") final String xquery) throws DnetInformationServiceException {
153 161
		return isStore.executeXUpdate(xquery);

Also available in: Unified diff