@NotNull
public ThinQuery drillacross(String queryName, @NotNull List<Integer> cellPosition,
@Nullable Map<String, List<String>> levels) {
try {
ThinQuery old = context.get(queryName).getOlapQuery();
Cube cub = olapDiscoverService.getNativeCube(old.getCube());
Query query = Fat.convert(old, cub);
CellSet cs = context.get(queryName).getOlapResult();
Set<Level> levelSet = new HashSet<Level>();
if (cs == null) {
throw new SaikuServiceException("Cannot drill across. Last CellSet empty");
}
for (int i = 0; i < cellPosition.size(); i++) {
List<Member> members = cs.getAxes().get(i).getPositions().get(cellPosition.get(i)).getMembers();
for (Member m : members) {
QueryHierarchy qh = query.getHierarchy(m.getHierarchy());
if (qh.getHierarchy().getDimension().getName().equals("Measures")) {
Measure measure = query.getMeasure(m.getName());
if (!query.getDetails().getMeasures().contains(measure)) {
query.getDetails().add(measure);
}
} else {
qh.clearSelection();
qh.clearFilters();
qh.clearSort();
query.moveHierarchy(qh, Axis.FILTER);
qh.includeMember(m);
levelSet.add(m.getLevel());
}
}
}
boolean clearedMeasures = false;
if (levels != null) {
for (String key : levels.keySet()) {
String dimensionName = key.split("###")[0];
if ("Measures".equals(dimensionName)) {
if (!clearedMeasures) {
query.getDetails().getMeasures().clear();
clearedMeasures = true;
}
for (String measureName : levels.get(key)) {
Measure measure = query.getMeasure(measureName);
if (measure != null) {
query.getDetails().add(measure);
} else {
for (Measure m : cub.getMeasures()) {
if (m.getUniqueName().equals(measureName)) {
query.getDetails().add(m);
}
}
}
}
continue;
}
String hierarchyName = key.split("###")[1];
Dimension d = cub.getDimensions().get(dimensionName);
Hierarchy h = d.getHierarchies().get(hierarchyName);
QueryHierarchy qh = query.getHierarchy(h);
for (Level l : h.getLevels()) {
for (String levelU : levels.get(key)) {
if (l.getUniqueName().equals(levelU) || l.getName().equals(levelU)) {
qh.includeLevel(l);
}
}
}
if (qh.getActiveQueryLevels().size() > 0) {
query.moveHierarchy(qh, Axis.ROWS);
}
}
}
if (query.getDetails().getMeasures().size() == 0) {
QueryHierarchy qh = query.getHierarchy("Measures");
Member defaultMeasure = qh.getHierarchy().getDefaultMember();
query.getDetails().add(query.getMeasure(defaultMeasure.getName()));
}
return Thin.convert(query, old.getCube());
} catch (Exception e) {
throw new SaikuServiceException("Error drilling across: " + queryName, e);
}
}