Skip to content

Commit

Permalink
Merge pull request #152 from dolfdijkstra/gst-foundation-12
Browse files Browse the repository at this point in the history
12.0.2 release
  • Loading branch information
Tony Field committed Mar 16, 2017
2 parents b2ff7f6 + 9ff177e commit 5dbf22e
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 161 deletions.
2 changes: 1 addition & 1 deletion gsf-build-tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>tools.gsf</groupId>
<artifactId>gsf-build-tools</artifactId>
<version>12.0.1</version>
<version>12.0.2</version>
<packaging>jar</packaging>
<name>Sites GSF: Build Tools</name>
<inceptionYear>2012</inceptionYear>
Expand Down
2 changes: 1 addition & 1 deletion gsf-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>tools.gsf</groupId>
<artifactId>gsf-parent</artifactId>
<version>12.0.1</version>
<version>12.0.2</version>
</parent>
<artifactId>gsf-core</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,13 @@
public abstract class SitePlanNavService<N extends AssetNode<N> & ConfigurableNode<N>> implements NavService<N, AssetId, AssetId> {

private static final Logger LOG = LoggerFactory.getLogger(SitePlanNavService.class);

private boolean _initialized;

private final ICS ics;
private final TemplateAssetAccess dao;
private final String sitename;
private final Long pubId;
private final Map<AssetId, List<N>> nodesById = new HashMap<>();

// Sure, we could join with PUBLICATION on the NAVIGATION_TREE_DUMP, but
Expand Down Expand Up @@ -145,6 +148,13 @@ private final Long _getPubId(String sitename) {
}
}

/**
* For a given (Page) AssetId, creates an object whose type matches this method's return type
* (Generic), which will be added to the nav structure produced by this NavService implementation.
*
* @param assetId The ID of the asset you want to create an AssetNode instance for
* @return An object whose type matches this method's return type (Generic type N)
*/
protected abstract N createAssetNode(AssetId assetId);

protected TemplateAssetAccess getTemplateAssetAccess() {
Expand All @@ -155,10 +165,6 @@ protected String getSitename() {
return this.sitename;
}

protected final ICS getIcs() {
return this.ics;
}

public SitePlanNavService(ICS ics, TemplateAssetAccess dao) {
this(ics, dao, null);
}
Expand All @@ -174,93 +180,103 @@ public SitePlanNavService(ICS ics, TemplateAssetAccess dao, String theSitename)
throw new IllegalStateException("Missing argument sitename. Nav structure cannot be built unless you specify the name of the site (publication) it is for.");
}
}
Long pubId = _getPubId(theSitename);
if (pubId == null) {
throw new IllegalStateException("Cannot determine pubid for site '" + theSitename + "'. Nav structure cannot be built unless you specify the name of the site (publication) it is for.");
Long aPubId = _getPubId(theSitename);
if (aPubId == null) {
throw new IllegalStateException("Cannot determine pubid for site '" + theSitename + "'. Nav structure cannot be built unless you specify the (valid) name of the site (publication) it is for.");
}

this.pubId = aPubId;
this.sitename = theSitename;
StatementParam params = NAVIGATION_TREE_DUMP.newParam();
params.setLong(0, pubId);

// read the site plan tree in one massive query
Map<Long, SitePlanTreeData> rowMap = new HashMap<>();
Map<Long, List<SitePlanTreeData>> childrenMap = new HashMap<>();

LOG.debug("Executing SitePlan query for gathering data for nav service...");

for (Row row : SqlHelper.select(ics, NAVIGATION_TREE_DUMP, params)) {
SitePlanTreeData nodeInfo = new SitePlanTreeData(row);
LOG.debug("Processing SitePlan row: {}", nodeInfo);
rowMap.put(nodeInfo.nid, nodeInfo);
LOG.debug("Added row {} to SitePlan rows map under key {}", nodeInfo, nodeInfo.nid);
List<SitePlanTreeData> children = childrenMap.get(nodeInfo.nparentid);
if (children == null) {
// Initialize the list of children for the current row's parent
children = new ArrayList<SitePlanTreeData>();
childrenMap.put(nodeInfo.nparentid, children);
}
// Add the current row to its parent's list of children
children.add(nodeInfo);
LOG.debug("Added SPT row {} to the list of children of SPT (parent) row {}. That list now looks like this: {}", nodeInfo, nodeInfo.nparentid, children);
}

// create Node objects
Map<Long, N> nidNodeMap = new HashMap<Long, N>();
for (long nid : rowMap.keySet()) {
LOG.debug("Will invoke createAssetNode for asset id {}", rowMap.get(nid).assetId);
N node = createAssetNode(rowMap.get(nid).assetId);
LOG.debug("AssetNode created for asset {}: {}", rowMap.get(nid).assetId, node);

// Log a dependency with every node (asset) we populate
LogDep.logDep(ics, node.getId());
LOG.debug("Logged dependency for asset {} inside nav service...", rowMap.get(nid).assetId);

nidNodeMap.put(nid, node);
LOG.debug("Added node {} to nodes map under key {}", node, nid);

// Stash for later. Probably won't have many duplicates so optimize
AssetId assetId = node.getId();
List<N> a1 = nodesById.get(assetId);
if (a1 == null) {
a1 = Stream.of(node).collect(Collectors.toList());
nodesById.put(assetId, a1);
} else {
a1.add(node);
}

LOG.debug("nodesById map: {}", nodesById);

}
}

