summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoremah2013-04-02 16:35:29 (EDT)
committer emah2013-04-02 16:35:29 (EDT)
commit1d5fa6f30ab1611e6a33ad2f02249a578264a4fa (patch)
treed844c52a75f14db8d511641c139734656952309e
parent72ae833e81f26e5a1617e879cf5e6be9640ff30b (diff)
downloadorg.eclipse.lyo.rio-1d5fa6f30ab1611e6a33ad2f02249a578264a4fa.zip
org.eclipse.lyo.rio-1d5fa6f30ab1611e6a33ad2f02249a578264a4fa.tar.gz
org.eclipse.lyo.rio-1d5fa6f30ab1611e6a33ad2f02249a578264a4fa.tar.bz2
Bug 404745 - Document and cleanup TRS RIOrefs/changes/11/11611/2
Clean up the TRS utilities and provide clearer javadoc The cleanup in the TRS utilities will allow an adopter to more easily understand and reuse the TRS utilities as a starting point. Introduced IResourceUtil to return a list of known resources to use to prime Base objects. Change the launch configuration to use a workspace reference with the project name so that the launch config can be run from the menu bar without the project being selected. Change-Id: Ie0c73439181a72952a9a254a16d16db9bcf8fc54 Signed-off-by: emah <ernest@ca.ibm.com>
-rw-r--r--org.eclipse.lyo.rio.trs/Launch TRS Reference App.launch4
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/cm/PersistenceResourceUtil.java32
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/BaseResource.java13
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/ChangeRequestResource.java7
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/TRSResource.java23
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/BaseGeneric.java10
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/ChangeLogGeneric.java13
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/TRSGeneric.java6
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/IResourceUtil.java23
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSObject.java401
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSUtil.java106
-rw-r--r--org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSUtilHelper.java281
12 files changed, 529 insertions, 390 deletions
diff --git a/org.eclipse.lyo.rio.trs/Launch TRS Reference App.launch b/org.eclipse.lyo.rio.trs/Launch TRS Reference App.launch
index fa064aa..791ec3b 100644
--- a/org.eclipse.lyo.rio.trs/Launch TRS Reference App.launch
+++ b/org.eclipse.lyo.rio.trs/Launch TRS Reference App.launch
@@ -16,6 +16,6 @@
<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
<listEntry value="org.eclipse.debug.ui.launchGroup.debug"/>
</listAttribute>
-<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-XX:MaxPermSize=256m -Dderby.system.home=${project_loc} -Dorg.eclipse.lyo.oslc4j.disableRelativeURIs=false"/>
-<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${project_loc}"/>
+<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-XX:MaxPermSize=256m -Dorg.eclipse.lyo.oslc4j.disableRelativeURIs=false"/>
+<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${workspace_loc:org.eclipse.lyo.rio.trs}"/>
</launchConfiguration> \ No newline at end of file
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/cm/PersistenceResourceUtil.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/cm/PersistenceResourceUtil.java
new file mode 100644
index 0000000..1df4f6f
--- /dev/null
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/cm/PersistenceResourceUtil.java
@@ -0,0 +1,32 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Ernest Mah - Initial implementation
+ *******************************************************************************/
+
+package org.eclipse.lyo.rio.trs.cm;
+
+import org.eclipse.lyo.oslc4j.core.model.AbstractResource;
+import org.eclipse.lyo.rio.trs.util.IResourceUtil;
+
+public final class PersistenceResourceUtil
+ implements IResourceUtil
+{
+
+ @Override
+ public AbstractResource[] getAllResources() {
+ return Persistence.getAllChangeRequests();
+ }
+
+ public static PersistenceResourceUtil instance = new PersistenceResourceUtil();
+}
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/BaseResource.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/BaseResource.java
index 647f290..6bcb24f 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/BaseResource.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/BaseResource.java
@@ -35,6 +35,8 @@ import org.eclipse.lyo.core.trs.Base;
import org.eclipse.lyo.core.trs.TRSConstants;
import org.eclipse.lyo.oslc4j.core.annotation.OslcService;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
+import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
+import org.eclipse.lyo.rio.trs.util.TRSObject;
import org.eclipse.lyo.rio.trs.util.TRSUtil;
/**
@@ -67,7 +69,6 @@ public class BaseResource {
@Context
protected UriInfo uriInfo;
-
/**
* getBase() on the root URI performs a redirect to page 1 in this implementation to represent
* paged Base resources of the Tracked Resource Set
@@ -90,14 +91,18 @@ public class BaseResource {
@Path("{page}")
@Produces({ OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.TEXT_TURTLE, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Base getBasePage(@PathParam("page")Long page) throws URISyntaxException{
- TRSUtil.updateTRSResourceURI(uriInfo.getBaseUri());
+ TRSUtil.updateTRSResourceURI(PersistenceResourceUtil.instance, uriInfo.getBaseUri());
// from uri find out which Inner container to access...
URI requestURI = uriInfo.getRequestUri();
- if (TRSUtil.getTrsBase(requestURI).isEmpty() || !TRSUtil.getTrsBase(requestURI).containsKey(page))
+ TRSObject trsObject = TRSUtil.getTrsObject(PersistenceResourceUtil.instance, requestURI);
+
+ Base base = trsObject.getBasePage(page);
+
+ if (base == null)
throw new WebApplicationException(Status.NOT_FOUND);
- return TRSUtil.getTrsBase(requestURI).get(page);
+ return base;
}
}
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/ChangeRequestResource.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/ChangeRequestResource.java
index 0a973e1..2d19758 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/ChangeRequestResource.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/ChangeRequestResource.java
@@ -57,6 +57,7 @@ import org.eclipse.lyo.oslc4j.core.model.OslcConstants;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.oslc4j.core.model.ResponseInfoArray;
import org.eclipse.lyo.rio.trs.cm.ChangeRequest;
+import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
import org.eclipse.lyo.rio.trs.cm.Constants;
import org.eclipse.lyo.rio.trs.cm.Persistence;
import org.eclipse.lyo.rio.trs.util.TRSUtil;
@@ -81,7 +82,7 @@ public class ChangeRequestResource
URI baseURI = uriInfo.getBaseUri();
// Initialize the base before adding / deleting any resource.
- TRSUtil.initialize(baseURI);
+ TRSUtil.initialize(PersistenceResourceUtil.instance, baseURI);
ChangeRequest cr = Persistence.createChangeRequest(description,
title, filedAgainst);
@@ -323,7 +324,7 @@ public class ChangeRequestResource
{
URI baseURI = uriInfo.getBaseUri();
// Initialize the base before adding / deleting any resource.
- TRSUtil.initialize(baseURI);
+ TRSUtil.initialize(PersistenceResourceUtil.instance, baseURI);
final ChangeRequest changeRequest = Persistence.deleteChangeRequest(changeRequestId);
@@ -353,7 +354,7 @@ public class ChangeRequestResource
URI baseURI = uriInfo.getBaseUri();
// Initialize the base before adding / deleting any resource.
- TRSUtil.initialize(baseURI);
+ TRSUtil.initialize(PersistenceResourceUtil.instance, baseURI);
ChangeRequest changeRequest = null;
changeRequest = Persistence.persistChangeRequest(baseURI, Persistence.createChangeRequest("Unable to execute Apache Tomcat due to missing Java runtime environment (JRE).",
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/TRSResource.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/TRSResource.java
index 838eb35..72e87b3 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/TRSResource.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/resources/TRSResource.java
@@ -19,8 +19,8 @@ package org.eclipse.lyo.rio.trs.resources;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
+import java.util.TreeMap;
-import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
@@ -38,11 +38,14 @@ import javax.ws.rs.core.UriInfo;
import org.apache.wink.common.annotations.Workspace;
import org.eclipse.lyo.core.trs.AbstractChangeLog;
import org.eclipse.lyo.core.trs.ChangeEvent;
+import org.eclipse.lyo.core.trs.ChangeLog;
import org.eclipse.lyo.core.trs.EmptyChangeLog;
import org.eclipse.lyo.core.trs.TRSConstants;
import org.eclipse.lyo.core.trs.TrackedResourceSet;
import org.eclipse.lyo.oslc4j.core.annotation.OslcService;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
+import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
+import org.eclipse.lyo.rio.trs.util.TRSObject;
import org.eclipse.lyo.rio.trs.util.TRSUtil;
/**
@@ -73,7 +76,6 @@ import org.eclipse.lyo.rio.trs.util.TRSUtil;
public class TRSResource {
@Context
protected UriInfo uriInfo;
- @Context private HttpServletRequest httpServletRequest;
@Context private HttpServletResponse httpServletResponse;
/**
@@ -86,9 +88,10 @@ public class TRSResource {
// from uri find out which Inner container to access...
URI requestURI = uriInfo.getRequestUri();
+ TRSObject trsObject = TRSUtil.getTrsObject(PersistenceResourceUtil.instance, requestURI);
+
+ AbstractChangeLog changeLog = trsObject.getCurrentChangeLog();
- AbstractChangeLog changeLog = TRSUtil.getTrsChangelogMap(requestURI).isEmpty() ? new EmptyChangeLog() : TRSUtil.getCurrentChangelog(requestURI);
-
TrackedResourceSet set = new TrackedResourceSet();
set.setAbout(requestURI);
@@ -112,8 +115,11 @@ public class TRSResource {
public AbstractChangeLog getChangeLog() throws URISyntaxException{
// from uri find out which Inner container to access...
URI requestURI = uriInfo.getRequestUri();
+
+ TRSObject trsObject = TRSUtil.getTrsObject(PersistenceResourceUtil.instance, requestURI);
+
+ AbstractChangeLog changeLog = trsObject.getCurrentChangeLog();
- AbstractChangeLog changeLog = TRSUtil.getCurrentChangelog(requestURI);
return changeLog;
}
@@ -127,11 +133,12 @@ public class TRSResource {
public AbstractChangeLog getChangeLogPage(@PathParam("page") final Long page) throws URISyntaxException{
// from uri find out which Inner container to access...
URI requestURI = uriInfo.getRequestUri();
+ TRSObject trsObject = TRSUtil.getTrsObject(PersistenceResourceUtil.instance, requestURI);
- if (!TRSUtil.getTrsChangelogMap(requestURI).isEmpty() && !TRSUtil.getTrsChangelogMap(requestURI).containsKey(page))
- throw new WebApplicationException(Status.NOT_FOUND);
+ AbstractChangeLog changeLog = trsObject.getChangeLogPage(page);
- AbstractChangeLog changeLog = TRSUtil.getTrsChangelogMap(requestURI).isEmpty() ? new EmptyChangeLog() : TRSUtil.getTrsChangelogMap(requestURI).get(page);
+ if (changeLog == null)
+ throw new WebApplicationException(Status.NOT_FOUND);
return changeLog;
}
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/BaseGeneric.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/BaseGeneric.java
index 08b5a0c..4c869a6 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/BaseGeneric.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/BaseGeneric.java
@@ -34,6 +34,8 @@ import javax.ws.rs.core.Response.Status;
import org.eclipse.lyo.core.trs.Base;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JContext;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JMarshaller;
+import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
+import org.eclipse.lyo.rio.trs.util.TRSObject;
import org.eclipse.lyo.rio.trs.util.TRSUtil;
/**
@@ -92,13 +94,15 @@ public class BaseGeneric extends HttpServlet {
Object[] oArray;
try {
requestBase = new URI(request.getRequestURL().toString());
- TRSUtil.updateTRSResourceURI(requestBase);
+ TRSUtil.updateTRSResourceURI(PersistenceResourceUtil.instance, requestBase);
- if (TRSUtil.getTrsBase(requestBase).isEmpty() || !TRSUtil.getTrsBase(requestBase).containsKey(page))
+ TRSObject trsObject = TRSUtil.getTrsObject(PersistenceResourceUtil.instance, requestBase);
+ Base base = trsObject.getBasePage(page);
+ if (base == null)
throw new WebApplicationException(Status.NOT_FOUND);
List<Base> results = new ArrayList<Base>();
- results.add(TRSUtil.getTrsBase(requestBase).get(page));
+ results.add(base);
// return results;
OSLC4JContext context = OSLC4JContext.newInstance();
OSLC4JMarshaller marshaller = context.createMarshaller();
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/ChangeLogGeneric.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/ChangeLogGeneric.java
index 3023c77..fb72cdd 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/ChangeLogGeneric.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/ChangeLogGeneric.java
@@ -29,9 +29,10 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;
import org.eclipse.lyo.core.trs.AbstractChangeLog;
-import org.eclipse.lyo.core.trs.EmptyChangeLog;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JContext;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JMarshaller;
+import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
+import org.eclipse.lyo.rio.trs.util.TRSObject;
import org.eclipse.lyo.rio.trs.util.TRSUtil;
/**
@@ -53,7 +54,8 @@ public class ChangeLogGeneric extends HttpServlet {
URI requestBase;
try {
requestBase = new URI(request.getRequestURL().toString());
-
+ TRSObject trsObject = TRSUtil.getTrsObject(PersistenceResourceUtil.instance, requestBase);
+
path = request.getPathInfo();
if ( path != null){
// find the page number
@@ -64,14 +66,13 @@ public class ChangeLogGeneric extends HttpServlet {
page = (long) Integer.parseInt(arrofStr[1]);
}
- if (!TRSUtil.getTrsChangelogMap(requestBase).isEmpty() && !TRSUtil.getTrsChangelogMap(requestBase).containsKey(page))
+ changeLog = trsObject.getChangeLogPage(page);
+ if (changeLog == null)
throw new WebApplicationException(Status.NOT_FOUND);
-
- changeLog = TRSUtil.getTrsChangelogMap(requestBase).isEmpty() ? new EmptyChangeLog() : TRSUtil.getTrsChangelogMap(requestBase).get(page);
}
else
{
- changeLog = TRSUtil.getTrsChangelogMap(requestBase).isEmpty() ? new EmptyChangeLog() : TRSUtil.getCurrentChangelog(requestBase);
+ changeLog = trsObject.getCurrentChangeLog();
}
} catch (URISyntaxException e) {
e.printStackTrace();
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/TRSGeneric.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/TRSGeneric.java
index 31f0d0a..a39ff62 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/TRSGeneric.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/servlet/TRSGeneric.java
@@ -28,11 +28,12 @@ import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import org.eclipse.lyo.core.trs.AbstractChangeLog;
-import org.eclipse.lyo.core.trs.EmptyChangeLog;
import org.eclipse.lyo.core.trs.TRSConstants;
import org.eclipse.lyo.core.trs.TrackedResourceSet;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JContext;
import org.eclipse.lyo.core.utils.marshallers.OSLC4JMarshaller;
+import org.eclipse.lyo.rio.trs.cm.PersistenceResourceUtil;
+import org.eclipse.lyo.rio.trs.util.TRSObject;
import org.eclipse.lyo.rio.trs.util.TRSUtil;
/**
* RESTful service endpoints for returning the Tracked Resource Set at a given
@@ -82,7 +83,8 @@ public class TRSGeneric extends HttpServlet {
URI requestBase;
try {
requestBase = new URI(request.getRequestURL().toString());
- AbstractChangeLog changeLog = TRSUtil.getTrsChangelogMap(requestBase).isEmpty() ? new EmptyChangeLog() : TRSUtil.getCurrentChangelog(requestBase);
+ TRSObject trsObject = TRSUtil.getTrsObject(PersistenceResourceUtil.instance, requestBase);
+ AbstractChangeLog changeLog = trsObject.getCurrentChangeLog();
URI base = requestBase.resolve("trs/"+TRSConstants.TRS_TERM_BASE+"/");
set.setBase(base);
set.setChangeLog(changeLog);
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/IResourceUtil.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/IResourceUtil.java
new file mode 100644
index 0000000..e4393b8
--- /dev/null
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/IResourceUtil.java
@@ -0,0 +1,23 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Ernest Mah - Initial implementation
+ *******************************************************************************/
+
+package org.eclipse.lyo.rio.trs.util;
+
+import org.eclipse.lyo.oslc4j.core.model.AbstractResource;
+
+public interface IResourceUtil {
+ public AbstractResource[] getAllResources();
+}
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSObject.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSObject.java
new file mode 100644
index 0000000..cf3983f
--- /dev/null
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSObject.java
@@ -0,0 +1,401 @@
+/*******************************************************************************
+ * Copyright (c) 2013 IBM Corporation.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
+ *
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ *
+ * Ernest Mah - Initial implementation
+ *******************************************************************************/
+
+package org.eclipse.lyo.rio.trs.util;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.TreeMap;
+
+import org.eclipse.lyo.core.trs.AbstractChangeLog;
+import org.eclipse.lyo.core.trs.Base;
+import org.eclipse.lyo.core.trs.ChangeEvent;
+import org.eclipse.lyo.core.trs.ChangeLog;
+import org.eclipse.lyo.core.trs.Creation;
+import org.eclipse.lyo.core.trs.Deletion;
+import org.eclipse.lyo.core.trs.EmptyChangeLog;
+import org.eclipse.lyo.core.trs.Modification;
+import org.eclipse.lyo.core.trs.TRSConstants;
+import org.eclipse.lyo.oslc4j.core.model.AbstractResource;
+
+/**
+ * @author ernest
+ *
+ * This class manages a set of objects that together comprise the
+ * resources of a TRS feed. The Base and ChangeLogs are paged in a
+ * TreeMap with the key being the page number. The Base TreeMap is
+ * initialized with a set of all known resources returned by
+ * IResourceUtil. Change events are added to the current change log page
+ * (which is the last ChangeLog object that was added) until the page is
+ * full.
+ *
+ */
+
+public class TRSObject {
+
+ private final TreeMap<Long, Base> trs_base_map = new TreeMap<Long, Base>();
+ private final TreeMap<Long, ChangeLog> trs_changelog_map = new TreeMap<Long, ChangeLog>();
+ private URI trs_uri = null;
+ private int trs_curr_changelog_size = 0;
+ private Long trs_curr_changelog_page = -1L;
+ private ChangeLog trs_curr_changelog = null;
+ private AbstractResource trs_prev_changelog = null;
+ private ChangeEvent last_change_event = null;
+ private ChangeEvent last_cut_off_event = null;
+ public boolean trs_base_initialized = false;
+ private IResourceUtil resourceUtil;
+ private int PAGE_SIZE = 3;
+
+ /**
+ * @param resourceUtil - IResourceUtil object capable of return an array of all the resources known to the
+ * server to prime the Base resources.
+ * @param trs_uri - uri root for where the trs endpoints begin. This is used for setting the urls for references
+ * to other resources in the TRS feed.
+ */
+ public TRSObject(IResourceUtil resourceUtil, URI trs_uri) {
+ super();
+ this.resourceUtil = resourceUtil;
+ this.trs_uri = trs_uri;
+ initialize();
+ }
+
+ /**
+ * This will initialize the paged Base resources with the array of known resources
+ * returned from the given IResourceUtil. At the very least, a single empty Base resource
+ * will be created. Page size is a fixed PAGE_SIZE, but boundaries can be defined by the
+ * implementation (size of the data, server load, etc)
+ */
+ private void initialize() {
+ synchronized (trs_base_map) {
+ trs_base_initialized = true;
+ AbstractResource[] abstractResources = resourceUtil.getAllResources();
+
+ // initialize TRS Base
+ int currentPageSize = 0;
+ Long currentPageNumber = 1L;
+ Base currentBase = null;
+
+ // if there are no known resources, then create an empty base object
+ if (abstractResources.length == 0) {
+ currentBase = createNewBase();
+ getTrsBaseMapInner().put(currentPageNumber, currentBase);
+ return;
+ }
+
+ // inventory of all CR resources
+ for (AbstractResource abstractResource : abstractResources) {
+
+ // If current size is zero, then create a new base page.
+ if (currentPageSize == 0) {
+ // Point the current Base to the previous Base page by URI before creating
+ // the new one
+ if (currentBase != null)
+ currentBase.setNextPage(getBaseResourceURI(currentPageNumber));
+ currentBase = createNewBase();
+ getTrsBaseMapInner().put(currentPageNumber, currentBase);
+ }
+
+ // add resource into TRS base
+ currentBase.getMembers().add(abstractResource.getAbout());
+ currentPageSize++;
+
+ // Have we reached the page boundary?
+ if (currentPageSize == PAGE_SIZE) {
+ currentPageSize = 0;
+ currentPageNumber++;
+ }
+ }
+ }
+ }
+
+ /**
+ * @return Return the current changelog. If there are no changes, then return the empty change log
+ */
+ public AbstractChangeLog getCurrentChangeLog() {
+ AbstractChangeLog changeLog = getTrsChangelogMapInner().isEmpty() ? new EmptyChangeLog() : trs_curr_changelog;
+ return changeLog;
+ }
+
+ /**
+ * @param page - page is the page number for the Base object being requested
+ * @return null is returned if page number does not exist in the paged Base resources
+ */
+ public Base getBasePage(Long page) {
+ // our base map should never be empty, but just in case
+ if (trs_base_map.isEmpty() || !trs_base_map.containsKey(page)) {
+ return null;
+ }
+ return trs_base_map.get(page);
+ }
+
+ /**
+ * @param page page is the page number for the ChangeLog object being requested
+ * @return null if there are no change logs or if the given page number does not exist in the paged ChangeLog resources
+ */
+ public ChangeLog getChangeLogPage(Long page) {
+ if (trs_changelog_map.isEmpty() || !trs_changelog_map.containsKey(page)) {
+ return null;
+ }
+ return trs_changelog_map.get(page);
+ }
+
+ /**
+ * insertEventTypeToChangeLog - Insert an event corresponding to the
+ * trsEvent for the resource located at
+ *
+ * @param trsEvent
+ * - One of TRS_TYPE_CREATION, TRS_TYPE_MODIFICATION or
+ * TRS_TYPE_DELETION
+ * @param resource
+ * - URI of the resource that has undergone the change identified
+ * by trsEvent
+ */
+ public void insertEventTypeToChangeLog(String trsEvent, URI resource) {
+ ChangeEvent event = null;
+ // increment the event number to maintain event order
+ int eventNumber = (last_change_event != null) ? last_change_event.getOrder() + 1 : 0;
+
+ if (trsEvent.equals(TRSConstants.TRS_TYPE_CREATION)) {
+ event = new Creation(getCurrentTimeStampURN(), resource,
+ eventNumber);
+ } else if (trsEvent.equals(TRSConstants.TRS_TYPE_MODIFICATION)) {
+ event = new Modification(getCurrentTimeStampURN(), resource,
+ eventNumber);
+ } else if (trsEvent.equals(TRSConstants.TRS_TYPE_DELETION)) {
+ event = new Deletion(getCurrentTimeStampURN(), resource,
+ eventNumber);
+ }
+
+ insertEventToPagedChangeLog(event, resource);
+
+ }
+
+ /**
+ * @param event ChangeEvent to insert into the change log
+ * @param resource URI of the resource that has undergone the change identified the ChangeEvent
+
+ */
+ private void insertEventToPagedChangeLog(ChangeEvent event, URI resource) {
+ synchronized (trs_changelog_map) {
+ // Create a change log entry if the current size is zero
+ if (trs_curr_changelog_size == 0) {
+ trs_curr_changelog_page++;
+
+ trs_prev_changelog = trs_curr_changelog;
+ trs_curr_changelog = new ChangeLog();
+ getTrsChangelogMapInner().put(trs_curr_changelog_page, trs_curr_changelog);
+ // The latest change log has an about URI with no page reference
+ trs_curr_changelog.setAbout(getTRSResourceURI("changelog"));
+
+ if (trs_curr_changelog_page > 0) {
+ // update the previous changelog's about URI to be the current change log number - 1.
+ // It used to be the most current change log so it had an about URI without a page number
+ URI previousURI = getTRSResourceURI("changelog/" + Long.toString(trs_curr_changelog_page - 1));
+ trs_prev_changelog.setAbout(previousURI);
+ // point the current change log to the previous change log URI
+ trs_curr_changelog.setPrevious(previousURI);
+ }
+ }
+
+ // add resource into current changelog
+ trs_curr_changelog.getChanges().add(0, event);
+ trs_curr_changelog_size++;
+ last_change_event = event;
+
+ // Have we reached the page boundary? If so, then set the size to zero
+ // so next event to come in will trigger the creation of another page
+ if (trs_curr_changelog_size == PAGE_SIZE) {
+ trs_curr_changelog_size = 0;
+ }
+ }
+ }
+
+ /**
+ * @param cutOffEvent ChangeEvent to set the base resource cutoff event to
+ */
+ private void setCutOffEventInner(ChangeEvent cutOffEvent) {
+ Collection<Base> baseValues = getTrsBaseMapInner().values();
+ for (Base base : baseValues) {
+ base.setCutoffEvent(cutOffEvent.getAbout());
+ }
+ }
+
+ public void modifyCutoffEventInner(ChangeEvent inCutOffEvent) {
+ synchronized (trs_changelog_map) {
+ List<ChangeEvent> ListofEvents2Compact = new ArrayList<ChangeEvent>();
+ boolean bNewCutOffFound = false;
+
+ // 1. Iterate change log entries if inCutOffEvent is found
+ for (long page = trs_curr_changelog_page; page >= 0; page--) {
+ ChangeLog aChangeLog = getTrsChangelogMapInner().get((long) page);
+ List<ChangeEvent> aListofevents = aChangeLog.getChanges();
+ for (ChangeEvent e : aListofevents) {
+ if (last_cut_off_event != null) {
+ if (e.equals(last_cut_off_event)) {
+ break;
+ }
+ }
+
+ if (bNewCutOffFound == false) {
+ if (e.equals(inCutOffEvent)) {
+ bNewCutOffFound = true;
+ ListofEvents2Compact.add(e);
+ }
+ } else {
+ // 1.a. Start adding each entry into ActionList till
+ // CurrentCutoffevent or end of list of Changelog is
+ // found.
+ ListofEvents2Compact.add(e);
+ }
+ }
+ }
+ // set last cutoff event for next iteration.
+ last_cut_off_event = inCutOffEvent;
+
+ // 1.b. take action for each entry in the ActionList in reverse
+ // order i.e. oldest change event first.
+ for (int ii = ListofEvents2Compact.size() - 1; ii >= 0; ii--) {
+ ChangeEvent e2 = ListofEvents2Compact.get(ii);
+ modifyBase(e2);
+ }
+ // 2. Set the cutOffEvent
+ setCutOffEventInner(inCutOffEvent);
+ }
+ }
+
+ /**
+ * @param uriAbout about URI for the change event being requested
+ * @return null if the change event with the given uri can not be found or the change event with the given uri
+ */
+ public ChangeEvent getChangeEventInner(String uriAbout) {
+ ChangeEvent result = null;
+
+ for (int page = 0; page <= trs_curr_changelog_page; page++) {
+ ChangeLog aChangeLog = getTrsChangelogMapInner().get((long) page);
+ List<ChangeEvent> aListofevents = aChangeLog.getChanges();
+ for (ChangeEvent e : aListofevents) {
+ if (uriAbout.equals(e.getAbout().toString())) {
+ result = e;
+ break;
+ }
+ }
+ }
+
+ return result;
+ }
+
+ private Base createNewBase() {
+ Base newBase = new Base();
+ newBase.setAbout(getBaseResourceURI(null));
+ try {
+ newBase.setCutoffEvent(new URI(TRSConstants.RDF_NIL));
+ newBase.setNextPage(new URI(TRSConstants.RDF_NIL));
+ } catch (URISyntaxException e) {
+ e.printStackTrace();
+ }
+ return newBase;
+ }
+
+ private TreeMap<Long, Base> getTrsBaseMapInner() {
+ return trs_base_map;
+ }
+
+ private TreeMap<Long, ChangeLog> getTrsChangelogMapInner() {
+ return trs_changelog_map;
+ }
+
+ private URI getBaseResourceURI(Long pageNumber) {
+ if (pageNumber == null)
+ return trs_uri.resolve("./" + TRSConstants.TRS_TERM_BASE + "/");
+ else
+ return trs_uri.resolve("./" + TRSConstants.TRS_TERM_BASE + "/" + pageNumber);
+ }
+
+ private URI getTRSResourceURI(String path) {
+ if (path == null)
+ return trs_uri;
+ else
+ return trs_uri.resolve(path);
+ }
+
+ /**
+ * This routine will update the base resources to include all resources up to the given change event.
+ * @param changeEvent change event process up to for inclusion the the base resource
+ */
+ private void modifyBase(ChangeEvent changeEvent) {
+ synchronized (trs_base_map) {
+ Collection<Base> baseValues = getTrsBaseMapInner().values();
+ // if Creation then add resource to base(last page).
+ if (changeEvent.getClass().equals(Creation.class)) {
+
+ int index = 0;
+ int size = baseValues.size();
+ Base currentBase = null;
+ for (Base base : baseValues) {
+ if (index == (baseValues.size() - 1)) {
+ // get the last base page
+ currentBase = base;
+ }
+ index++;
+ }
+
+ // add the resource corresponding to the current event to the base
+ if (currentBase != null) {
+ currentBase.getMembers().add(changeEvent.getChanged());
+ }
+ }
+ // for modification do nothing.
+
+ // for deletion we need to delete the resource from the base page
+ // (where it's currently exists).
+ if (changeEvent.getClass().equals(Deletion.class)) {
+ for (Base base : baseValues) {
+ List<URI> resources = base.getMembers();
+ for (URI uriRes : resources) {
+ // if we get the match delete the resource and exit.
+ if (uriRes.equals(changeEvent.getChanged())) {
+ base.getMembers().remove(changeEvent.getChanged());
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // the following two methods help generate a unique urn for a change event
+ private static URI getCurrentTimeStampURN() {
+ URI timestampURI = null;
+ try {
+ timestampURI = new URI("urn:urn-3:cm1.example.com:" + getCurrentTimeStamp());
+ } catch (URISyntaxException e) {
+ e.printStackTrace();
+ }
+ return timestampURI;
+ }
+
+ private static String getCurrentTimeStamp() {
+ Date currDate = new Date();
+ SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss.SS");
+ String currDateStr = dateFormatGmt.format(currDate);
+ return currDateStr;
+ }
+}
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSUtil.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSUtil.java
index d35796a..859b6f1 100644
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSUtil.java
+++ b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSUtil.java
@@ -17,21 +17,24 @@
package org.eclipse.lyo.rio.trs.util;
import java.net.URI;
-import java.net.URISyntaxException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
import java.util.TreeMap;
-import org.eclipse.lyo.core.trs.AbstractChangeLog;
-import org.eclipse.lyo.core.trs.Base;
import org.eclipse.lyo.core.trs.ChangeEvent;
-import org.eclipse.lyo.core.trs.ChangeLog;
-import org.eclipse.lyo.core.trs.Creation;
-import org.eclipse.lyo.core.trs.Deletion;
-import org.eclipse.lyo.core.trs.Modification;
-import org.eclipse.lyo.core.trs.TRSConstants;
+/**
+ * @author ernest
+ *
+ * This class is used to manage a set of TRSObject's. Typical applications would just use
+ * the TRSObject directly. The sole purpose of this class is to manage a set of TRSObjects
+ * that represent changes to the same type of resource. In this reference application we
+ * show surfacing a TRS feed through JAX-RS and then also through simple servlets.
+ * TRSUtil is needed to make sure the same change is propogated to both TRSObjects. The reason
+ * we need more than one is that the objects contained in the TRSObject have
+ * references to urls to access other resources in the same set (say several pages of base
+ * resources or several pages of change logs).
+ */
public class TRSUtil {
+ private final TreeMap<String, TRSObject> trs_object_map = new TreeMap<String, TRSObject>();
/**
* insertEventTypeToChangeLog - Insert an event corresponding to the trsEvent for the resource
@@ -40,34 +43,17 @@ public class TRSUtil {
* @param resource - URI of the resource that has undergone the change identified by trsEvent
*/
public static void insertEventTypeToChangeLog(String trsEvent, URI resource) {
- ChangeEvent event = null;
- // increment the event number to maintain event order
- int eventNumber = (TRSUtil.LATEST_EVENT != null) ? TRSUtil.LATEST_EVENT.getOrder() + 1 : 0;
-
- if (trsEvent.equals(TRSConstants.TRS_TYPE_CREATION)) {
- event = new Creation(getCurrentTimeStampURN(), resource, eventNumber);
- }
- else if (trsEvent.equals(TRSConstants.TRS_TYPE_MODIFICATION)) {
- event = new Modification(getCurrentTimeStampURN(), resource, eventNumber);
- }
- else if (trsEvent.equals(TRSConstants.TRS_TYPE_DELETION)) {
- event = new Deletion(getCurrentTimeStampURN(), resource, eventNumber);
- }
// add the event to each inner container helper obj representing JAXRS and generic implementation.
for (int i = 0 ; i <innerHelpr.length; i++ )
- innerHelpr[i].insertEventToPagedChangeLog(event, resource);
-
+ innerHelpr[i].insertEventTypeToChangeLog(trsEvent, resource);
}
// if required, this routine will update the URI
// and prime the base resources
- public static void initialize(URI baseURI) {
- updateTRSResourceURI(baseURI);
- // initialize each inner container helper obj representing JAXRS and generic implementation.
- for (int i = 0 ; i <innerHelpr.length; i++ )
- innerHelpr[i].initialize();
-
+ public static void initialize(IResourceUtil resourceUtil, URI baseURI) {
+ updateTRSResourceURI(resourceUtil, baseURI);
}
+
private static boolean IsGenericImplDesired(URI inURI){
String sUri = inURI.getPath();
if (sUri.contains(TRS_GENERIC_MARKER))
@@ -76,18 +62,18 @@ public class TRSUtil {
return false;
}
- public static TreeMap<Long, Base> getTrsBase(URI inURI) {
+ public static TRSObject getTrsObject(IResourceUtil resourceUtil, URI inURI) {
if (!TRS_URI_INITIALIZED) {
- initialize(inURI);
+ initialize(resourceUtil, inURI);
}
// from uri find out which Inner container to access...
if(!IsGenericImplDesired(inURI))
- return innerHelpr[0].getTrsBaseInner();
+ return innerHelpr[0];
else
- return innerHelpr[1].getTrsBaseInner();
- }
+ return innerHelpr[1];
+ }
- public static void updateTRSResourceURI(URI resource) {
+ public static void updateTRSResourceURI(IResourceUtil resourceUtil, URI resource) {
if (!TRS_URI_INITIALIZED) {
String sPath = resource.getPath();
String sContext = null;
@@ -100,7 +86,6 @@ public class TRSUtil {
TRS_URI_INITIALIZED = true;
// initialize both inner container helper obj representing JAXRS and generic implementation.
for (int i = 0 ; i <innerHelpr.length; i++ ){
- innerHelpr[i] = new TRSUtilHelper();
URI trs_Uri;
if (i == 0){
trs_Uri = resource.resolve("/" + sContext + TRS_URI_PATH);
@@ -108,59 +93,19 @@ public class TRSUtil {
else{
trs_Uri = resource.resolve("/" + sContext + TRS_URI_PATH2);
}
- innerHelpr[i].set_TRS_URI(trs_Uri);
+ innerHelpr[i] = new TRSObject(resourceUtil, trs_Uri);
}
}
}
- public static TreeMap<Long, ChangeLog> getTrsChangelogMap(URI inURI) {
- if (!TRS_URI_INITIALIZED) {
- initialize(inURI);
- }
- // from uri find out which Inner container to access...
- if(!IsGenericImplDesired(inURI))
- return innerHelpr[0].getTrsChangelogMapInner();
- else
- return innerHelpr[1].getTrsChangelogMapInner();
- }
-
- public static AbstractChangeLog getCurrentChangelog(URI inURI) {
- if (!TRS_URI_INITIALIZED) {
- initialize(inURI);
- }
- // from uri find out which Inner container to access...
- if(!IsGenericImplDesired(inURI))
- return innerHelpr[0].getCurrentChangeLogInner();
- else
- return innerHelpr[1].getCurrentChangeLogInner();
- }
-
- public static final int PAGE_SIZE = 3;
public final static String TRS_URI_PATH = "/rest/trs/";
public final static String TRS_URI_PATH2 = "/restx/trs/";
public final static String TRS_GENERIC_MARKER = "/restx/";
// inner container helper obj representing JAXRS and generic implementation.
- private static TRSUtilHelper[] innerHelpr = new TRSUtilHelper[2];
+ private static TRSObject[] innerHelpr = new TRSObject[2];
public static boolean TRS_URI_INITIALIZED = false;
- private static URI getCurrentTimeStampURN() {
- URI timestampURI = null;
- try {
- timestampURI = new URI("urn:urn-3:cm1.example.com:" + getCurrentTimeStamp());
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- return timestampURI;
- }
-
- private static String getCurrentTimeStamp() {
- Date currDate = new Date();
- SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss.SS");
- String currDateStr = dateFormatGmt.format(currDate);
- return currDateStr;
- }
-
public static void modifyCutoffEvent(ChangeEvent inCutOffEvent) {
for (int i = 0 ; i <innerHelpr.length; i++ )
innerHelpr[i].modifyCutoffEventInner(inCutOffEvent);
@@ -170,6 +115,5 @@ public class TRSUtil {
// since the same set of event is kept in both the helper's changelog; access the first helper.
return innerHelpr[0].getChangeEventInner(uriAbout);
}
- public static ChangeEvent LATEST_EVENT;
}
diff --git a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSUtilHelper.java b/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSUtilHelper.java
deleted file mode 100644
index de2d28c..0000000
--- a/org.eclipse.lyo.rio.trs/src/main/java/org/eclipse/lyo/rio/trs/util/TRSUtilHelper.java
+++ /dev/null
@@ -1,281 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2013 IBM Corporation.
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
- *
- * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
- * and the Eclipse Distribution License is available at
- * http://www.eclipse.org/org/documents/edl-v10.php.
- *
- * Contributors:
- *
- * Ernest Mah - Initial implementation
- *******************************************************************************/
-
-package org.eclipse.lyo.rio.trs.util;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.TreeMap;
-
-import org.eclipse.lyo.core.trs.AbstractChangeLog;
-import org.eclipse.lyo.core.trs.Base;
-import org.eclipse.lyo.core.trs.ChangeEvent;
-import org.eclipse.lyo.core.trs.ChangeLog;
-import org.eclipse.lyo.core.trs.Creation;
-import org.eclipse.lyo.core.trs.Deletion;
-import org.eclipse.lyo.core.trs.EmptyChangeLog;
-import org.eclipse.lyo.core.trs.TRSConstants;
-import org.eclipse.lyo.oslc4j.core.model.AbstractResource;
-import org.eclipse.lyo.rio.trs.cm.ChangeRequest;
-import org.eclipse.lyo.rio.trs.cm.Persistence;
-
-public class TRSUtilHelper {
-
- private final TreeMap<Long, Base> trs_base_map = new TreeMap<Long, Base>();
- private final TreeMap<Long, ChangeLog> trs_changelog_map = new TreeMap<Long, ChangeLog>();
- private URI trs_uri = null;
- private int trs_curr_changelog_size = 0;
- private Long trs_curr_changelog_page = -1L;
- private ChangeLog trs_curr_changelog = null;
- private AbstractResource trs_prev_changelog = null;
- private ChangeEvent last_cut_off_event = null;
- public boolean trs_base_initialized = false;
-
- public AbstractChangeLog getCurrentChangeLogInner(){
- AbstractChangeLog changeLog = getTrsChangelogMapInner().isEmpty() ? new EmptyChangeLog() : trs_curr_changelog;
- return changeLog;
- }
-
- public URI get_TRS_URI()
- {
- return trs_uri;
- }
-
- public void set_TRS_URI(URI inUri)
- {
- trs_uri = inUri;
- }
-
- private Base createNewBase() {
- Base newBase = new Base();
- newBase.setAbout(getBaseResourceURI(null));
- try {
- newBase.setCutoffEvent(new URI(TRSConstants.RDF_NIL));
- newBase.setNextPage(new URI(TRSConstants.RDF_NIL));
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- return newBase;
- }
-
- public void initialize() {
- synchronized (trs_base_map) {
- trs_base_initialized = true;
-
- // initialize TRS Base
- ChangeRequest[] changeRequests = Persistence.getAllChangeRequests();
- int currentPageSize = 0;
- Long currentPageNumber = 1L;
- Base currentBase = null;
-
- if (changeRequests.length == 0) {
- currentBase = createNewBase();
- getTrsBaseMapInner().put(currentPageNumber, currentBase);
- return;
- }
-
- // inventory of all CR resources
- for (ChangeRequest changeRequest : changeRequests) {
- // Create a new base entry
- if (currentPageSize == 0) {
- if (currentBase != null)
- currentBase.setNextPage(getBaseResourceURI(currentPageNumber));
- currentBase = createNewBase();
- getTrsBaseMapInner().put(currentPageNumber, currentBase);
- }
-
- // add resource into TRS base
- currentBase.getMembers().add(changeRequest.getAbout());
- currentPageSize++;
-
- // Have we reached the page boundary?
- if (currentPageSize == TRSUtil.PAGE_SIZE) {
- currentPageSize = 0;
- currentPageNumber++;
- }
- }
- }
- }
- public TreeMap<Long, Base> getTrsBaseInner() {
- if (!trs_base_initialized) {
- initialize();
- }
- return getTrsBaseMapInner();
- }
-
- private TreeMap<Long, Base> getTrsBaseMapInner() {
- return trs_base_map;
- }
-
- public TreeMap<Long, ChangeLog> getTrsChangelogMapInner() {
- return trs_changelog_map;
- }
-
- private URI getBaseResourceURI(Long pageNumber) {
- if (pageNumber == null)
- return trs_uri.resolve("./" + TRSConstants.TRS_TERM_BASE+"/");
- else
- return trs_uri.resolve("./" + TRSConstants.TRS_TERM_BASE+"/"+pageNumber);
- }
-
- public void insertEventToPagedChangeLog(ChangeEvent event, URI resource) {
- synchronized (trs_changelog_map) {
- TRSUtil.updateTRSResourceURI(resource);
-
- // Create a new base entry
- if (trs_curr_changelog_size == 0) {
- trs_curr_changelog_page++;
-
- trs_prev_changelog = trs_curr_changelog;
- trs_curr_changelog = new ChangeLog();
- getTrsChangelogMapInner().put(trs_curr_changelog_page, trs_curr_changelog);
- trs_curr_changelog.setAbout(getTRSResourceURI("changelog"));
-
- if (trs_curr_changelog_page > 0) {
- URI previousURI = getTRSResourceURI("changelog/"+Long.toString(trs_curr_changelog_page-1));
- trs_curr_changelog.setPrevious(previousURI);
- trs_prev_changelog.setAbout(previousURI);
- }
- }
-
- // add resource into current changelog
- trs_curr_changelog.getChanges().add(0, event);
- trs_curr_changelog_size++;
- TRSUtil.LATEST_EVENT = event;
-
- // Have we reached the page boundary?
- if (trs_curr_changelog_size == Persistence.PAGE_SIZE) {
- trs_curr_changelog_size = 0;
- }
- }
- }
-
- private URI getTRSResourceURI(String path) {
- if (path == null)
- return trs_uri;
- else
- return trs_uri.resolve(path);
- }
-
- public void setCutOffEventInner(ChangeEvent cutOffEvent) {
- Collection<Base> baseValues = getTrsBaseMapInner().values();
- for (Base base : baseValues) {
- base.setCutoffEvent(cutOffEvent.getAbout());
- }
- }
-
- public void modifyCutoffEventInner(ChangeEvent inCutOffEvent) {
- synchronized (trs_changelog_map) {
- List<ChangeEvent> ListofEvents2Compact = new ArrayList<ChangeEvent>();
- boolean bNewCutOffFound = false;
-
- // 1. Iterate change log entries if inCutOffEvent is found
- for (long page = trs_curr_changelog_page; page >= 0 ; page-- ) {
- ChangeLog aChangeLog = getTrsChangelogMapInner().get((long) page);
- List<ChangeEvent> aListofevents = aChangeLog.getChanges();
- for(ChangeEvent e : aListofevents) {
- if (last_cut_off_event != null) {
- if (e.equals(last_cut_off_event)) {
- break;
- }
- }
-
- if (bNewCutOffFound == false) {
- if (e.equals(inCutOffEvent)) {
- bNewCutOffFound = true;
- ListofEvents2Compact.add(e);
- }
- }
- else {
- // 1.a. Start adding each entry into ActionList till
- // CurrentCutoffevent or end of list of Changelog is found.
- ListofEvents2Compact.add(e);
- }
- }
- }
- // set last cutoff event for next iteration.
- last_cut_off_event = inCutOffEvent;
-
- // 1.b. take action for each entry in the ActionList in reverse order i.e. oldest change event first.
- for (int ii = ListofEvents2Compact.size()-1; ii >= 0; ii--) {
- ChangeEvent e2 = ListofEvents2Compact.get(ii);
- modifyBase(e2);
- }
- // 2. Set the cutOffEvent
- setCutOffEventInner(inCutOffEvent);
- }
- }
-
- private void modifyBase(ChangeEvent e) {
- synchronized (trs_base_map) {
- Collection<Base> baseValues = getTrsBaseMapInner().values();
- // if Creation then add resource to base(last page).
- if (e.getClass().equals(Creation.class)) {
-
- int index = 0;
- int size = baseValues.size();
- Base currentBase = null;
- for (Base base : baseValues) {
- if (index == (baseValues.size()-1)) {
- // get the last base page
- currentBase = base;
- }
- index++;
- }
-
- // add the resource corresponding to the current event to the base
- if (currentBase != null) {
- currentBase.getMembers().add(e.getChanged());
- }
- }
- // for modification do nothing.
-
- // for deletion we need to delete the resource from the base page (where it's currently exists).
- if (e.getClass().equals(Deletion.class)) {
- for (Base base : baseValues) {
- List<URI> resources = base.getMembers();
- for (URI uriRes : resources) {
- // if we get the match delete the resource and exit.
- if (uriRes.equals(e.getChanged())) {
- base.getMembers().remove(e.getChanged());
- break;
- }
- }
- }
- }
- }
- }
-
- public ChangeEvent getChangeEventInner (String uriAbout) {
- ChangeEvent result = null;
-
- for (int page = 0; page <= trs_curr_changelog_page; page++) {
- ChangeLog aChangeLog = getTrsChangelogMapInner().get((long) page);
- List<ChangeEvent> aListofevents = aChangeLog.getChanges();
- for(ChangeEvent e: aListofevents) {
- if (uriAbout.equals(e.getAbout().toString())) {
- result = e;
- break;
- }
- }
- }
-
- return result;
- }
-}