1 30 package com.genimen.djeneric.web.renderers.tree; 31 32 import java.util.ArrayList ; 33 import java.util.HashMap ; 34 35 import org.w3c.dom.DOMException ; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.Element ; 38 39 import com.genimen.djeneric.language.Messages; 40 import com.genimen.djeneric.repository.DjAssociation; 41 import com.genimen.djeneric.repository.DjCursor; 42 import com.genimen.djeneric.repository.DjList; 43 import com.genimen.djeneric.repository.DjObject; 44 import com.genimen.djeneric.repository.DjOql; 45 import com.genimen.djeneric.repository.DjProperty; 46 import com.genimen.djeneric.repository.DjSession; 47 import com.genimen.djeneric.repository.exceptions.DjenericException; 48 import com.genimen.djeneric.repository.oql.core.ParseException; 49 import com.genimen.djeneric.structure.ExtentUsage; 50 import com.genimen.djeneric.structure.RelationUsage; 51 import com.genimen.djeneric.tools.specifier.Specifier; 52 import com.genimen.djeneric.tools.specifier.interfaces.DjenericObjectContainer; 53 54 public class WebFolder extends AbstractWebNode implements DjenericObjectContainer 55 { 56 public final static int MAX_OBJECTS_IN_FOLDER = 300; 57 protected boolean _allDisplayed = true; 58 protected boolean _filtered = false; 59 protected int[] _sortIndices = null; 60 protected DjList _objects = null; 61 protected DjOql _oql = null; 62 protected RelationUsage _via = null; 63 protected Boolean _canFilter = null; 64 65 public WebFolder(DjSession session) 66 { 67 super(session); 68 } 69 70 public void reload() throws DjenericException 71 { 72 clearLoadedObjects(); 73 load(); 74 setExpanded(true); 75 } 76 77 public void delete() throws Exception 78 { 79 } 81 82 87 public DjList getObjects() throws DjenericException 88 { 89 if (_objects != null) return _objects; 90 91 try 92 { 93 setStatusMessage(Messages.getString("global.Loading"), true); 94 95 initOql(); 96 97 _allDisplayed = true; 98 _objects = new DjList(); 99 100 ExtentUsage usage = getExtentUsage(); 101 _objects.setStoredTypeName(usage.getExtent()); 102 103 DjCursor cursor; 104 if (isRootFolder() || !isContentsLoadedViaRelation()) 105 { 106 if (_oql != null) cursor = getSession().getObjectsCursor(_oql, true); 107 else cursor = getSession().getObjectsCursor(usage.getExtent(), true); 108 } 109 else 110 { 111 DjAssociation assoc = getObject().getDetailAssociationByName(getVia().getRelation().getName(), 112 usage.getExtent()); 113 if (_oql != null) cursor = assoc.getObjectsCursor(_oql, true); 114 else cursor = assoc.getObjectsCursor(true); 115 } 116 117 try 118 { 119 int maxObjects = MAX_OBJECTS_IN_FOLDER; 120 DjObject obj; 121 while (((obj = cursor.getNext()) != null) && maxObjects-- > 0) 122 { 123 _objects.add(obj); 124 } 125 if (maxObjects < 0) 126 { 127 _allDisplayed = false; 128 } 129 } 130 finally 131 { 132 cursor.close(); 133 } 134 135 if (_sortIndices == null) _sortIndices = getExtentUsage().getExtent().getPropertySortIndices(); 136 _objects.sort(_sortIndices); 137 return _objects; 138 } 139 finally 140 { 141 setStatusMessage(Specifier.OK_MSG, true); 142 } 143 } 144 145 public boolean isContentsLoadedViaRelation() 146 { 147 return getVia() != null && getVia().getRelation() != null; 148 } 149 150 protected boolean isRootFolder() 151 { 152 return getVia() == null; 153 } 154 155 public String toString() 156 { 157 StringBuffer sb = new StringBuffer (50); 158 159 sb.append(super.toString()); 160 if (_filtered) sb.append(Messages.getString("DjenericFolder.Filtered")); 161 if (!_allDisplayed) sb.append(Messages.getString("DjenericFolder.Partially")); 162 163 return sb.toString(); 164 } 165 166 169 public void filter(DjOql oql) throws DjenericException 170 { 171 173 clearLoadedObjects(); 174 175 _oql = oql; 176 177 if (hasOqlFilter()) 179 { 180 String wcl = "(" + getExtentUsage().getOqlExpression() + ")"; 181 String flr = oql.getWhereExpression(); 182 if (flr != null && flr.trim().length() > 0) wcl += " and (" + flr + ")"; 183 oql.setWhereExpression(wcl); 184 } 185 186 setOqlParameters(_oql, true); 187 188 _filtered = true; 189 DjList lst = getObjects(); 190 showObjects(); 191 expandPath(); 192 193 String msg; 194 if (!_allDisplayed) msg = Messages.getString("DjenericFolder.FoundTruncated", String.valueOf(lst.size()), 195 getExtentUsage().getExtent().getNamePlural()); 196 else 197 { 198 if (lst.size() == 1) msg = Messages.getString("DjenericFolder.Found1", getExtentUsage().getExtent() 199 .getNameSingular()); 200 else msg = Messages.getString("DjenericFolder.Found", String.valueOf(lst.size()), getExtentUsage().getExtent() 201 .getNamePlural()); 202 } 203 setStatusMessage(msg, true); 204 } 205 206 209 public DjObject getObject() 210 { 211 AbstractWebNode parent = getParent(); 212 if (parent != null && parent instanceof WebNode) 213 { 214 return ((WebNode) parent).getObject(); 215 } 216 return null; 217 } 218 219 225 public RelationUsage getVia() 226 { 227 return _via; 228 } 229 230 236 public void setVia(RelationUsage via) 237 { 238 _via = via; 239 setExtentUsage(via.getDetail()); 240 } 241 242 public void load() throws DjenericException 243 { 244 showObjects(); 246 } 247 248 public WebNode[] getChildNodes() throws Exception 249 { 250 load(); 251 ArrayList result = new ArrayList (); 252 for (int i = 0; i < getChildCount(); i++) 253 result.add(getChildAt(i)); 254 return (WebNode[]) result.toArray(new WebNode[0]); 255 } 256 257 public boolean canDelete() 258 { 259 return false; 260 } 261 262 public boolean canFilter() throws DjenericException 263 { 264 if (getExtentUsage() == null || !getSession().canQuery()) return false; 265 266 if (_canFilter != null) return _canFilter.booleanValue(); 267 268 boolean qry = false; 269 270 DjProperty[] props = getExtentUsage().getExtent().getProperties(); 271 for (int i = 0; i < props.length && !qry; i++) 272 { 273 qry = props[i].isQueryable(); 274 } 275 276 _canFilter = new Boolean (qry); 277 return qry; 278 } 279 280 public boolean canExport() throws DjenericException 281 { 282 return getExtentUsage() != null && getExtentUsage().getEditor() != null && getSession().canQuery(); 283 } 284 285 public boolean hasOqlFilter() 286 { 287 if (getExtentUsage() == null) return false; 288 289 String filter = getExtentUsage().getOqlExpression(); 290 return filter != null && filter.trim().length() != 0; 291 } 292 293 298 protected void initOql() throws DjenericException 299 { 300 if (hasOqlFilter() && _oql == null) 301 { 302 _oql = getExtentUsage().createDefaultOql(getSession()); 303 304 setOqlParameters(_oql, false); 305 } 306 } 307 308 private void setOqlParameters(DjOql oql, boolean ignoreNonExistingParameters) throws DjenericException 309 { 310 String [] parameters; 311 try 312 { 313 parameters = oql.getParameterNames(); 314 } 315 catch (ParseException e) 316 { 317 throw new DjenericException(e); 318 } 319 HashMap parentIds = new HashMap (); 320 collectParameters(parentIds); 321 322 for (int i = 0; i < parameters.length; i++) 323 { 324 DjObject obj = (DjObject) parentIds.get(parameters[i]); 325 if (obj == null && !ignoreNonExistingParameters) throw new DjenericException("Parameter with ID " + parameters[i] 326 + " not defined in tree"); 327 if (obj != null) oql.setParameter(parameters[i], obj.getObjectId()); 328 } 329 } 330 331 protected void clearLoadedObjects() 332 { 333 _alreadyLoaded = false; 334 _objects = null; 335 _oql = null; 336 _filtered = false; 337 } 338 339 343 protected void showObjects() throws DjenericException 344 { 345 boolean isCollapsed = !isExpanded(); 346 removeAllChildren(); 347 348 try 349 { 350 ExtentUsage usage = getExtentUsage(); 351 boolean detailHasKids = (usage.getDetailRelationCount() != 0); 352 getObjects(); 353 354 for (int j = 0; j < _objects.size(); j++) 355 { 356 DjObject obj = _objects.getDjenericObjectAt(j); 357 WebNode node = new WebNode(getSession()); 358 node.setExtentUsage(usage); 359 node.setObject(obj); 360 insertAsChild(node); 361 } 362 363 _alreadyLoaded = true; 364 } 365 finally 366 { 367 if (!isCollapsed) 368 { 369 expandPath(); 370 } 371 } 372 } 373 374 public void setExpanded(boolean expand) throws DjenericException 375 { 376 if (!_expanded && expand && !_alreadyLoaded) 377 { 378 showObjects(); 379 } 380 _expanded = expand; 381 } 382 383 public Element asXml(Document doc) throws DOMException , DjenericException 384 { 385 Element node = doc.createElement("folder"); 386 387 node.setAttribute("title", toString()); 388 node.setAttribute("nodeid", String.valueOf(getPath())); 389 node.setAttribute("expandable", String.valueOf(isExpandable())); 390 node.setAttribute("expanded", String.valueOf(isExpanded())); 391 392 node.setAttribute("canedit", "false"); 393 node.setAttribute("candelete", String.valueOf(canDelete())); 394 node.setAttribute("cancreate", String.valueOf(canCreate())); 395 node.setAttribute("canfilter", String.valueOf(canFilter())); 396 node.setAttribute("cancopy", String.valueOf(canCopy())); 397 node.setAttribute("canexport", String.valueOf(canExport())); 398 node.setAttribute("canimport", String.valueOf(getSession().canModify() && getSession().canCreate())); 399 400 if (isExpanded()) 401 { 402 for (int i = 0; i < getChildCount(); i++) 403 { 404 node.appendChild(getChildAt(i).asXml(doc)); 405 } 406 } 407 408 return node; 409 } 410 } | Popular Tags |