private Object unmarshall(PersistentEntity persistentEntity, Long id, Map<String, Object> data) {

    log.debug("unmarshalling entity {}, props {}, {}", id, data);
    EntityAccess entityAccess = new EntityAccess(persistentEntity, persistentEntity.newInstance());
    entityAccess.setConversionService(persistentEntity.getMappingContext().getConversionService());
    entityAccess.setIdentifier(id);
    data.remove("__id__");

    for (PersistentProperty property :
        entityAccess.getPersistentEntity().getPersistentProperties()) {

      String propertyName = property.getName();
      if (property instanceof Simple) { // implicitly sets version property as well
        entityAccess.setProperty(propertyName, data.remove(propertyName));
        //            } else if (property instanceof OneToOne) {
        //                log.error("property " + property.getName() + " is of type " +
        // property.getClass().getSuperclass());
      } else if (property instanceof ToOne) {
        ToOne to = (ToOne) property;

        CypherResult cypherResult =
            getSession()
                .getNativeInterface()
                .execute(
                    CypherBuilder.findRelationshipEndpointIdsFor(to),
                    Collections.singletonMap("id", id));

        Map<String, Object> row = IteratorUtil.singleOrNull(cypherResult);
        if (row != null) {
          Long endpointId = (Long) row.get("id");
          entityAccess.setProperty(
              propertyName,
              getMappingContext()
                  .getProxyFactory()
                  .createProxy(session, to.getAssociatedEntity().getJavaClass(), endpointId));
        }
      } else if ((property instanceof OneToMany) || (property instanceof ManyToMany)) {

        LazyEnititySet lazyEnititySet =
            new LazyEnititySet(
                entityAccess,
                (Association) property,
                getMappingContext().getProxyFactory(),
                getSession());
        entityAccess.setProperty(propertyName, lazyEnititySet);

      } else {
        throw new IllegalArgumentException(
            "property $property.name is of type ${property.class.superclass}");
      }
    }

    if (!data.isEmpty()) {
      GroovyObject go = (GroovyObject) (entityAccess.getEntity());
      go.setProperty(Neo4jGormEnhancer.UNDECLARED_PROPERTIES, data);
    }

    firePostLoadEvent(entityAccess.getPersistentEntity(), entityAccess);
    return entityAccess.getEntity();
  }
Beispiel #2
0
  void init2ndPass() {
    init2ndPassNamesWithDefaults();

    for (int i = 0; i < properties.size(); i++) {
      Property property = properties.get(i);
      property.setOrdinal(i);
      property.init2ndPass();
      if (property.isPrimaryKey()) {
        propertiesPk.add(property);
      } else {
        propertiesNonPk.add(property);
      }
    }

    if (propertiesPk.size() == 1) {
      pkProperty = propertiesPk.get(0);
      pkType = schema.mapToJavaTypeNullable(pkProperty.getPropertyType());
    } else {
      pkType = "Void";
    }

    propertiesColumns = new ArrayList<Property>(properties);
    for (ToOne toOne : toOneRelations) {
      toOne.init2ndPass();
      Property[] fkProperties = toOne.getFkProperties();
      for (Property fkProperty : fkProperties) {
        if (!propertiesColumns.contains(fkProperty)) {
          propertiesColumns.add(fkProperty);
        }
      }
    }

    for (ToMany toMany : toManyRelations) {
      toMany.init2ndPass();
      // Source Properties may not be virtual, so we do not need the following code:
      // for (Property sourceProperty : toMany.getSourceProperties()) {
      // if (!propertiesColumns.contains(sourceProperty)) {
      // propertiesColumns.add(sourceProperty);
      // }
      // }
    }

    if (active == null) {
      active = schema.isUseActiveEntitiesByDefault();
    }
    active |= !toOneRelations.isEmpty() || !toManyRelations.isEmpty();

    if (hasKeepSections == null) {
      hasKeepSections = schema.isHasKeepSectionsByDefault();
    }

    init2ndPassIndexNamesWithDefaults();

    for (ContentProvider contentProvider : contentProviders) {
      contentProvider.init2ndPass();
    }
  }
Beispiel #3
0
 public ToOne addToOneWithoutProperty(
     String name, Entity target, String fkColumnName, boolean notNull, boolean unique) {
   PropertyBuilder propertyBuilder = new PropertyBuilder(schema, this, null, name);
   if (notNull) {
     propertyBuilder.notNull();
   }
   if (unique) {
     propertyBuilder.unique();
   }
   propertyBuilder.columnName(fkColumnName);
   Property column = propertyBuilder.getProperty();
   Property[] fkColumns = {column};
   ToOne toOne = new ToOne(schema, this, target, fkColumns, false);
   toOne.setName(name);
   toOneRelations.add(toOne);
   return toOne;
 }
