1 30 package com.genimen.djeneric.structure; 31 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 import java.util.Comparator ; 35 import java.util.HashMap ; 36 37 import com.genimen.djeneric.language.Messages; 38 import com.genimen.djeneric.repository.DjExtent; 39 import com.genimen.djeneric.repository.DjObject; 40 import com.genimen.djeneric.repository.DjOql; 41 import com.genimen.djeneric.repository.DjRelation; 42 import com.genimen.djeneric.repository.DjSession; 43 import com.genimen.djeneric.repository.exceptions.DjenericException; 44 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 45 import com.genimen.djeneric.repository.exceptions.PropertyRequiredException; 46 import com.genimen.djeneric.repository.exceptions.QbeFilterException; 47 import com.genimen.djeneric.repository.oql.core.ParseException; 48 import com.genimen.djeneric.util.DjLogger; 49 50 public class ExtentUsage implements Cloneable 51 { 52 DjExtent _extent; 53 int _seq = 0; 54 String _title = null; 55 String _oqlExpression = null; 56 String _id = null; 57 ResourceDefinition _imageIcon = null; 58 boolean _editAllowed = true; 59 String _editorTarget = null; 60 boolean _insertAllowed = true; 61 boolean _deleteAllowed = true; 62 boolean _multiRow = true; 63 int _rowsDisplayed = 5; 64 String _descriptorExpression = null; 65 String _customNodeClass = null; 66 EditorDefinition _editor = null; 67 ExtentUsage _parent = null; 68 RelationDescriptor _isDetailVia = null; 69 70 ArrayList _detailRelations = new ArrayList (); 71 ArrayList _propertyUsages = new ArrayList (); 72 73 boolean _recursive = false; 75 76 public ExtentUsage() 77 { 78 } 79 80 public ExtentUsage(DjExtent extent) 81 { 82 _extent = extent; 83 } 84 85 public Object clone() 86 { 87 ExtentUsage result = null; 88 try 89 { 90 result = (ExtentUsage) super.clone(); 91 if (_editor != null) result._editor = (EditorDefinition) _editor.clone(); 92 93 result._propertyUsages = new ArrayList (); 94 for (int i = 0; i < _propertyUsages.size(); i++) 95 result._propertyUsages.add(((PropertyUsage) _propertyUsages.get(i)).clone()); 96 97 result._detailRelations = new ArrayList (); 98 99 for (int i = 0; i < getDetailRelationCount(); i++) 100 { 101 RelationUsage usg = getDetailRelation(i); 102 result.addDetail((ExtentUsage) usg.getDetail().clone(), usg.getRelation()); 103 } 104 } 105 catch (CloneNotSupportedException e) 106 { 107 DjLogger.log(e); 108 } 109 return result; 110 } 111 112 public boolean equals(Object obj) 113 { 114 if (obj == this) return true; 115 116 if (!(obj instanceof ExtentUsage)) return false; 117 ExtentUsage other = (ExtentUsage) obj; 118 119 return _extent == other._extent && _seq == other._seq && safeCompare(_title, other._title) 120 && safeCompare(_oqlExpression, other._oqlExpression) && safeCompare(_id, other._id) 121 && safeCompare(_editorTarget, other._editorTarget) && _imageIcon == other._imageIcon 122 && _editAllowed == other._editAllowed && _insertAllowed == other._insertAllowed 123 && _deleteAllowed == other._deleteAllowed && _multiRow == other._multiRow && _multiRow == other._multiRow 124 && _rowsDisplayed == other._rowsDisplayed && safeCompare(_descriptorExpression, other._descriptorExpression) 125 && safeCompare(_editor, other._editor) && _parent == other._parent 126 && safeCompare(_isDetailVia, other._isDetailVia) && safeCompare(_detailRelations, other._detailRelations) 127 && safeCompare(_propertyUsages, other._propertyUsages); 128 } 129 130 public int hashCode() 131 { 132 int result = 0; 133 if (_extent != null) result += _extent.hashCode(); 134 if (_title != null) result += _title.hashCode(); 135 if (_oqlExpression != null) result += _oqlExpression.hashCode(); 136 if (_editorTarget != null) result += _editorTarget.hashCode(); 137 138 return result; 139 } 140 141 private boolean safeCompare(Object one, Object two) 142 { 143 if (one == null && two == null) return true; 144 if (one == null || two == null) return false; 145 146 return one.equals(two); 147 } 148 149 public String toString() 150 { 151 return getName() + "(" + getExtent() + ")"; 152 } 153 154 public void moveDetailDown(int detailNum) 155 { 156 if (detailNum + 1 >= _detailRelations.size()) return; 157 158 Object o = _detailRelations.get(detailNum); 159 _detailRelations.remove(detailNum); 160 _detailRelations.add(detailNum + 1, o); 161 } 162 163 public void moveDetailUp(int detailNum) 164 { 165 if (detailNum == 0 || detailNum >= _detailRelations.size()) return; 166 167 Object o = _detailRelations.get(detailNum); 168 _detailRelations.remove(detailNum); 169 _detailRelations.add(detailNum - 1, o); 170 } 171 172 public void setEditor(EditorDefinition editor) 173 { 174 _editor = editor; 175 } 176 177 public EditorDefinition getEditor() 178 { 179 if (isRecursive()) 180 { 181 return getParent().getEditor(); 182 } 183 else 184 { 185 return _editor; 186 187 } 188 } 189 190 public RelationUsage addDetail(ExtentUsage detail, DjRelation via) 191 { 192 if (isRecursive()) 193 { 194 return getParent().addDetail(detail, via); 195 } 196 else 197 { 198 RelationUsage ru = new RelationUsage(this, detail, via); 199 _detailRelations.add(ru); 200 detail._parent = this; 201 if (via != null) detail.setIsDetailVia(new RelationDescriptor(via, detail.getExtent())); 202 return ru; 203 } 204 } 205 206 public ExtentUsage getParent() 207 { 208 return _parent; 209 } 210 211 public void setRecursive(boolean r) 212 { 213 _recursive = r; 214 } 215 216 public boolean isRecursive() 217 { 218 return _recursive; 219 } 220 221 public void removeDetailRelation(RelationUsage d) 222 { 223 if (isRecursive()) 224 { 225 getParent().removeDetailRelation(d); 226 } 227 else 228 { 229 _detailRelations.remove(d); 230 d.getDetail()._parent = null; 231 } 232 } 233 234 public RelationUsage[] getDetailRelations() 235 { 236 if (isRecursive()) 237 { 238 return getParent().getDetailRelations(); 239 } 240 else 241 { 242 return (RelationUsage[]) _detailRelations.toArray(new RelationUsage[0]); 243 } 244 } 245 246 public ExtentUsage[] getChildren() 247 { 248 RelationUsage[] rels = getDetailRelations(); 249 ExtentUsage[] result = new ExtentUsage[rels.length]; 250 for (int i = 0; i < rels.length; i++) 251 { 252 result[i] = rels[i].getDetail(); 253 } 254 return result; 255 } 256 257 public RelationUsage getDetailRelation(int idx) 258 { 259 if (isRecursive()) 260 { 261 return getParent().getDetailRelation(idx); 262 } 263 else 264 { 265 return (RelationUsage) _detailRelations.get(idx); 266 } 267 } 268 269 public int getDetailRelationCount() 270 { 271 if (isRecursive()) 272 { 273 return getParent().getDetailRelationCount(); 274 } 275 else 276 { 277 return _detailRelations.size(); 278 } 279 } 280 281 public void setIsDetailVia(RelationDescriptor via) 284 { 285 _isDetailVia = via; 286 } 287 288 public void setExtent(DjExtent extent) 289 { 290 _extent = extent; 291 } 292 293 public boolean isMultiRow() 294 { 295 return _multiRow; 296 } 297 298 public int getRowsDisplayed() 299 { 300 return _rowsDisplayed; 301 } 302 303 public void setRowsDisplayed(int rows) 304 { 305 _rowsDisplayed = rows; 306 } 307 308 public void setMultiRow(boolean multirow) 309 { 310 _multiRow = multirow; 311 } 312 313 public int getPropertyUsageCount() 314 { 315 return _propertyUsages.size(); 316 } 317 318 public int getPropertyUsageIndex(String propertyName) throws ObjectNotDefinedException 319 { 320 for (int i = 0; i < getPropertyUsageCount(); i++) 321 { 322 if (getPropertyUsage(i).getPropertyName().equals(propertyName)) 323 { 324 return i; 325 } 326 } 327 throw new ObjectNotDefinedException(Messages.getString("ExtentUsage.PropertyNotDefined", propertyName)); 328 } 329 330 public PropertyUsage getPropertyUsage(int idx) 331 { 332 return (PropertyUsage) _propertyUsages.get(idx); 333 } 334 335 public void addPropertyUsage(PropertyUsage usg) 336 { 337 _propertyUsages.add(usg); 338 } 339 340 public void addPropertyUsage(int atIdx, PropertyUsage usg) 341 { 342 _propertyUsages.add(atIdx, usg); 343 } 344 345 public void removePropertyUsage(int idx) 346 { 347 _propertyUsages.remove(idx); 348 } 349 350 public void removePropertyUsage(PropertyUsage cu) 351 { 352 _propertyUsages.remove(cu); 353 } 354 355 public String getName() 356 { 357 return getExtent().getName(); 358 } 359 360 public void setSeq(int seq) 361 { 362 _seq = seq; 363 } 364 365 public int getSeq() 366 { 367 return _seq; 368 } 369 370 public void sortPropertyUsages() 371 { 372 Collections.sort(_propertyUsages, new PropertyUsageComparator()); 373 } 374 375 public String getTitle() 376 { 377 if (_title == null && _extent != null) 378 { 379 return _extent.getNamePlural(); 380 } 381 return _title; 382 } 383 384 public void setTitle(String title) 385 { 386 if (title != null) 387 { 388 if (title.trim().equals("")) 389 { 390 title = null; 391 } 392 } 393 _title = title; 394 } 395 396 public String getId() 397 { 398 if (_id == null && _extent != null) 399 { 400 return _extent.getName().toLowerCase(); 401 } 402 return _id; 403 } 404 405 public void setId(String id) 406 { 407 if (id != null) 408 { 409 if (id.trim().equals("")) 410 { 411 id = null; 412 } 413 } 414 _id = id; 415 } 416 417 public boolean isEditAllowed() 418 { 419 return _editAllowed; 420 } 421 422 public void setEditAllowed(boolean allowed) 423 { 424 _editAllowed = allowed; 425 } 426 427 public boolean isInsertAllowed() 428 { 429 return _insertAllowed; 430 } 431 432 public void setInsertAllowed(boolean allowed) 433 { 434 _insertAllowed = allowed; 435 } 436 437 public boolean isDeleteAllowed() 438 { 439 return _deleteAllowed; 440 } 441 442 public void setDeleteAllowed(boolean allowed) 443 { 444 _deleteAllowed = allowed; 445 } 446 447 public DjExtent getExtent() 448 { 449 return _extent; 450 } 451 452 public RelationDescriptor getVia() 453 { 454 return _isDetailVia; 455 } 456 457 public void setDescriptorExpression(String descriptorExpression) 458 { 459 _descriptorExpression = descriptorExpression; 460 } 461 462 public String getDescriptorExpression() 463 { 464 return _descriptorExpression; 465 } 466 467 public void checkRequiredProperties(DjObject obj) throws ObjectNotDefinedException, DjenericException 468 { 469 ArrayList missingProperties = null; 470 471 for (int i = 0; i < getPropertyUsageCount(); i++) 472 { 473 if ((getPropertyUsage(i).isRequired() || getPropertyUsage(i).getProperty().isRequired()) 474 && obj.isNull(getPropertyUsage(i).getPropertyName())) 475 { 476 if (missingProperties == null) 477 { 478 missingProperties = new ArrayList (); 479 } 480 missingProperties.add(getPropertyUsage(i).getPropertyName()); 481 } 482 } 483 if (missingProperties != null) 484 { 485 throw new PropertyRequiredException(Messages.getString("global.RequiredPropNull"), obj, missingProperties); 486 } 487 } 488 489 public int[] getPropertySortIndices() 490 { 491 ArrayList lst = new ArrayList (); 492 for (int i = 0; i < getPropertyUsageCount(); i++) 493 { 494 if (getPropertyUsage(i).getSortOrder() != 0) lst.add(getPropertyUsage(i)); 495 } 496 Collections.sort(lst, new PropertyUsageSortOrderComparator()); 497 int[] result = new int[lst.size()]; 498 499 try 500 { 501 for (int i = 0; i < lst.size(); i++) 502 { 503 result[i] = getExtent().getPropertyIndex(((PropertyUsage) lst.get(i)).getBaseProperty().getName()); 504 if (((PropertyUsage) lst.get(i)).getSortOrder() < 0) result[i] = -result[i]; 505 } 506 } 507 catch (ObjectNotDefinedException onde) 508 { 509 DjLogger.log(onde); 512 } 513 return result; 514 } 515 516 public void removeFromParent() 517 { 518 if (_parent != null) 519 { 520 _parent.removeDetailRelation(this, _isDetailVia); 521 } 522 } 523 524 protected void removeDetailRelation(ExtentUsage detail, RelationDescriptor isDetailVia) 525 { 526 if (isRecursive()) 527 { 528 getParent().removeDetailRelation(detail, isDetailVia); 529 } 530 else 531 { 532 RelationUsage ru; 533 try 534 { 535 ru = new RelationUsage(this, detail, isDetailVia.getRelation()); 536 _detailRelations.remove(ru); 537 detail._parent = null; 538 detail.setIsDetailVia(null); 539 } 540 catch (ObjectNotDefinedException e) 541 { 542 } 544 } 545 } 546 547 public String getOqlExpression() 548 { 549 return _oqlExpression; 550 } 551 552 public void setOqlExpression(String oqlExpression) 553 { 554 if (oqlExpression != null && oqlExpression.trim().length() == 0) oqlExpression = null; 555 _oqlExpression = oqlExpression; 556 } 557 558 public DjOql createDefaultOql(DjSession session) 559 { 560 DjOql result = session.createOql(getExtent()); 561 String expr = getOqlExpression(); 562 if (expr != null) result.setWhereExpression(expr); 563 564 return result; 565 } 566 567 public void validateOqlExpression() throws QbeFilterException 568 { 569 try 570 { 571 String expr = getOqlExpression(); 572 573 if (expr == null || expr.trim().length() == 0) return; 574 575 DjOql test = new DjOql(getExtent()); 576 test.setWhereExpression(getOqlExpression()); 577 test.getFromWhereClause(); 578 } 579 catch (ParseException e) 580 { 581 throw new QbeFilterException(e); 582 } 583 584 } 585 586 public void validate() throws QbeFilterException, DjenericException 587 { 588 validateOqlExpression(); 589 for (int i = 0; i < getPropertyUsageCount(); i++) 590 { 591 try 592 { 593 getPropertyUsage(i).validate(); 594 } 595 catch (DjenericException dje) 596 { 597 throw new DjenericException(getId() + ": " + dje.getMessage()); 598 } 599 } 600 } 601 602 public ResourceDefinition getImageIconResource() 603 { 604 return _imageIcon; 605 } 606 607 public void setImageIconResource(ResourceDefinition definition) 608 { 609 _imageIcon = definition; 610 } 611 612 public void deleteInvalidNodes() 613 { 614 RelationUsage[] rels = getDetailRelations(); 615 for (int i = 0; i < rels.length; i++) 616 { 617 if (!rels[i].isValid()) removeDetailRelation(rels[i]); 618 else rels[i].getDetail().deleteInvalidNodes(); 619 } 620 } 621 622 626 public String getEditorTarget() 627 { 628 return _editorTarget; 629 } 630 631 635 public void setEditorTarget(String editorTarget) 636 { 637 if (editorTarget != null && editorTarget.trim().length() == 0) editorTarget = null; 638 _editorTarget = editorTarget; 639 } 640 641 public String getRelationsAsXml(int indent) 642 { 643 if (isRecursive()) return ""; 644 645 StringBuffer result = new StringBuffer (500); 646 StringBuffer indentStr = new StringBuffer (20); 647 for (int i = 0; i < indent; i++) 648 indentStr.append(" "); 649 650 for (int i = 0; i < getDetailRelationCount(); i++) 651 { 652 RelationUsage rel = getDetailRelation(i); 653 result.append(indentStr.toString()); 654 result.append("<detail relation=\"" + rel.getRelation().getName() + "\" extent=\"" + rel.getDetail().getExtent() 655 + "\">\n"); 656 result.append(rel.getDetail().getRelationsAsXml(indent + 2)); 657 result.append(indentStr.toString()); 658 result.append("</detail>\n"); 659 } 660 return result.toString(); 661 } 662 663 public String getCustomNodeClass() 664 { 665 return _customNodeClass; 666 } 667 668 public void setCustomNodeClass(String customNodeClass) 669 { 670 if (customNodeClass != null) 671 { 672 customNodeClass = customNodeClass.trim(); 673 if (customNodeClass.length() == 0) customNodeClass = null; 674 } 675 _customNodeClass = customNodeClass; 676 } 677 678 protected void collectUsageIds(HashMap ids) 679 { 680 ids.put(getId(), this); 681 if (getParent() != null) getParent().collectUsageIds(ids); 682 } 683 684 public HashMap getParameters() 685 { 686 HashMap result = new HashMap (); 687 collectUsageIds(result); 688 return result; 689 } 690 } 691 692 class PropertyUsageSortOrderComparator implements Comparator 693 { 694 695 public int compare(Object o1, Object o2) 696 { 697 PropertyUsage f1 = (PropertyUsage) o1; 698 PropertyUsage f2 = (PropertyUsage) o2; 699 700 return f1.getSortOrder() - f2.getSortOrder(); 701 } 702 } | Popular Tags |