Package org.springframework.data.mapping.model

Examples of org.springframework.data.mapping.model.MappingException


   */
  public static String encodeByteArray(byte[] byteArray) {
    try {
      return new String(Base64.encodeBase64(byteArray), UTF8_ENCODING);
    } catch(UnsupportedEncodingException e) {
      throw new MappingException("Could not encode byteArray to UTF8 encoding", e);
    }
  }
View Full Code Here


   */
  public static byte[] decodeByteArray(String value) throws ParseException {
    try {
      return Base64.decodeBase64(value.getBytes(UTF8_ENCODING));
    } catch(UnsupportedEncodingException e) {
      throw new MappingException("Could not decode byteArray to UTF8 encoding", e);
    }

  }
View Full Code Here

        return references;
    }

  private static MappingException toMappingException(Exception cause, String accessMethod, String fieldName,
      Object fieldObject) {
    return new MappingException("Could not call " + accessMethod + " for field " + fieldName + " in class:  "
        + fieldObject.getClass(), cause);
  }
View Full Code Here

      Links existingLinks = new Links(links);

      for (Link link : associationLinks.getLinksFor(association, basePath)) {
        if (existingLinks.hasLink(link.getRel())) {
          throw new MappingException(String.format(AMBIGUOUS_ASSOCIATIONS, property.toString()));
        } else {
          links.add(link);
        }
      }
View Full Code Here

    private boolean isNodeEntity(TypeInformation<?> targetType) {
        final Class<?> type = targetType.getType();
        if (type.isAnnotationPresent(NodeEntity.class)) return true;
        if (type.isAnnotationPresent(RelationshipEntity.class)) return false;
        throw new MappingException("Target type for relationship " + this.type + " field is invalid " + type);
    }
View Full Code Here

  private static Object getMappedId(Object o) {
    if (ClassUtils.hasProperty(o.getClass(), "id")) {
      try {
        return FieldUtils.readDeclaredField(o, "id", true);
      } catch (IllegalAccessException e) {
        throw new MappingException("Id property could not be accessed!", e);
      }
    }

    for (java.lang.reflect.Field field : o.getClass().getDeclaredFields()) {
      Annotation annotation = AnnotationUtils.getAnnotation(field, Id.class);
      if (annotation != null) {
        try {
          return FieldUtils.readField(field, o, true);
        } catch (IllegalArgumentException e) {
          throw new MappingException("Id property could not be accessed!", e);
        } catch (IllegalAccessException e) {
          throw new MappingException("Id property could not be accessed!", e);
        }
      }
    }
    throw new MappingException("Id property could not be found!");
  }