Beispiel #4
0
  private void init3rdPassAdditionalImports() {
    if (active && !javaPackage.equals(javaPackageDao)) {
      additionalImportsEntity.add(javaPackageDao + "." + classNameDao);
    }

    for (ToOne toOne : toOneRelations) {
      Entity targetEntity = toOne.getTargetEntity();
      checkAdditionalImportsEntityTargetEntity(targetEntity);
      // For deep loading
      if (!targetEntity.getJavaPackage().equals(javaPackageDao)) {
        additionalImportsDao.add(targetEntity.getJavaPackage() + "." + targetEntity.getClassName());
      }
    }

    for (ToMany toMany : toManyRelations) {
      Entity targetEntity = toMany.getTargetEntity();
      checkAdditionalImportsEntityTargetEntity(targetEntity);
    }
  }
Beispiel #5
0
  private void init3rdPassRelations() {
    Set<String> toOneNames = new HashSet<String>();
    for (ToOne toOne : toOneRelations) {
      toOne.init3ndPass();
      if (!toOneNames.add(toOne.getName().toLowerCase())) {
        throw new RuntimeException("Duplicate name for " + toOne);
      }
    }

    Set<String> toManyNames = new HashSet<String>();
    for (ToMany toMany : toManyRelations) {
      toMany.init3ndPass();
      Entity targetEntity = toMany.getTargetEntity();
      for (Property targetProperty : toMany.getTargetProperties()) {
        if (!targetEntity.propertiesColumns.contains(targetProperty)) {
          targetEntity.propertiesColumns.add(targetProperty);
        }
      }
      if (!toManyNames.add(toMany.getName().toLowerCase())) {
        throw new RuntimeException("Duplicate name for " + toMany);
      }
    }
  }
  private void persistAssociationsOfEntity(
      PersistentEntity pe, EntityAccess entityAccess, boolean isUpdate) {

    Object obj = entityAccess.getEntity();
    DirtyCheckable dirtyCheckable = null;
    if (obj instanceof DirtyCheckable) {
      dirtyCheckable = (DirtyCheckable) obj;
    }

    for (PersistentProperty pp : pe.getAssociations()) {
      if ((!isUpdate) || ((dirtyCheckable != null) && dirtyCheckable.hasChanged(pp.getName()))) {

        Object propertyValue = entityAccess.getProperty(pp.getName());

        if ((pp instanceof OneToMany) || (pp instanceof ManyToMany)) {
          Association association = (Association) pp;

          if (propertyValue != null) {

            if (association.isBidirectional()) { // Populate other side of bidi
              for (Object associatedObject : (Iterable) propertyValue) {
                EntityAccess assocEntityAccess =
                    createEntityAccess(association.getAssociatedEntity(), associatedObject);
                assocEntityAccess.setProperty(association.getReferencedPropertyName(), obj);
              }
            }

            Iterable targets = (Iterable) propertyValue;
            persistEntities(association.getAssociatedEntity(), targets);

            boolean reversed = RelationshipUtils.useReversedMappingFor(association);

            if (!reversed) {
              if (!(propertyValue instanceof LazyEnititySet)) {
                LazyEnititySet les =
                    new LazyEnititySet(
                        entityAccess,
                        association,
                        getMappingContext().getProxyFactory(),
                        getSession());
                les.addAll(targets);
                entityAccess.setProperty(association.getName(), les);
              }
            }
          }
        } else if (pp instanceof ToOne) {
          if (propertyValue != null) {
            ToOne to = (ToOne) pp;

            if (to.isBidirectional()) { // Populate other side of bidi
              EntityAccess assocEntityAccess =
                  createEntityAccess(to.getAssociatedEntity(), propertyValue);
              if (to instanceof OneToOne) {
                assocEntityAccess.setProperty(to.getReferencedPropertyName(), obj);
              } else {
                Collection collection =
                    (Collection) assocEntityAccess.getProperty(to.getReferencedPropertyName());
                if (collection == null) {
                  collection = new ArrayList();
                  assocEntityAccess.setProperty(to.getReferencedPropertyName(), collection);
                }
                if (!collection.contains(obj)) {
                  collection.add(obj);
                }
              }
            }

            persistEntity(to.getAssociatedEntity(), propertyValue);

            boolean reversed = RelationshipUtils.useReversedMappingFor(to);
            String relType = RelationshipUtils.relationshipTypeUsedFor(to);

            if (!reversed) {
              getSession()
                  .addPendingInsert(
                      new RelationshipPendingInsert(
                          entityAccess,
                          relType,
                          new EntityAccess(to.getAssociatedEntity(), propertyValue),
                          getCypherEngine()));
            }
          }
        } else {
          throw new IllegalArgumentException(
              "wtf don't know how to handle " + pp + "(" + pp.getClass() + ")");
        }
      }
    }
  }
Beispiel #7
0
 /**
  * Convenience for {@link #addToOne(Entity, Property)} with a subsequent call to {@link
  * ToOne#setName(String)}.
  */
 public ToOne addToOne(Entity target, Property fkProperty, String name) {
   ToOne toOne = addToOne(target, fkProperty);
   toOne.setName(name);
   return toOne;
 }