Today we will share you sample program that load the whole dimension tree from Oracle Endeca MDEX. This program is using presentation API to make dimension search. Dimension search result is organized into a tree structure using custom data structure. This program is connecting to MDEX in the localhost running on port 15000. This program is tested against Oracle Endeca Discover Electronics reference application.
Oracle Endeca Dimension Search Client.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.ourownjava.endeca; import com.endeca.navigation.DimLocation; import com.endeca.navigation.DimLocationList; import com.endeca.navigation.DimVal; import com.endeca.navigation.DimValList; import com.endeca.navigation.DimensionSearchResult; import com.endeca.navigation.DimensionSearchResultGroup; import com.endeca.navigation.DimensionSearchResultGroupList; import com.endeca.navigation.ENEQuery; import com.endeca.navigation.ENEQueryException; import com.endeca.navigation.ENEQueryResults; import com.endeca.navigation.HttpENEConnection; /** * Oracle Endeca Dimension Search Example to load the dimension tree. * * @author ourownjava.com * */ public class OracleEndecaDimensionSearchClient { public DimensionTree loadDimensionTree() throws ENEQueryException { final DimensionTree dimensionTree = new DimensionTree(); final ENEQuery query = new ENEQuery(); query.setDimSearchTerms("*"); final ENEQueryResults results = search(query); final DimensionSearchResult dimensionSearchResult = results.getDimensionSearch(); final DimensionSearchResultGroupList dimensionSearchResultGroupList = dimensionSearchResult.getResults(); for (final Object dimensionSearchResultGroupElement : dimensionSearchResultGroupList) { final DimensionSearchResultGroup dimensionSearchResultGroup = (DimensionSearchResultGroup) dimensionSearchResultGroupElement; final DimValList roots = dimensionSearchResultGroup.getRoots(); if (!roots.isEmpty()) { final DimVal root = (DimVal) roots.get(0); for (int i = 0; i < dimensionSearchResultGroup.size(); i++) { final DimLocationList dimLocationList = (DimLocationList) dimensionSearchResultGroup.get(i); for (final Object dimLocationElement : dimLocationList) { final DimLocation dimLocation = (DimLocation) dimLocationElement; final DimValList ancestors = dimLocation.getAncestors(); final DimVal dimVal = dimLocation.getDimValue(); dimensionTree.addNodes(root, ancestors, dimVal); } } } } return dimensionTree; } private ENEQueryResults search(final ENEQuery eneQuery) throws ENEQueryException { return new HttpENEConnection("localhost", "15000").query(eneQuery); } public static void main(String[] args) throws ENEQueryException { System.out.println(new OracleEndecaDimensionSearchClient().loadDimensionTree()); } } |
DimensionTree Bean to organize dimensions into a tree structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
package com.ourownjava.endeca; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import com.endeca.navigation.DimVal; import com.endeca.navigation.DimValList; /** * * @author ourownjava.com * */ public class DimensionTree { private final Map<Long, Dimension> idRootDimensionMap = new LinkedHashMap<Long, Dimension>(); public void addNodes(final DimVal rootNode, final DimValList ancestors, final DimVal leaf) { Dimension root = idRootDimensionMap.get(rootNode.getId()); if (null == root) { root = new Dimension(rootNode.getId()); root.setName(rootNode.getName()); idRootDimensionMap.put(rootNode.getId(), root); } for (Object dimValElement : ancestors) { final DimVal dimVal = (DimVal) dimValElement; root = createNode(root, dimVal); } createNode(root, leaf); } private Dimension createNode(final Dimension parentDimension, final DimVal dimVal) { Dimension node = idRootDimensionMap.get(dimVal.getId()); if (null == node) { node = new Dimension(dimVal.getId()); node.setName(dimVal.getName()); parentDimension.addRefinement(node); idRootDimensionMap.put(dimVal.getId(), node); } return node; } @Override public String toString() { final StringBuilder builder = new StringBuilder(); for(final Dimension dimension : getRootDimension()){ builder.append(dimension); builder.append("\n"); } return builder.toString(); } public List<Dimension> getRootDimension() { final List<Dimension> dimensions = new ArrayList<Dimension>(); for (final Dimension dimensionBean : idRootDimensionMap.values()) { if (dimensionBean.hasRefinements()) { dimensions.add(dimensionBean); } } return dimensions; } } |
Dimension Bean for presentation purpose.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
package com.ourownjava.endeca; import java.util.ArrayList; import java.util.List; /** * * @author ourownjava.com * */ public class Dimension { public Dimension(final long id) { this.id = id; } private long id; private String name; private final List<Dimension> refinements = new ArrayList<Dimension>(); public long getId() { return id; } public void setId(final long id) { this.id = id; } public String getName() { return name; } public void setName(final String name) { this.name = name; } public List<Dimension> getRefinements() { return refinements; } public void addRefinement(final Dimension dimension) { this.refinements.add(dimension); } public boolean hasRefinements(){ return !refinements.isEmpty(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (int) (id ^ (id >>> 32)); return result; } @Override public boolean equals(final Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Dimension other = (Dimension) obj; if (id != other.id) return false; return true; } @Override public String toString() { return "Dimension [id=" + id + ", name=" + name + ", refinements=" + refinements + "]"; } } |
Program is written only for demonstration purpose. Tested against three level dimension tree. (Level -> Level2 -> Level3) This program may not be helpful if your dimension tree has got more than tree levels. Complete source code can be downloaded from here.
Pingback: Oracle Endeca — List all dimensions using presentation API.
I don’t know how to thank you enough!!
it works like a beauty.. The only customizations that I needed to do were
1. adding query.setDimSearchDimension(new Long(“100″)); to start from the node I wish to, and
2. overriding the toString function to return me an xml-like structure:
@Override
public String toString() {
return “”
+ refinements + “”;
}
hi Anirban, I am glad to know that it helped you.
Hi Adrian,
I tried your example..
query.setDimSearchTerms(“*”); this query returning zero results.. If I pass any one character like
query.setDimSearchTerms(“*a*”); then I am getting the results with letter “a”
But I need to get all the dimensions .. how to get it ? Pl help me to solve this issue.
I really appreciate your help.
Did you check if the Wildcard search is enabled?
Excellent piece of code and details associated, it worked very swiftly. With very small changes I would be able to use it as per my need.
Many Thanks!, Keep up the good work!
Hi Sanju,
Thanks you so much. Its people like you who keeps internet so useful. Thanks again.
Good work ! It was very useful even after 3 years ! Could you tell us know to get Level2 with just level1 and level 3 with level1+level2 ?
Thanks
kris