Project

General

Profile

« Previous | Next » 

Revision 6149

take getLogRecords impl form CrudService and use in CNCoreImpl

View differences:

src/edu/ucsb/nceas/metacat/dataone/CNCoreImpl.java
26 26
import java.io.IOException;
27 27
import java.io.InputStream;
28 28
import java.sql.SQLException;
29
import java.util.Calendar;
29 30
import java.util.Date;
30 31
import java.util.HashMap;
32
import java.util.Vector;
31 33

  
32 34
import org.apache.log4j.Logger;
33 35
import org.dataone.cn.batch.utils.TypeMarshaller;
......
45 47
import org.dataone.service.types.Event;
46 48
import org.dataone.service.types.Identifier;
47 49
import org.dataone.service.types.Log;
50
import org.dataone.service.types.LogEntry;
48 51
import org.dataone.service.types.NodeList;
52
import org.dataone.service.types.NodeReference;
49 53
import org.dataone.service.types.ObjectFormat;
50 54
import org.dataone.service.types.ObjectFormatIdentifier;
51 55
import org.dataone.service.types.ObjectFormatList;
52 56
import org.dataone.service.types.Session;
57
import org.dataone.service.types.Subject;
53 58
import org.dataone.service.types.SystemMetadata;
54 59
import org.jibx.runtime.JiBXException;
55 60

  
......
163 168

  
164 169
    }
165 170

  
166
  @Override
167
  public Log getLogRecords(Session arg0, Date arg1, Date arg2,
168
    Event arg3) throws InvalidToken, InvalidRequest, ServiceFailure,
169
    NotAuthorized, NotImplemented {
170
  	  // TODO Auto-generated method stub
171
  	  return null;
172
  }
171
	@Override
172
	public Log getLogRecords(Session session, Date fromDate, Date toDate,
173
			Event event) throws InvalidToken, InvalidRequest, ServiceFailure,
174
			NotAuthorized, NotImplemented {
175

  
176
		Log log = new Log();
177
		Vector<LogEntry> logs = new Vector<LogEntry>();
178
		IdentifierManager im = IdentifierManager.getInstance();
179
		EventLog el = EventLog.getInstance();
180
		if (fromDate == null) {
181
			logMetacat.debug("setting fromdate from null");
182
			fromDate = new Date(1);
183
		}
184
		if (toDate == null) {
185
			logMetacat.debug("setting todate from null");
186
			toDate = new Date();
187
		}
188

  
189
		logMetacat.debug("fromDate: " + fromDate);
190
		logMetacat.debug("toDate: " + toDate);
191

  
192
		String report = el.getReport(null, null, null, null,
193
				new java.sql.Timestamp(fromDate.getTime()),
194
				new java.sql.Timestamp(toDate.getTime()), false);
195

  
196
		logMetacat.debug("report: " + report);
197

  
198
		String logEntry = "<logEntry>";
199
		String endLogEntry = "</logEntry>";
200
		int startIndex = 0;
201
		int foundIndex = report.indexOf(logEntry, startIndex);
202
		while (foundIndex != -1) {
203
			// parse out each entry
204
			int endEntryIndex = report.indexOf(endLogEntry, foundIndex);
205
			String entry = report.substring(foundIndex, endEntryIndex);
206
			logMetacat.debug("entry: " + entry);
207
			startIndex = endEntryIndex + endLogEntry.length();
208
			foundIndex = report.indexOf(logEntry, startIndex);
209

  
210
			String entryId = getLogEntryField("entryid", entry);
211
			String ipAddress = getLogEntryField("ipAddress", entry);
212
			String principal = getLogEntryField("principal", entry);
213
			String docid = getLogEntryField("docid", entry);
214
			String eventS = getLogEntryField("event", entry);
215
			String dateLogged = getLogEntryField("dateLogged", entry);
216

  
217
			LogEntry le = new LogEntry();
218

  
219
			Event e = Event.convert(eventS);
220
			if (e == null) { // skip any events that are not Dataone Crud events
221
				continue;
222
			}
223
			le.setEvent(e);
224
			Identifier entryid = new Identifier();
225
			entryid.setValue(entryId);
226
			le.setEntryId(entryid);
227
			Identifier identifier = new Identifier();
228
			try {
229
				logMetacat.debug("converting docid '" + docid + "' to a guid.");
230
				if (docid == null || docid.trim().equals("") || docid.trim().equals("null")) {
231
					continue;
232
				}
233
				docid = docid.substring(0, docid.lastIndexOf("."));
234
				identifier.setValue(im.getGUID(docid, im.getLatestRevForLocalId(docid)));
235
			} catch (Exception ex) { 
236
				// try to get the guid, if that doesn't
237
				// work, just use the local id
238
				// throw new ServiceFailure("1030",
239
				// "Error getting guid for localId " +
240
				// docid + ": " + ex.getMessage());\
241

  
242
				// skip it if the guid can't be found
243
				continue;
244
			}
245

  
246
			le.setIdentifier(identifier);
247
			le.setIpAddress(ipAddress);
248
			Calendar c = Calendar.getInstance();
249
			String year = dateLogged.substring(0, 4);
250
			String month = dateLogged.substring(5, 7);
251
			String date = dateLogged.substring(8, 10);
252
			logMetacat.debug("year: " + year + " month: " + month + " day: " + date);
253
			c.set(new Integer(year).intValue(), new Integer(month).intValue(),
254
					new Integer(date).intValue());
255
			Date logDate = c.getTime();
256
			le.setDateLogged(logDate);
257
			NodeReference memberNode = new NodeReference();
258
			memberNode.setValue(ipAddress);
259
			le.setMemberNode(memberNode);
260
			Subject princ = new Subject();
261
			princ.setValue(principal);
262
			le.setSubject(princ);
263
			le.setUserAgent("metacat/RESTService");
264

  
265
			if (event == null) {
266
				logs.add(le);
267
			}
268

  
269
			if (event != null
270
					&& e.toString().toLowerCase().trim().equals(
271
							event.toString().toLowerCase().trim())) {
272
				logs.add(le);
273
			}
274
		}
275

  
276
		log.setLogEntryList(logs);
277
		logMetacat.info("getLogRecords");
278
		return log;
279
	}
280

  
281
	/**
282
	 * parse a logEntry and get the relevant field from it
283
	 * 
284
	 * @param fieldname
285
	 * @param entry
286
	 * @return
287
	 */
288
	private String getLogEntryField(String fieldname, String entry) {
289
		String begin = "<" + fieldname + ">";
290
		String end = "</" + fieldname + ">";
291
		// logMetacat.debug("looking for " + begin + " and " + end +
292
		// " in entry " + entry);
293
		String s = entry.substring(entry.indexOf(begin) + begin.length(), entry
294
				.indexOf(end));
295
		logMetacat.debug("entry " + fieldname + " : " + s);
296
		return s;
297
	}
173 298
  
174 299
  /**
175 300
   * Return the object format based on the given object format identifier

Also available in: Unified diff