// hook up parent-child relationships, starting from the list
// of nodes with a parent
for (long nparentid : childrenMap.keySet()) {
LOG.debug("Processing parent-child relationships for SitePlanTree row with nid = {}", nparentid);
N parent = nidNodeMap.get(nparentid);
if (parent != null) {
LOG.debug("AssetNode for nid {} is: {}", nparentid, parent);
List<SitePlanTreeData> children = childrenMap.get(nparentid);
if (children != null) {
LOG.debug("List of children for SPT whose nid = {} is: {}", nparentid, children);
for (SitePlanTreeData childRow : children) {
N child = nidNodeMap.get(childRow.nid);
if (child != null) {
parent.addChild(child);
child.setParent(parent);
LOG.debug("Bound together parent node {} and child node {} as per SPT entry {}", parent, child, childRow);
} else {
LOG.warn("There could be a problem here... we registered SPT row {} as a child of parent row {} but we did not instantiate a node for that child?", childRow, nparentid);
}
private synchronized void _initializeNavStructure() {
if (!this._initialized) {
StatementParam params = NAVIGATION_TREE_DUMP.newParam();
params.setLong(0, pubId);

// read the site plan tree in one massive query
Map<Long, SitePlanTreeData> rowMap = new HashMap<>();
Map<Long, List<SitePlanTreeData>> childrenMap = new HashMap<>();

LOG.debug("Executing SitePlan query for gathering data for nav service...");

for (Row row : SqlHelper.select(ics, NAVIGATION_TREE_DUMP, params)) {
SitePlanTreeData nodeInfo = new SitePlanTreeData(row);
LOG.debug("Processing SitePlan row: {}", nodeInfo);
rowMap.put(nodeInfo.nid, nodeInfo);
LOG.debug("Added row {} to SitePlan rows map under key {}", nodeInfo, nodeInfo.nid);
List<SitePlanTreeData> children = childrenMap.get(nodeInfo.nparentid);
if (children == null) {
// Initialize the list of children for the current row's parent
children = new ArrayList<SitePlanTreeData>();
childrenMap.put(nodeInfo.nparentid, children);
}
// Add the current row to its parent's list of children
children.add(nodeInfo);
LOG.debug("Added SPT row {} to the list of children of SPT (parent) row {}. That list now looks like this: {}", nodeInfo, nodeInfo.nparentid, children);
}

// create Node objects
Map<Long, N> nidNodeMap = new HashMap<Long, N>();
for (long nid : rowMap.keySet()) {
LOG.debug("Will invoke createAssetNode for asset id {}", rowMap.get(nid).assetId);
N node = createAssetNode(rowMap.get(nid).assetId);
LOG.debug("AssetNode created for asset {}: {}", rowMap.get(nid).assetId, node);

// Log a dependency with every node (asset) we populate
LogDep.logDep(ics, node.getId());
LOG.debug("Logged dependency for asset {} inside nav service...", rowMap.get(nid).assetId);

nidNodeMap.put(nid, node);
LOG.debug("Added node {} to nodes map under key {}", node, nid);

// Stash for later. Probably won't have many duplicates so optimize
AssetId assetId = node.getId();
List<N> a1 = nodesById.get(assetId);
if (a1 == null) {
a1 = Stream.of(node).collect(Collectors.toList());
nodesById.put(assetId, a1);
} else {
a1.add(node);
}

LOG.debug("nodesById map: {}", nodesById);

}

// hook up parent-child relationships, starting from the list
// of nodes with a parent
for (long nparentid : childrenMap.keySet()) {
LOG.debug("Processing parent-child relationships for SitePlanTree row with nid = {}", nparentid);
N parent = nidNodeMap.get(nparentid);
if (parent != null) {
LOG.debug("AssetNode for nid {} is: {}", nparentid, parent);
List<SitePlanTreeData> children = childrenMap.get(nparentid);
if (children != null) {
LOG.debug("List of children for SPT whose nid = {} is: {}", nparentid, children);
for (SitePlanTreeData childRow : children) {
N child = nidNodeMap.get(childRow.nid);
if (child != null) {
parent.addChild(child);
child.setParent(parent);
LOG.debug("Bound together parent node {} and child node {} as per SPT entry {}", parent, child, childRow);
} else {
LOG.warn("There could be a problem here... we registered SPT row {} as a child of parent row {} but we did not instantiate a node for that child?", childRow, nparentid);
}
}
} else {
LOG.warn("Not sure how we ended up with a a parent node in the children nodes map with no children whatsoever.");
}
} else {
LOG.warn("Not sure how we ended up with a a parent node in the children nodes map with no children whatsoever.");
}
} else {
LOG.warn("We have a list of children for SPT entry whose nid = {}. However, there is not a node matching that SPT entry's asset ({}). This is a bit weird, but also legitimate (for instance, the query excludes SiteNavigation assets)", nparentid, rowMap.get(nparentid));
}
}
}
} else {
LOG.warn("We have a list of children for SPT entry whose nid = {}. However, there is not a node matching that SPT entry's asset ({}). This is a bit weird, but also legitimate (for instance, the query excludes SiteNavigation assets)", nparentid, rowMap.get(nparentid));
}
}

this._initialized = true;
} else {
LOG.debug("Nav Structure had been already initialized for NavService instance {}", this);
}
}

