Package org.openbravo.base.structure

Examples of org.openbravo.base.structure.BaseOBObject


  @Override
  public BaseOBObject resolve(String entityName, String id, boolean referenced) {

    final Entity entity = ModelProvider.getInstance().getEntity(entityName);

    BaseOBObject result = null;

    // The zero organization is in fact a system level concept, return it
    // always
    if (entityName.equals(Organization.ENTITY_NAME) && id != null && id.equals("0")) {
      return getOrganizationZero();
    }

    if (entityName.equals(Client.ENTITY_NAME) && id != null && id.equals("0")) {
      return getClientZero();
    }

    if (id != null) {
      result = getData().get(getKey(entityName, id));
      if (result != null) {
        return result;
      }
    }

    // note id can be null if someone did not care to add it in a manual
    // xml file
    // In case of client import the client is always created new
    if (!entityName.equals(Client.ENTITY_NAME) && id != null) {
      result = searchInstance(entity, id);
    }

    if (result != null) {
      // found, cache it for future use
      getData().put(getKey(entityName, id), result);
    } else {

      // not found create a new one
      result = (BaseOBObject) OBProvider.getInstance().get(entityName);

      if (id != null) {

        // force new
        result.setNewOBObject(true);

        // check if we can keep the id for this one
        if (!OBDal.getInstance().exists(entityName, id)) {
          result.setId(id);
        }

        // keep it here so it can be found later
        getData().put(getKey(entityName, id), result);
      }
View Full Code Here


    return instance;
  }

  public void checkDeleteAllowed(Object o) {
    if (!OBContext.getOBContext().isInAdministratorMode() && o instanceof BaseOBObject) {
      final BaseOBObject bob = (BaseOBObject) o;
      final Entity entity = ModelProvider.getInstance().getEntity(bob.getEntityName());
      if (!entity.isDeletable()) {
        throw new OBSecurityException("Entity " + entity.getName() + " is not deletable");
      }
    }
    checkWriteAccess(o);
View Full Code Here

    // so that the objects on which other depend are inserted first
    final List<BaseOBObject> toInsert = xec.getToInsert();
    int done = 0;
    final Set<BaseOBObject> inserted = new HashSet<BaseOBObject>();
    for (int i = toInsert.size() - 1; i > -1; i--) {
      final BaseOBObject ins = toInsert.get(i);
      // for (final BaseOBObject ins : toInsert) {
      insertObjectGraph(ins, inserted);
      ir.getInsertedObjects().add(ins);
      done++;
    }
    Check.isTrue(done == toInsert.size(), "Not all objects have been inserted, check for loop: "
        + done + "/" + toInsert.size());

    // flush to set the ids in the objects
    OBDal.getInstance().flush();

    // do the updates the other way around also
    done = 0;
    final List<BaseOBObject> toUpdate = xec.getToUpdate();
    for (int i = toUpdate.size() - 1; i > -1; i--) {
      final BaseOBObject upd = toUpdate.get(i);
      OBDal.getInstance().save(upd);
      ir.getUpdatedObjects().add(upd);
      done++;
    }
    Check.isTrue(done == toUpdate.size(), "Not all objects have been inserted, check for loop: "
View Full Code Here

        if (p.isOneToMany()) {
          @SuppressWarnings("unchecked")
          final List<BaseOBObject> bobs = (List<BaseOBObject>) value;
          for (int i = 0; i < bobs.size(); i++) {
            final BaseOBObject curValue = bobs.get(i);
            if (fromTo.containsKey(curValue)) {
              bobs.set(i, fromTo.get(curValue));
            }
          }
        } else if (fromTo.containsKey(value)) {
View Full Code Here

  }

  private static BaseOBObject copy(BaseOBObject source, boolean copyChildren, boolean resetId,
      Map<BaseOBObject, BaseOBObject> fromTo) {

    final BaseOBObject target = (BaseOBObject) OBProvider.getInstance().get(source.getEntityName());
    fromTo.put(source, target);
    for (final Property p : source.getEntity().getProperties()) {
      final Object value = source.getValue(p.getName());
      if (p.isOneToMany()) {
        if (copyChildren) {
          final List<BaseOBObject> targetChildren = new ArrayList<BaseOBObject>();
          target.setValue(p.getName(), targetChildren);
          @SuppressWarnings("unchecked")
          final List<BaseOBObject> sourceChildren = (List<BaseOBObject>) value;
          for (final BaseOBObject sourceChild : sourceChildren) {
            targetChildren.add(copy(sourceChild, copyChildren, resetId, fromTo));
          }
        }
      } else {
        target.setValue(p.getName(), value);
      }
    }
    if (resetId) {
      target.setId(null);
    }
    return target;
  }
View Full Code Here

              table.getDBTableName()).getPropertyByColumnName(columnName).getName();
          DataPackage dpackage = table.getDataPackage();
          try {
            Class tableClass = Class.forName(dpackage.getJavaPackage() + "."
                + table.getJavaClassName());
            BaseOBObject parentObject = (BaseOBObject) OBDal.getInstance().get(tableClass,
                parentObjectId);
            parentObject.set(propertyName, null);
            OBDal.getInstance().flush();
            OBDal.getInstance().remove(image);
          } catch (Exception e) {
            e.printStackTrace();
            log4j.error("Class for table not found", e);
View Full Code Here

          break;
        }

        final LocalElement element = getElement(xmlReader);

        final BaseOBObject bob = processEntityElement(element, xmlReader, false);

        if (hasErrorOccured()) {
          return result;
        }
View Full Code Here

      final boolean hasReferenceAttribute = obElement.getAttributes().get(
          XMLConstants.REFERENCE_ATTRIBUTE) != null;

      // resolve the entity, using the id, note that
      // resolve will create a new object if none is found
      BaseOBObject bob = resolve(entityName, id, false);

      // should never be null at this point
      Check.isNotNull(bob, "The business object " + entityName + " (" + id
          + ") can not be resolved");

      // warn/error is logged below if the entity is updated
      // update is prevented below
      final boolean writable = OBContext.getOBContext().isInAdministratorMode()
          || SecurityChecker.getInstance().isWritable(bob);

      // do some checks to determine if this one should be updated
      // a referenced instance should not be updated if it is not new
      // note that embedded children are updated but non-embedded children
      // are not updated!
      final boolean preventRealUpdate = !writable
          || (hasReferenceAttribute && !bob.isNewOBObject());

      final Entity entity = ModelProvider.getInstance().getEntity(obElement.getName());
      boolean updated = false;

      // now parse the property elements
      while (true) {
        xmlReader.nextTag();

        if (isAtEndElement(xmlReader, entityName)) {
          break;
        }

        final LocalElement childElement = getElement(xmlReader);
        final Property p = entity.getProperty(childElement.getName());
        log.debug(">>> Importing property " + p.getName());

        // TODO: make this option controlled
        final boolean isNotImportableProperty = p.isTransient(bob)
            || (p.isAuditInfo() && !isOptionImportAuditInfo()) || p.isInactive();
        if (isNotImportableProperty) {
          log.debug("Property " + p + " is inactive, transient or auditinfo, ignoring it");
          skipElement(xmlReader);
          continue;
        }

        // ignore the id properties as they are already set, or should
        // not be set
        if (p.isId()) {
          skipElement(xmlReader);
          continue;
        }

        if (p.isOneToMany()) {
          throw new EntityXMLException("This XML converter can not handle one-to-many properties");
        }

        final Object currentValue = bob.get(p.getName());

        // do the primitive values
        if (p.isPrimitive()) {

          // NOTE: I noticed a difference between running from Eclipse and from the commandline
          // after some searching, there is a jar file wstx-asl-3.0.2.jar used in
          // src-db/database/lib
          // which provides the woodstox XMLStreamReader. From the commandline the xerces
          // XMLStreamReader
          // is used. They differ in the handling of the getText method in handling entities. For
          // example the following text: <text>Tom&amp;Jerry</text> now assume that the pointer is
          // at the content of the text tag, and you do getText, then woodstox will return Tom&Jerry
          // while xerces will return Tom, this because the &amp; is an entity which is a separate
          // event.
          // below we use the getElementText which works correctly in both cases apparently

          // element value, note that getElementText means that it is not required anymore
          // to go to element end, that is done by the xmlReader
          final String elementText = xmlReader.getElementText();
          Object newValue = XMLTypeConverter.getInstance().fromXML(p.getPrimitiveType(),
              elementText);

          // correct the value
          newValue = replaceValue(bob, p, newValue);

          log.debug("Primitive property with value " + newValue);

          // only update if changed
          if ((currentValue != null && newValue == null)
              || (currentValue == null && newValue != null)
              || (currentValue != null && newValue != null && !currentValue.equals(newValue))) {
            log.debug("Value changed setting it");
            if (!preventRealUpdate) {
              bob.set(p.getName(), newValue);
              updated = true;
            }
          }
        } else {
          Check.isTrue(!p.isOneToMany(), "One to many property not allowed here");
          // never update the org or client through xml!
          final boolean clientUpdate = bob instanceof ClientEnabled
              && p.getName().equals(Organization.PROPERTY_CLIENT);
          final boolean orgUpdate = bob instanceof OrganizationEnabled
              && p.getName().equals(Client.PROPERTY_ORGANIZATION);
          if (!isOptionClientImport() && currentValue != null && (clientUpdate || orgUpdate)) {
            skipElement(xmlReader);
            continue;
          }

          // determine the referenced entity
          Object newValue;

          // handle null value
          if (childElement.getAttributes().get(XMLConstants.ID_ATTRIBUTE) == null) {
            newValue = null;
          } else {
            // get the info and resolve the reference
            final String refId = childElement.getAttributes().get(XMLConstants.ID_ATTRIBUTE);
            final String refEntityName = p.getTargetEntity().getName();
            newValue = resolve(refEntityName, refId, true);
          }
          newValue = replaceValue(bob, p, newValue);

          final boolean hasChanged = (currentValue == null && newValue != null)
              || (currentValue != null && newValue != null && !currentValue.equals(newValue));
          if (hasChanged) {
            log.debug("Setting value " + newValue);
            if (!preventRealUpdate) {
              bob.set(p.getName(), newValue);
              updated = true;
            }
          }
          checkEndElement(xmlReader, childElement.getName());
        }
View Full Code Here

    Check.isNotNull(client, "Client should not be null");
    Check.isNotNull(organization, "Org should not be null");

    final Entity entity = ModelProvider.getInstance().getEntity(entityName);

    BaseOBObject result = null;
    // note id can be null if someone did not care to add it in a manual
    // xml file
    if (id != null) {
      result = data.get(getKey(entityName, id));
      if (result != null) {
        return result;
      }

      result = searchInstance(entity, id);
    }

    if (result != null) {
      // found, cache it for future use
      data.put(getKey(entityName, id), result);
    } else {
      if (referenced && !isOptionCreateReferencedIfNotFound()) {
        throw new EntityNotFoundException("Entity " + entityName + " with id " + id + " not found");
      }
      if (resolvingMode == ResolvingMode.MUST_EXIST) {
        throw new EntityNotFoundException("Entity " + entityName + " with id " + id + " not found");
      }

      // not found create a new one
      result = (BaseOBObject) OBProvider.getInstance().get(entityName);

      if (id != null) {
        // keep the relation so that ad_ref_data_loaded can be filled
        // later
        objectOriginalIdMapping.put(result, id);

        // check if we can keep the id for this one
        if (!OBDal.getInstance().exists(entityName, id)) {
          result.setId(id);
        }
        // force new
        result.setNewOBObject(true);

        // keep it here so it can be found later
        data.put(getKey(entityName, id), result);
      }
View Full Code Here

  }

  // search on the basis of the access level of the entity
  protected BaseOBObject searchInstance(Entity entity, String id) {
    final AccessLevel al = entity.getAccessLevel();
    BaseOBObject result = null;
    if (al == AccessLevel.SYSTEM) {
      result = searchSystem(id, entity);
    } else if (al == AccessLevel.SYSTEM_CLIENT) {
      // search client and system
      result = searchClient(id, entity);
View Full Code Here

TOP

Related Classes of org.openbravo.base.structure.BaseOBObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.