View Full Code Here

    /*
     * Ensure that this is not both a @Table(@Persistent) and a @PrimaryKey
     */
    if (isTable && isPrimaryKeyClass) {
      exceptions.add(new MappingException("Entity cannot be of type Table and PrimaryKey"));
      throw exceptions;
    }

    /*
     * Ensure that this is either a @Table(@Persistent) or a @PrimaryKey
     */
    if (!isTable && !isPrimaryKeyClass) {
      exceptions.add(new MappingException(
          "Cassandra entities must have the @Table, @Persistent or @PrimaryKeyClass Annotation"));
      throw exceptions;
    }

    /*
     * Parse the properties
     */
    entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {

      @Override
      public void doWithPersistentProperty(CassandraPersistentProperty p) {

        if (p.isIdProperty()) {
          idProperties.add(p);
        } else if (p.isCompositePrimaryKey()) {
          compositePrimaryKeys.add(p);
        } else if (p.isPartitionKeyColumn()) {
          partitionKeyColumns.add(p);
          primaryKeyColumns.add(p);
        } else if (p.isClusterKeyColumn()) {
          clusterKeyColumns.add(p);
          primaryKeyColumns.add(p);
        }
      }
    });

    final int idPropertyCount = idProperties.size();
    final int partitionKeyColumnCount = partitionKeyColumns.size();
    final int primaryKeyColumnCount = primaryKeyColumns.size();

    /*
     * Perform rules verification on PrimaryKeyClass
     */
    if (isPrimaryKeyClass) {

      /*
       * Must have at least 1 attribute annotated with @PrimaryKeyColumn
       */
      if (primaryKeyColumnCount == 0) {
        exceptions.add(new MappingException(String.format(
            "composite primary key type [%s] has no fields annotated with @%s", entity.getType().getName(),
            PrimaryKeyColumn.class.getSimpleName())));
      }

      /*
       * At least one of the PrimaryKeyColumns must have a type PARTIONED
       */
      if (partitionKeyColumnCount == 0) {
        exceptions.add(new MappingException(
            "At least one of the @PrimaryKeyColumn annotation must have a type of PARTITIONED"));
      }

      /*
       * Cannot have any Id or PrimaryKey Annotations
       */
      if (idPropertyCount > 0) {
        exceptions.add(new MappingException(
            "Annotations @Id and @PrimaryKey are invalid for type annotated with @PrimaryKeyClass"));
      }

      /*
       * Ensure that PrimaryKeyColumn is a supported Type.
       */
      for (CassandraPersistentProperty p : primaryKeyColumns) {
        if (CassandraSimpleTypeHolder.getDataTypeFor(p.getType()) == null) {
          exceptions.add(new MappingException("Fields annotated with @PrimaryKeyColumn must be simple CassandraTypes"));
        }
      }

      /*
       * Ensure PrimaryKeyClass is Serializable
       */
      if (!Serializable.class.isAssignableFrom(thisType)) {
        exceptions.add(new MappingException("@PrimaryKeyClass must be Serializable"));
      }

      /*
       * Ensure PrimaryKeyClass only extends Object
       */
      if (!thisType.getSuperclass().equals(Object.class)) {
        exceptions.add(new MappingException("@PrimaryKeyClass must only extend Object"));
      }

      /*
       * Check that PrimaryKeyClass overrides "boolean equals(Object)"
       */
      try {
        Method equalsMethod = thisType.getDeclaredMethod("equals", Object.class);
        if (equalsMethod == null || !equalsMethod.getDeclaringClass().equals(thisType)) {
          throw new NoSuchMethodException();
        }
      } catch (NoSuchMethodException e) {
        String message = "@PrimaryKeyClass should override 'boolean equals(Object)' method and use all @PrimaryKeyColumn fields";
        if (strict) {
          exceptions.add(new MappingException(message, e));
        } else {
          log.warn(message);
        }
      }

      /*
       * Ensure PrimaryKeyClass overrides "int hashCode()"
       */
      try {
        Method hashCodeMethod = thisType.getDeclaredMethod("hashCode", (Class<?>[]) null);
        if (hashCodeMethod == null || !hashCodeMethod.getDeclaringClass().equals(thisType)) {
          throw new NoSuchMethodException();
        }
      } catch (NoSuchMethodException e) {
        String message = "@PrimaryKeyClass should override 'int hashCode()' method and use all @PrimaryKeyColumn fields";
        if (strict) {
          exceptions.add(new MappingException(message, e));
        } else {
          log.warn(message);
        }
      }
    }

    /*
     * Perform rules verification on Table/Persistent
     */
    if (isTable) {

      /*
       * TODO Verify annotation values with CqlIndentifier
       */

      /*
       * Ensure only one PK or at least one partitioned PKC and not both PK(s) & PKC(s)
       */
      if (primaryKeyColumnCount == 0) {
        /*
         * Can only have one PK.
         */
        if (idPropertyCount != 1) {
          exceptions
              .add(new MappingException(String.format(
                  "@Table/@Persistent types must have only one @PrimaryKey attribute, if any.  Found %s.",
                  idPropertyCount)));
          throw exceptions;
        }
        /*
         * Ensure that Id is a supported Type.  At the point there is only 1.
         */
        Class<?> typeClass = idProperties.get(0).getType();
        if (!typeClass.isAnnotationPresent(PrimaryKeyClass.class)
            && CassandraSimpleTypeHolder.getDataTypeFor(typeClass) == null) {
          exceptions.add(new MappingException(
              "Fields annotated with @PrimaryKey must be simple CassandraTypes or @PrimaryKeyClass type"));
        }
      } else if (idPropertyCount > 0) {
        /*
         * Then we have both PK(s) & PKC(s)
         */
        exceptions
            .add(new MappingException(
                String
                    .format(
                        "@Table/@Persistent types must not define both @PrimaryKeyColumn field%s (found %s) and @PrimaryKey field%s (found %s)",
                        primaryKeyColumnCount == 1 ? "" : "s", primaryKeyColumnCount, idPropertyCount == 1 ? "" : "s",
                        idPropertyCount)));
        throw exceptions;
      } else {
        /*
         * We have no PKs & only PKC(s) -- ensure at least one is of type PARTITIONED
         */
        if (partitionKeyColumnCount == 0) {
          exceptions.add(new MappingException(String
              .format("@Table/@Persistent types must define at least one @PrimaryKeyColumn of type PARTITIONED")));
        }
      }
    }

View Full Code Here

      }

    });

    if (spec.getPartitionKeyColumns().isEmpty()) {
      throw new MappingException("no partition key columns found in the entity " + entity.getType());
    }

    return spec;
  }
View Full Code Here

  public <T> T unmarshall(String jsonString, Class<?> objectType) {
    Assert.notNull(jsonString);
    try {
      return (T) jsonMapper.readValue(jsonString, objectType);
    } catch(IOException e) {
      throw new MappingException("Could not unmarshall object : " + jsonString, e);
    }
  }
View Full Code Here

    jsonMapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);
    jsonMapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "@class");
    try {
      return jsonMapper.writeValueAsString(input);
    } catch(Exception e) {
      throw new MappingException(e.getMessage(), e);
    }
  }
View Full Code Here

TOP

Related Classes of org.springframework.data.mapping.model.MappingException

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.