1 13 package com.tonbeller.jpivot.navigator.hierarchy; 14 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.apache.log4j.Logger; 21 22 import com.tonbeller.jpivot.olap.model.Dimension; 23 import com.tonbeller.jpivot.olap.model.Hierarchy; 24 import com.tonbeller.jpivot.olap.model.Member; 25 import com.tonbeller.jpivot.olap.model.OlapUtils; 26 import com.tonbeller.jpivot.olap.navi.ChangeSlicer; 27 import com.tonbeller.jpivot.olap.navi.PlaceMembersOnAxes; 28 import com.tonbeller.wcf.catedit.Item; 29 import com.tonbeller.wcf.controller.RequestContext; 30 import com.tonbeller.wcf.controller.RequestListener; 31 import com.tonbeller.wcf.utils.DomUtils; 32 33 42 public class HierarchyItem implements Item, RequestListener, Comparable { 43 44 private static final Logger logger = Logger.getLogger(HierarchyItem.class); 45 46 private HierarchyNavigator navigator; 48 49 private AbstractCategory category; 51 52 private Hierarchy hierarchy; 54 55 private List axisSelection; 57 private boolean axisSelectionDirty; 58 59 private List slicerSelection; 61 private boolean slicerSelectionDirty; 62 63 private List deleted = new ArrayList (); 65 66 private Object expression; 68 69 private String id = DomUtils.randomId(); 70 71 private Dimension dimension; 72 73 public String getId() { 74 return id; 75 } 76 77 public HierarchyItem(AbstractCategory category, Hierarchy hierarchy) { 78 this.category = category; 79 this.hierarchy = hierarchy; 80 this.dimension = hierarchy.getDimension(); 81 this.navigator = category.getNavigator(); 82 navigator.getTempDispatcher().addRequestListener(id, null, this); 83 } 84 85 void initializeSlicerSelection() { 86 ChangeSlicer slicerExtension = navigator.getSlicerExtension(); 87 slicerSelection = new ArrayList (); 88 if (slicerExtension != null) { 89 Member[] members = slicerExtension.getSlicer(); 90 loop : for (int i = 0; i < members.length; i++) { 91 if (members[i].getLevel().getHierarchy().equals(hierarchy)) { 92 slicerSelection.add(members[i]); 93 break loop; 94 } 95 } 96 } 97 } 98 99 void initializeAxisSelection() { 100 PlaceMembersOnAxes memberExtension = navigator.getMemberExtension(); 101 axisSelection = new ArrayList (); 102 if (memberExtension != null) { 103 List members = memberExtension.findVisibleMembers(hierarchy); 104 axisSelection.addAll(members); 105 } 106 } 107 108 private void clear() { 109 axisSelection = null; 110 slicerSelection = null; 111 deleted.clear(); 112 expression = null; 113 } 114 115 119 public void setCategory(AbstractCategory category) { 120 this.category = category; 121 } 122 123 public AbstractCategory getCategory() { 124 return category; 125 } 126 127 131 public Hierarchy getHierarchy() { 132 return hierarchy; 133 } 134 135 138 public Dimension getDimension() { 139 return dimension; 140 } 141 142 public String getLabel() { 143 return hierarchy.getLabel(); 144 } 145 146 149 public void request(RequestContext context) throws Exception { 150 category.itemClicked(context, this); 151 } 152 153 157 public List getAxisSelection() { 158 if (axisSelection == null) 159 initializeAxisSelection(); 160 return axisSelection; 161 } 162 163 167 public List getSlicerSelection() { 168 if (slicerSelection == null) 169 initializeSlicerSelection(); 170 return slicerSelection; 171 } 172 173 177 public void setAxisSelection(Collection selection) { 178 if (selection.equals(axisSelection)) { 179 return; 181 } 182 clear(); 183 updateHierarchy(selection); 184 if (axisSelection == null) 185 axisSelection = new ArrayList (); 186 else 187 axisSelection.clear(); 188 axisSelection.addAll(selection); 189 axisSelectionDirty = true; 190 category.setDirty(true); 191 expression = null; 192 } 193 194 198 public void setSlicerSelection(Collection selection) { 199 clear(); 200 updateHierarchy(selection); 201 if (slicerSelection == null) 202 slicerSelection = new ArrayList (); 203 else 204 slicerSelection.clear(); 205 slicerSelection.addAll(selection); 206 slicerSelectionDirty = true; 207 category.setDirty(true); 208 expression = null; 209 } 210 211 private void updateHierarchy(Collection selection) { 212 if (selection == null || selection.isEmpty()) 213 hierarchy = dimension.getHierarchies()[0]; 214 else { 215 Member m = (Member) selection.iterator().next(); 216 hierarchy = m.getLevel().getHierarchy(); 217 if (!hierarchy.getDimension().equals(dimension)) 218 logger.error("invalid dimension in " + hierarchy.getLabel()); 219 } 220 221 } 222 223 227 public boolean isAxisSelectionDirty() { 228 return axisSelectionDirty; 229 } 230 231 235 public boolean isSlicerSelectionDirty() { 236 return slicerSelectionDirty; 237 } 238 239 243 public void setAxisSelectionDirty(boolean axisSelectionDirty) { 244 this.axisSelectionDirty = axisSelectionDirty; 245 } 246 247 251 public void setSlicerSelectionDirty(boolean slicerSelectionDirty) { 252 this.slicerSelectionDirty = slicerSelectionDirty; 253 } 254 255 public void setSelection(Collection selection) { 256 category.setSelection(this, selection); 257 } 258 263 public String validateSelection(Collection selection) { 264 return category.validateSelection(this, selection); 265 } 266 267 270 public int compareTo(Object arg) { 271 HierarchyItem that = (HierarchyItem) arg; 272 return this.hierarchy.getLabel().compareTo(that.getHierarchy().getLabel()); 273 } 274 275 278 public void removeFromSelection(Set deleted) { 279 if (axisSelection != null) 280 axisSelection.removeAll(deleted); 281 if (slicerSelection != null) 282 slicerSelection.removeAll(deleted); 283 } 284 285 289 public Collection getDeleted() { 290 return deleted; 291 } 292 293 297 public void setDeleted(Collection c) { 298 deleted.clear(); 299 deleted.addAll(c); 300 } 301 302 public Object getExpression() { 303 return expression; 304 } 305 306 public void setExpression(Object object) { 307 clear(); 308 expression = object; 309 category.setDirty(true); 310 } 311 312 public boolean isMovable() { 313 return !OlapUtils.isSingleRecord(hierarchy); 314 } 315 316 public boolean isClickable() { 317 return navigator.getHierarchyItemClickHandler() != null; 318 } 319 320 } 321 | Popular Tags |