/** tests the PB.retrieveAllReferences() feature */
  public void testRetrieveAllReferences() {
    String name = "testRetrieveAllReferences_" + System.currentTimeMillis();

    // ensure there is an item to find
    Article tmpArticle = createArticle(name);
    ProductGroup pg = createProductGroup(name);
    tmpArticle.setProductGroup(pg);

    broker.beginTransaction();
    broker.store(pg);
    broker.store(tmpArticle);
    broker.commitTransaction();
    Identity tmpOID = broker.serviceIdentity().buildIdentity(tmpArticle);
    broker.clearCache();
    ObjectReferenceDescriptor ord = null;
    try {
      // switch to shallow retrieval
      ClassDescriptor cld = broker.getClassDescriptor(Article.class);
      ord = (ObjectReferenceDescriptor) cld.getObjectReferenceDescriptors().get(0);
      ord.setCascadeRetrieve(false);

      Article article = (Article) broker.getObjectByIdentity(tmpOID);
      assertNull("now reference should be null", article.getProductGroup());

      // now force loading:
      broker.retrieveAllReferences(article);
      assertNotNull("now reference should NOT be null", article.getProductGroup());

      // clean up cld
      ord.setCascadeRetrieve(true);
    } finally {
      // restore old value
      if (ord != null) ord.setCascadeRetrieve(true);
    }
  }
  /**
   * @see
   *     org.kuali.rice.krad.service.PersistenceStructureService#getBusinessObjectAttributeClass(java.lang.Class,
   *     java.lang.String)
   */
  public Class<? extends PersistableBusinessObjectExtension> getBusinessObjectAttributeClass(
      Class<? extends PersistableBusinessObject> clazz, String attributeName) {
    String baseAttributeName = attributeName;
    String subAttributeString = null;
    if (attributeName.contains(".")) {
      baseAttributeName = attributeName.substring(0, attributeName.indexOf('.'));
      subAttributeString = attributeName.substring(attributeName.indexOf('.') + 1);
    }

    // Legacy OJB
    Class attributeClassLegacy = null;
    ClassDescriptor classDescriptor = null;
    try {
      classDescriptor = this.getClassDescriptor(clazz);
    } catch (ClassNotPersistableException e) {
      LOG.warn("Class descriptor for " + clazz.getName() + "was not found");
    }

    ObjectReferenceDescriptor refDescriptor = null;
    if (classDescriptor != null) {
      refDescriptor = classDescriptor.getObjectReferenceDescriptorByName(baseAttributeName);
    }

    if (refDescriptor != null) {
      attributeClassLegacy = refDescriptor.getItemClass();
    }

    // recurse if necessary
    if (subAttributeString != null) {
      attributeClassLegacy =
          getBusinessObjectAttributeClass(attributeClassLegacy, subAttributeString);
    }

    return attributeClassLegacy;
  }
  /**
   * This method returns a list of primary key field names. This method uses the CacheNoCopy caching
   * method. "NoCopy" is the faster of the caching annotations, but because it does not make a copy
   * the list that is returned must not be modified. To enforce this the returned list is wrapped in
   * a Collections.unmodifiableList method. This will cause an exception to be thrown if the list is
   * altered.
   *
   * @param clazz
   * @return unmodifiableList of field names. Any attempt to alter list will result in an
   *     UnsupportedOperationException
   */
  public List listPrimaryKeyFieldNames(Class clazz) {
    // Legacy OJB
    List fieldNamesLegacy = new ArrayList();
    ClassDescriptor classDescriptor = getClassDescriptor(clazz);
    FieldDescriptor keyDescriptors[] = classDescriptor.getPkFields();

    for (int i = 0; i < keyDescriptors.length; ++i) {
      FieldDescriptor keyDescriptor = keyDescriptors[i];
      fieldNamesLegacy.add(keyDescriptor.getAttributeName());
    }
    return fieldNamesLegacy;
  }
  public Map<String, String> getPersistableAttributesColumnMap(Class clazz)
      throws ClassNotPersistableException {
    Map<String, String> fieldMap = new HashMap<String, String>();
    if (isPersistable(clazz)) {
      ClassDescriptor classDescriptor = getClassDescriptor(clazz);
      FieldDescriptor fieldDescriptors[] = classDescriptor.getFieldDescriptions();
      for (FieldDescriptor fieldDescriptor : fieldDescriptors) {
        fieldMap.put(fieldDescriptor.getAttributeName(), fieldDescriptor.getColumnName());
      }
      return fieldMap;
    }

    throw new ClassNotPersistableException(clazz.getName() + " is not Persistable");
  }
 public void tearDown() throws Exception {
   // restore isolation level
   cld.setIsolationLevel(oldIsolationLevel);
   try {
     lockManager.releaseLock(tx1, obj);
     lockManager.releaseLock(tx2, obj);
   } finally {
     super.tearDown();
   }
 }
  public void setUp() throws Exception {
    super.setUp();

    // change isolation-level used for test
    cld =
        MetadataManager.getInstance()
            .getRepository()
            .getDescriptorFor(org.apache.ojb.broker.Article.class);
    oldIsolationLevel = cld.getIsolationLevel();
    cld.setIsolationLevel(testIsoLevel);

    lockManager = LockManagerHelper.getLockManagerSpecifiedByConfiguration();

    // initialize the dummies
    tx2 = new Object();
    tx1 = new Object();

    obj = org.apache.ojb.broker.Article.createInstance();
  }
  public void testShallowAndDeepRetrieval() throws Exception {
    String name = "testShallowAndDeepRetrieval_" + System.currentTimeMillis();

    ObjectReferenceDescriptor ord = null;

    try {
      // prepare test, create article with ProductGroup
      Article tmpArticle = createArticle(name);
      ProductGroup pg = createProductGroup(name);
      tmpArticle.setProductGroup(pg);
      pg.add(tmpArticle);

      broker.beginTransaction();
      // in repository Article 1:1 refererence to PG hasn't enabled auto-update,
      // so first store the PG. PG has enabled auto-update and will store the
      // article automatic
      broker.store(pg);
      broker.commitTransaction();
      // after insert we can build the Article identity
      Identity tmpOID = broker.serviceIdentity().buildIdentity(tmpArticle);
      broker.clearCache();

      // switch to shallow retrieval
      ClassDescriptor cld = broker.getClassDescriptor(Article.class);
      ord = cld.getObjectReferenceDescriptorByName("productGroup");
      ord.setCascadeRetrieve(false);

      Article article = (Article) broker.getObjectByIdentity(tmpOID);
      assertNull("now reference should be null", article.getProductGroup());

      // now switch to deep retrieval
      ord.setCascadeRetrieve(true);
      // should work without setting cld
      // broker.setClassDescriptor(cld);
      broker.clearCache();
      article = (Article) broker.getObjectByIdentity(tmpOID);
      assertNotNull("now reference should NOT be null", article.getProductGroup());
    } finally {
      // restore old value
      if (ord != null) ord.setCascadeRetrieve(true);
    }
  }
  // copied and adapted from OJB's MtoNBroker
  protected void updateMtoNRelation(PersistenceBroker pb, RelationTupleInfo tupleInfo) {
    DomainObject obj1 = tupleInfo.obj1;
    DomainObject obj2 = tupleInfo.obj2;

    ClassDescriptor cld1 = pb.getDescriptorRepository().getDescriptorFor(obj1.getClass());
    CollectionDescriptor cod = cld1.getCollectionDescriptorByName(tupleInfo.colNameOnObj1);
    if (cod == null) {
      // try the mapping on the other object
      cld1 = pb.getDescriptorRepository().getDescriptorFor(obj2.getClass());
      cod = cld1.getCollectionDescriptorByName(tupleInfo.colNameOnObj2);

      // switch objects
      obj1 = tupleInfo.obj2;
      obj2 = tupleInfo.obj1;
    }

    String[] oidColumns = new String[2];
    oidColumns[0] = cod.getFksToThisClass()[0];
    oidColumns[1] = cod.getFksToItemClass()[0];

    ValueContainer[] oidValues = new ValueContainer[2];
    oidValues[0] = new ValueContainer(obj1.getOid(), OID_JDBC_TYPE);
    oidValues[1] = new ValueContainer(obj2.getOid(), OID_JDBC_TYPE);

    String table = cod.getIndirectionTable();

    // always remove the tuple
    String sqlStmt = pb.serviceSqlGenerator().getDeleteMNStatement(table, oidColumns, null);
    pb.serviceJdbcAccess().executeUpdateSQL(sqlStmt, cld1, oidValues, null);

    // if it was not to remove but to add, then add it
    // this "delete-first, add-after" serves to ensure that we can add
    // multiple times
    // the same tuple to a relation and still have the Set semantics for the
    // relation.
    if (!tupleInfo.remove) {
      sqlStmt = pb.serviceSqlGenerator().getInsertMNStatement(table, oidColumns, EMPTY_ARRAY);
      pb.serviceJdbcAccess().executeUpdateSQL(sqlStmt, cld1, oidValues, null);
    }
  }
