Project

General

Profile

« Previous | Next » 

Revision 48139

integrated (hopefully) all required changes from dnet40

View differences:

OpenaireResultSubmitter.java
16 16
import eu.dnetlib.msro.openaireplus.utils.OafToIndexRecordFactory;
17 17
import eu.dnetlib.msro.rmi.MSROException;
18 18
import org.apache.commons.io.IOUtils;
19
import org.apache.commons.lang.StringUtils;
20 19
import org.apache.commons.lang.exception.ExceptionUtils;
21 20
import org.apache.commons.logging.Log;
22 21
import org.apache.commons.logging.LogFactory;
......
90 89
	private ClassPathResource findIndexDsInfo;
91 90

  
92 91
	@Deprecated
93
	@RequestMapping(value = {"/api/publications/feedJson", "/api/results/feedJson"}, method = RequestMethod.POST)
92
	@RequestMapping(value = { "/api/publications/feedJson", "/api/results/feedJson" }, method = RequestMethod.POST)
94 93
	public
95 94
	@ResponseBody
96
	boolean feedObjectJson(@RequestParam(value = "json", required = true) final String json,
95
	String feedObjectJson(@RequestParam(value = "json", required = true) final String json,
97 96
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws MSROException {
98 97
		final ResultEntry pub = new Gson().fromJson(json, ResultEntry.class);
99 98
		return feedObject(pub, commit);
100 99
	}
101 100

  
102

  
103
	@RequestMapping(value = {"/api/results/feedObject"} , method = RequestMethod.POST)
101
	@RequestMapping(value = { "/api/results/feedObject" }, method = RequestMethod.POST)
104 102
	public
105 103
	@ResponseBody
106 104
	String feedResult(@RequestBody final ResultEntry pub,
107 105
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit)
108 106
			throws MSROException {
109 107
		try {
110
			if(feedObject(pub, commit)) {
111
				return ResultEntry.calculateObjId(pub.getOriginalId(), pub.getCollectedFromId(), serviceLocator.getService(ISLookUpService.class));
112
			}
113
			else return null;
108
			return feedObject(pub, commit);
114 109
		} catch (final Throwable e) {
115 110
			throw new MSROException("Error adding publication: " + e.getMessage(), e);
116 111
		}
117 112
	}
118 113

  
119
	@RequestMapping(value = {"/api/publications/feedObject"} , method = RequestMethod.POST)
114
	@RequestMapping(value = { "/api/publications/feedObject" }, method = RequestMethod.POST)
120 115
	public
121 116
	@ResponseBody
122
	boolean feedObject(@RequestBody final ResultEntry pub,
117
	String feedObject(@RequestBody final ResultEntry pub,
123 118
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit)
124 119
			throws MSROException {
125 120

  
126
		checkParameters(pub);
127 121
		final List<IndexDsInfo> idxList;
128 122
		try {
129 123
			idxList = calculateCurrentIndexDsInfo();
130 124
			if (idxList == null || idxList.isEmpty()) {
131
				throw new MSROException("Cannot add result: " + pub.getOriginalId() + " : No public Search Service found");
125
				throw new MSROException("Cannot add result: " + pub + " : No public Search Service found");
132 126
			}
133 127
			if (idxList.size() > 1) log.warn("Found more than 1 public search service");
134 128
			final String oafRecord = pub.asOafRecord(velocityEngine, serviceLocator.getService(ISLookUpService.class), oafSchemaLocation);
......
147 141
				}
148 142
			}
149 143
			recentResultsQueue.add(oafRecord);
150
			return true;
144
			return pub.getOpenaireId();
151 145
		} catch (final Throwable e) {
152 146
			throw new MSROException("Error adding publication: " + e.getMessage(), e);
153 147
		}
154

  
155 148
	}
156 149

  
150
	@RequestMapping(value = "/api/result/{openaireId}", method = RequestMethod.DELETE)
151
	public
152
	@ResponseBody
153
	boolean deleteResultWithOpenaireId(
154
			@PathVariable(value = "openaireId") final String openaireId,
155
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws MSROException {
157 156

  
157
		return deleteResult(openaireId, commit);
158
	}
158 159

  
159 160
	@RequestMapping(value = "/api/results", method = RequestMethod.DELETE)
160 161
	public
161 162
	@ResponseBody
162
	boolean deleteResult(
163
	boolean deleteResultWithOriginalId(
163 164
			@RequestParam(value = "originalId", required = true) final String originalId,
164 165
			@RequestParam(value = "collectedFromId", required = true) final String collectedFromId,
165
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws MSROException {
166
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws Exception {
166 167

  
167
		return deleteResultPost(originalId, collectedFromId, commit);
168
		final String openaireId = ResultEntry.calculateOpenaireId(originalId, collectedFromId, serviceLocator.getService(ISLookUpService.class));
169
		return deleteResult(openaireId, commit);
168 170
	}
169 171

  
170
	@RequestMapping(value = {"/api/publications/deleteObject", "/api/results/delete"}, method = RequestMethod.POST)
172
	@Deprecated
173
	@RequestMapping(value = { "/api/publications/deleteObject", "/api/results/delete" }, method = RequestMethod.POST)
171 174
	public
172 175
	@ResponseBody
173 176
	boolean deleteResultPost(
174 177
			@RequestParam(value = "originalId", required = true) final String originalId,
175 178
			@RequestParam(value = "collectedFromId", required = true) final String collectedFromId,
176
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws MSROException {
179
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws Exception {
177 180

  
181
		final String openaireId = ResultEntry.calculateOpenaireId(originalId, collectedFromId, serviceLocator.getService(ISLookUpService.class));
182
		return deleteResult(openaireId, commit);
183
	}
184

  
185
	private boolean deleteResult(final String openaireId, final boolean commit) throws MSROException {
186

  
178 187
		final List<IndexDsInfo> idxList;
179 188
		try {
180 189
			idxList = calculateCurrentIndexDsInfo();
181 190

  
182 191
			if (idxList == null || idxList.isEmpty()) {
183
				throw new MSROException("Cannot delete result: " + originalId + " : No public Search Service found");
192
				throw new MSROException("Cannot delete result: " + openaireId + " : No public Search Service found");
184 193
			}
185 194
			if (idxList.size() > 1) log.warn("Found more than 1 public search service");
186 195

  
187
			final String objId = ResultEntry.calculateObjId(originalId, collectedFromId, serviceLocator.getService(ISLookUpService.class));
196
			//			final String objId = ResultEntry.calculateOpenaireId(originalId, collectedFromId, serviceLocator.getService(ISLookUpService.class));
188 197

  
189 198
			for (IndexDsInfo idx : idxList) {
190 199
				CloudIndexClient idxClient = null;
191 200
				try {
192 201
					idxClient = CloudIndexClientFactory.newIndexClient(idx.getIndexBaseUrl(), idx.getColl(), false);
193
					idxClient.remove(objId, commit);
194
					log.info("Deleted result with id: " + objId + " from: " + idx.getIndexBaseUrl());
202
					idxClient.remove(openaireId, commit);
203
					log.info("Deleted result with id: " + openaireId + " from: " + idx.getIndexBaseUrl());
195 204
				} catch (final Throwable e) {
196 205
					throw new MSROException("Error deleting publication: " + e.getMessage(), e);
197 206
				} finally {
......
200 209
					}
201 210
				}
202 211
			}
203
			recentResultsQueue.remove(objId);
212
			recentResultsQueue.remove(openaireId);
204 213
			return true;
205 214
		} catch (IOException | ISLookUpException e) {
206 215
			throw new MSROException("Error deleting publication: " + e.getMessage(), e);
......
216 225
		return new ErrorMessage(e);
217 226
	}
218 227

  
219
	private void checkParameters(ResultEntry pub) throws MSROException{
220
		if (StringUtils.isBlank(pub.getOriginalId())) { throw new MSROException("A required field is missing: originalId"); }
221
		if (StringUtils.isBlank(pub.getTitle())) { throw new MSROException("A required field is missing: title"); }
222
		if (StringUtils.isBlank(pub.getUrl())) { throw new MSROException("A required field is missing: url"); }
223
		if (StringUtils.isBlank(pub.getLicenseCode())) { throw new MSROException("A required field is missing: licenseCode"); }
224
		if (StringUtils.isBlank(pub.getResourceType())) { throw new MSROException("A required field is missing: resourceType"); }
225
		if (StringUtils.isBlank(pub.getCollectedFromId())) { throw new MSROException("A required field is missing: collectedFromId"); }
226
		if (StringUtils.isBlank(pub.getType())) { throw new MSROException("A required field is missing: type"); }
227
	}
228

  
229 228
	private List<IndexDsInfo> calculateCurrentIndexDsInfo() throws IOException, ISLookUpException {
230
		List<IndexDsInfo> list = Lists.newArrayList();
229
		final List<IndexDsInfo> list = Lists.newArrayList();
231 230

  
232 231
		final String queryUrl = IOUtils.toString(findSolrIndexUrl.getInputStream());
233 232
		final String queryDs = IOUtils.toString(findIndexDsInfo.getInputStream());
......
267 266

  
268 267
	}
269 268

  
270
}
269
}

Also available in: Unified diff