private static class SitePlanTreeData {
final long nid;
Expand Down Expand Up @@ -291,6 +307,9 @@ public List<N> getNav(AssetId sitePlan) {
throw new IllegalArgumentException("Null param not allowed");
}

// Initialize the nav structure
_initializeNavStructure();

// find the requested structure
List<N> spNodes = nodesById.get(sitePlan);
if (spNodes == null) throw new IllegalArgumentException("Could not locate nav structure corresponding to "+sitePlan);
Expand All @@ -302,10 +321,12 @@ public List<N> getNav(AssetId sitePlan) {
}

public List<N> getBreadcrumb(AssetId id) {

if (id == null) {
throw new IllegalArgumentException("Cannot calculate breadcrumb of a null asset");
}

// Initialize the nav structure
_initializeNavStructure();

Collection<BreadcrumbCandidate<N>> breadcrumbs = new ArrayList<>();
LOG.debug("Obtaining all nodes whose ID matches {}", id);
Expand Down
2 changes: 1 addition & 1 deletion gsf-legacy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>tools.gsf</groupId>
<artifactId>gsf-parent</artifactId>
<version>12.0.1</version>
<version>12.0.2</version>
</parent>
<artifactId>gsf-legacy</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ public final class LightweightSitePlanNavService extends SitePlanNavService<MySa

private static final Logger LOG = LoggerFactory.getLogger(LightweightSitePlanNavService.class);

private ICS ics;

public LightweightSitePlanNavService(ICS ics, TemplateAssetAccess dao) {
super(ics, dao);
LOG.debug("Initialized instance of LightweightSitePlanNavService with: ics = {} / dao = {} / no sitename specified", ics, dao);
this.ics = ics;
LOG.debug("Initialized instance of LightweightSitePlanNavService with: ics = {} / dao = {} / no sitename specified (defaulting to {})", ics, dao, this.getSitename());
}

protected MySampleAssetNode createAssetNode(AssetId assetId) {
Expand All @@ -53,10 +56,10 @@ protected MySampleAssetNode createAssetNode(AssetId assetId) {
// even HashMap ;-) ).
LOG.debug("Starting LightweightSitePlanNavService.createAssetnode for asset id: {}", assetId);

// You can either instantiate the BuildersFactory here as per below OR
// you can have it passed onto this NavService implementation (for example:
// by obtaining it through your project-specific IcsBackedFactory implementation).
com.fatwire.assetapi.data.BuildersFactory buildersFactory = new DefaultBuildersFactory(this.getIcs());
// You can either instantiate the BuildersFactory here as per below OR you can
// have it passed onto this NavService's constructor (for example: by obtaining
// it through your project-specific IcsBackedFactory implementation).
com.fatwire.assetapi.data.BuildersFactory buildersFactory = new DefaultBuildersFactory(this.ics);

// You own 100% of your AssetNode implementation, so you can pass into its
// constructor anything you need for it to do what it has to do.
Expand Down
Loading

0 comments on commit 5dbf22e

Please sign in to comment.