Exemple #9
0
  /**
   * Searches the class descriptor for the ojbConcrete class attribute if it finds the concrete
   * class attribute, append a where clause which specifies we can load all classes that are this
   * type or extents of this type.
   *
   * @param cld
   * @return the extent classes
   */
  private static Collection getExtentClasses(ClassDescriptor cld) {
    /** 1. check if this class has a ojbConcreteClass attribute */
    FieldDescriptor fd = cld.getFieldDescriptorByName(ClassDescriptor.OJB_CONCRETE_CLASS);
    Collection classes = new HashSet(); // use same class only once
    if (fd != null) {
      classes.add(cld.getClassOfObject().getName());
    }

    /** 2. if this class has extents/is an extent search for all extents */
    if (cld.isExtent()) {
      Vector extentClasses = cld.getExtentClasses();

      /** 3. get all extents for this class */
      for (int i = 0; i < extentClasses.size(); i++) {
        Class ec = (Class) extentClasses.get(i);
        ClassDescriptor extCld = cld.getRepository().getDescriptorFor(ec);
        classes.addAll(getExtentClasses(extCld));
      }
    }

    return classes;
  }
Exemple #10
0
  /** tests the PB.retrieveReference() feature */
  public void testRetrieveReference() throws Exception {
    String name = "testRetrieveReference_" + System.currentTimeMillis();

    // ensure there is an item to find
    Article tmpArticle = createArticle(name);
    ProductGroup pg = createProductGroup(name);
    tmpArticle.setProductGroup(pg);
    broker.beginTransaction();
    broker.store(pg);
    broker.store(tmpArticle);
    broker.commitTransaction();
    Identity tmpOID = broker.serviceIdentity().buildIdentity(tmpArticle);
    broker.clearCache();

    ObjectReferenceDescriptor ord = null;
    try {
      // switch to shallow retrieval
      ClassDescriptor cld = broker.getClassDescriptor(Article.class);
      // article only has one ord
      ord = cld.getObjectReferenceDescriptorByName("productGroup");
      ord.setCascadeRetrieve(false);

      Article article = (Article) broker.getObjectByIdentity(tmpOID);
      assertNull("now reference should be null", article.getProductGroup());

      // now force loading:
      broker.retrieveReference(article, "productGroup");
      assertNotNull("now reference should NOT be null", article.getProductGroup());

      // repair cld
      ord.setCascadeRetrieve(true);
      // should work without setting cld
      // broker.setClassDescriptor(cld);
    } finally {
      // restore old value
      if (ord != null) ord.setCascadeRetrieve(true);
    }
  }