IQuery query = getIQuery(queryName);
CellSet cs = query.getCellset();
if (cs == null) {
throw new SaikuServiceException("Cannot get filter of result if last cellset is null");
}
CellDataSet result = OlapResultSetUtil.cellSet2Matrix(cs, query.getFormatter());
List<SimpleCubeElement> members = new ArrayList<SimpleCubeElement>();
Set<MetadataElement> mset = new HashSet<MetadataElement>();
Cube cube = query.getCube();
Hierarchy h = cube.getHierarchies().get(hierarchyName);
if (h == null) {
throw new Exception("Cannot find hierarchy in cube " + cube.getName() + " with name " + hierarchyName);
}
Dimension d = h.getDimension();
Level l = h.getLevels().get(levelName);
if (l == null) {
throw new Exception("Cannot find level in hierarchy " + h.getName() + " with name " + levelName);
}
SimpleCubeElement hierarchy = new SimpleCubeElement(h.getName(), h.getUniqueName(), h.getCaption());
SimpleCubeElement dimension = new SimpleCubeElement(d.getName(), d.getUniqueName(), d.getCaption());
Long start = new Date().getTime();
// try headers first
AbstractBaseCell[][] headers = result.getCellSetHeaders();
if (headers != null && headers.length > 0 && headers[0].length > 0) {
for (AbstractBaseCell[] header : headers) {
for (int k = 0; k < headers[0].length; k++) {
if (header[k] == null) {
continue;
}
MemberCell mc = (MemberCell) header[k];
if (mc.getUniqueName() != null) {
if (mc.getHierarchy().equals(hierarchy.getUniqueName()) && mc.getLevel().equals(l.getUniqueName())) {
String mu = mc.getUniqueName();
List<IdentifierSegment> memberList = IdentifierNode.parseIdentifier(mu).getSegmentList();
Member m = cube.lookupMember(memberList);
mset.add(m);
}
}
}
}
}
Long header = new Date().getTime();
if (mset.size() == 0) {
// try body next
AbstractBaseCell[][] body = result.getCellSetBody();
if (body != null && body.length > 0 && body[0].length > 0) {
for (AbstractBaseCell[] aBody : body) {
for (int k = 0; k < body[0].length; k++) {
if (aBody[k] == null) {
continue;
}
AbstractBaseCell ac = aBody[k];
if (ac instanceof DataCell) {
break;
}
if (ac instanceof MemberCell) {
MemberCell mc = (MemberCell) aBody[k];
if (mc.getUniqueName() != null) {
if (mc.getHierarchy().equals(hierarchy.getUniqueName()) && mc.getLevel().equals(l.getUniqueName())) {
String mu = mc.getUniqueName();
List<IdentifierSegment> memberList = IdentifierNode.parseIdentifier(mu).getSegmentList();
Member m = cube.lookupMember(memberList);
mset.add(m);
}
}
}
}
}
}
}
Long body = new Date().getTime();
// fallback - check inclusions (probably they are only on filter)
if (mset.size() == 0) {
//List<CellSetAxis> axes = new ArrayList<CellSetAxis>();
//axes.addAll(cs.getAxes());
//axes.add(cs.getFilterAxis());
//for (CellSetAxis axis : axes) {
//int posIndex = 0;
//for (Hierarchy he : axis.getAxisMetaData().getHierarchies()) {
//if (he.getName().equals(hierarchyName)) {
//if (hierarchy == null) {
//hierarchy = new SimpleCubeElement(he.getName(),
// he.getUniqueName(), he.getCaption());
//dimension = new SimpleCubeElement(d.getName(),
// d.getUniqueName(), d.getCaption());
//}
//if (he.getLevels().size() == 1) {
//break;
//}
//
//for (Position pos : axis.getPositions()) {
//Member m = pos.getMembers().get(posIndex);
//if (m.getLevel().getName().equals(levelName)) {
//mset.add(m);
//}
//}
//break;
//}
//posIndex++;
//}
//}
if (mset.size() == 0) {
QueryDimension qd = query.getDimension(dimensionName);
if (qd != null && qd.getAxis().getLocation() != null) {
for (Selection sel : qd.getInclusions()) {
if (sel.getRootElement() instanceof Member) {
Member m = (Member) sel.getRootElement();
if (m.getLevel().getName().equals(levelName)) {
mset.add(m);
}
}
}
}
}
}
Long end = new Date().getTime();
//System.out.println("Header Time: " + new Double(header - start).intValue());
//System.out.println("Body Time: " + new Double(body - header).intValue());
//System.out.println("Rest Time: " + new Double(end- body).intValue());
members = ObjectUtil.convert2Simple(mset);
Collections.sort(members, new SaikuUniqueNameComparator());
LOG.debug("Create Filters: Found members in the result or query: " + members.size());
return new SaikuFilter(filtername, null, dimension, hierarchy, members);
} catch (Exception e) {
throw new SaikuServiceException("Error getFilter:" + filtername + " for query: " + queryName, e);
}
}