List of usage examples for org.springframework.data.mapping PropertyHandler PropertyHandler
PropertyHandler
From source file:com.turbospaces.model.BO.java
/** * @return collection of persistent properties sorted in alphabetical order *//*from w w w . j a v a 2s . com*/ public PersistentProperty[] getOrderedProperties() { if (orderedProperties == null) { final List<PersistentProperty> nonOrderedProperties = Lists.newLinkedList(); // Potentially non-ordered properties, add to temporary set and then sort getOriginalPersistentEntity().doWithProperties(new PropertyHandler() { @Override public void doWithPersistentProperty(final PersistentProperty persistentProperty) { if (!persistentProperty.equals(getOriginalPersistentEntity().getIdProperty()) && !persistentProperty.equals(getOptimisticLockVersionProperty()) && !persistentProperty.equals(getRoutingProperty())) nonOrderedProperties.add(persistentProperty); } }); // sort properties in alphabetical order Collections.sort(nonOrderedProperties, new Comparator<PersistentProperty>() { @Override public int compare(final PersistentProperty o1, final PersistentProperty o2) { return o1.getName().compareTo(o2.getName()); } }); // construct ordered properties lists where idProperty is first and version is second List<PersistentProperty> ordered = Lists.newLinkedList(); if (getOptimisticLockVersionProperty() != null) ordered.add(getOptimisticLockVersionProperty()); if (getRoutingProperty() != null) ordered.add(getRoutingProperty()); // set id field first - we need this optimization to be fast in matchById reading ordered.add(getIdIndex(), getIdProperty()); ordered.addAll(nonOrderedProperties); orderedProperties = ordered.toArray(new PersistentProperty[ordered.size()]); } return orderedProperties; }
From source file:com.joyveb.dbpimpl.cass.prepare.convert.MappingCassandraConverter.java
@Override public AlterTableSpecification getAlterTableSpecification(final CassandraPersistentEntity<?> entity, final TableMetadata table, final boolean dropRemovedAttributeColumns) { final AlterTableSpecification spec = new AlterTableSpecification(); spec.name(entity.getTableName());/*from w ww. jav a 2 s . c om*/ final Set<String> definedColumns = dropRemovedAttributeColumns ? new HashSet<String>() : Collections.<String>emptySet(); doWithAllProperties(entity, new PropertyHandler<CassandraPersistentProperty>() { public void doWithPersistentProperty(CassandraPersistentProperty prop) { String columnName = prop.getColumnName(); DataType columnDataType = prop.getDataType(); String tableColumnName = columnName.toLowerCase(); if (dropRemovedAttributeColumns) { definedColumns.add(tableColumnName); } ColumnMetadata columnMetadata = table.getColumn(tableColumnName); if (columnMetadata != null && columnDataType.equals(columnMetadata.getType())) { return; } if (prop.isIdProperty() || prop.getKeyPart() != null) { throw new MappingException("unable to add or alter column in the primary index " + columnName + " for entity " + entity.getName()); } else { if (columnMetadata == null) { spec.add(columnName, columnDataType); } else { spec.alter(columnName, columnDataType); } } } }); if (dropRemovedAttributeColumns) { for (ColumnMetadata columnMetadata : table.getColumns()) { String columnName = columnMetadata.getName(); if (!definedColumns.contains(columnName)) { spec.drop(columnName); } } } return spec; }
From source file:com.turbospaces.model.BO.java
/** * register the set of persistent classes and enrich kryo with some extract serialized related to persistent class. * // w ww .j av a 2s .c o m * @param kryo * serialization provider * @param persistentEntities * classes to register * @throws ClassNotFoundException * re-throw conversion service * @throws NoSuchMethodException * re-throw cglib's exception * @throws SecurityException * re-throw cglib's exception * @throws IntrospectionException * re-throw introspection exception */ public static void registerPersistentClasses(final DecoratedKryo kryo, final BasicPersistentEntity... persistentEntities) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IntrospectionException { for (BasicPersistentEntity<?, ?> e : persistentEntities) { BO bo = new BO(e); bo.getOriginalPersistentEntity().doWithProperties(new PropertyHandler() { @Override public void doWithPersistentProperty(final PersistentProperty p) { Class type = p.getType(); if (type.isArray() && !kryo.isTypeRegistered(type)) { SingleDimensionArraySerializer serializer = new SingleDimensionArraySerializer(type, kryo); kryo.register(type, serializer); } else if (type.isEnum() && !kryo.isTypeRegistered(type)) { EnumSerializer enumSerializer = new EnumSerializer(type); kryo.register(type, enumSerializer); } } }); Class<?> arrayWrapperType = Class.forName("[L" + e.getType().getName() + ";"); PropertiesSerializer serializer = new PropertiesSerializer(kryo, bo); SingleDimensionArraySerializer arraysSerializer = new SingleDimensionArraySerializer(arrayWrapperType, kryo); kryo.register(e.getType(), serializer); kryo.register(arrayWrapperType, arraysSerializer); } }
From source file:com.joyveb.dbpimpl.cass.prepare.convert.MappingCassandraConverter.java
@Override public List<CreateIndexSpecification> getCreateIndexSpecifications(final CassandraPersistentEntity<?> entity) { final List<CreateIndexSpecification> indexList = new ArrayList<CreateIndexSpecification>(); doWithAllProperties(entity, new PropertyHandler<CassandraPersistentProperty>() { public void doWithPersistentProperty(CassandraPersistentProperty prop) { if (prop.isIdProperty() || prop.getKeyPart() != null) { if (prop.isIndexed()) { throw new MappingException("unable to create index on column in the primary key " + prop.getColumnName() + " for entity " + entity.getName()); }//from ww w . j a v a 2s .c o m return; } if (prop.isIndexed()) { indexList.add(new CreateIndexSpecification().optionalName(prop.getIndexName()) .on(entity.getTableName()).column(prop.getColumnName())); } } }); return indexList; }
From source file:nivance.jpa.cassandra.prepare.convert.MappingCassandraConverter.java
@Override public List<CreateIndexSpecification> getCreateIndexSpecifications(final CassandraPersistentEntity<?> entity) { final List<CreateIndexSpecification> indexList = new ArrayList<CreateIndexSpecification>(); doWithAllProperties(entity, new PropertyHandler<CassandraPersistentProperty>() { public void doWithPersistentProperty(CassandraPersistentProperty prop) { // if (prop.isIdProperty() || prop.getKeyPart() != null) { if (prop.isIdProperty()) { if (prop.isIndexed()) { throw new MappingException("unable to create index on column in the primary key " + prop.getColumnName() + " for entity " + entity.getName()); }/* w ww . j a v a 2s.c o m*/ return; } if (prop.isIndexed()) { indexList.add(new CreateIndexSpecification().optionalName(prop.getIndexName()) .on(entity.getTableName()).column(prop.getColumnName())); } } }); return indexList; }
From source file:com.joyveb.dbpimpl.cass.prepare.convert.MappingCassandraConverter.java
public List<WithNameSpecification<?>> getIndexChangeSpecifications(final CassandraPersistentEntity<?> entity, final TableMetadata table) { final List<WithNameSpecification<?>> list = new ArrayList<WithNameSpecification<?>>(); final Set<String> definedColumns = new HashSet<String>(); doWithAllProperties(entity, new PropertyHandler<CassandraPersistentProperty>() { public void doWithPersistentProperty(CassandraPersistentProperty prop) { if (prop.isIdProperty() || prop.getKeyPart() != null) { if (prop.isIndexed()) { throw new MappingException("unable to create index on column in the primary key " + prop.getColumnName() + " for entity " + entity.getName()); }/* w ww . ja v a2s . com*/ return; } String columnName = prop.getColumnName(); String tableColumnName = columnName.toLowerCase(); definedColumns.add(tableColumnName); ColumnMetadata columnMetadata = table.getColumn(tableColumnName); if (prop.isIndexed() && (columnMetadata == null || columnMetadata.getIndex() == null)) { list.add(new CreateIndexSpecification().optionalName(prop.getIndexName()) .on(entity.getTableName()).column(prop.getColumnName())); } else if (!prop.isIndexed() && columnMetadata != null && columnMetadata.getIndex() != null) { list.add(new DropIndexSpecification().name(columnMetadata.getIndex().getName())); } } }); for (ColumnMetadata columnMetadata : table.getColumns()) { String columnName = columnMetadata.getName(); if (!definedColumns.contains(columnName) && columnMetadata.getIndex() != null) { list.add(new DropIndexSpecification().name(columnMetadata.getIndex().getName())); } } return list; }
From source file:nivance.jpa.cassandra.prepare.convert.MappingCassandraConverter.java
public List<WithNameSpecification<?>> getIndexChangeSpecifications(final CassandraPersistentEntity<?> entity, final TableMetadata table) { final List<WithNameSpecification<?>> list = new ArrayList<WithNameSpecification<?>>(); final Set<String> definedColumns = new HashSet<String>(); doWithAllProperties(entity, new PropertyHandler<CassandraPersistentProperty>() { public void doWithPersistentProperty(CassandraPersistentProperty prop) { // if (prop.isIdProperty() || prop.getKeyPart() != null) { if (prop.isIdProperty()) { if (prop.isIndexed()) { throw new MappingException("unable to create index on column in the primary key " + prop.getColumnName() + " for entity " + entity.getName()); }/*from ww w .j a va 2 s .c o m*/ return; } String columnName = prop.getColumnName(); String tableColumnName = columnName.toLowerCase(); definedColumns.add(tableColumnName); ColumnMetadata columnMetadata = table.getColumn(tableColumnName); if (prop.isIndexed() && (columnMetadata == null || columnMetadata.getIndex() == null)) { list.add(new CreateIndexSpecification().optionalName(prop.getIndexName()) .on(entity.getTableName()).column(prop.getColumnName())); } else if (!prop.isIndexed() && columnMetadata != null && columnMetadata.getIndex() != null) { list.add(new DropIndexSpecification().name(columnMetadata.getIndex().getName())); } } }); for (ColumnMetadata columnMetadata : table.getColumns()) { String columnName = columnMetadata.getName(); if (!definedColumns.contains(columnName) && columnMetadata.getIndex() != null) { list.add(new DropIndexSpecification().name(columnMetadata.getIndex().getName())); } } return list; }
From source file:com.joyveb.dbpimpl.cass.prepare.convert.MappingCassandraConverter.java
public List<Clause> getPrimaryKey(final CassandraPersistentEntity<?> entity, final Object id) { final List<Clause> result = new LinkedList<Clause>(); entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() { public void doWithPersistentProperty(CassandraPersistentProperty prop) { if (prop.isIdProperty()) { if (prop.hasEmbeddableType()) { if (!prop.getRawType().isAssignableFrom(id.getClass())) { throw new MappingException( "id class " + id.getClass() + " can not be converted to embeddedid property " + prop.getColumnName() + " in the entity " + entity.getName()); }// w w w . j a v a2 s.c om embeddedPrimaryKey(prop.getRawType(), id, result, false); } else { result.add(QueryBuilder.eq(prop.getColumnName(), id)); } } } }); if (result.isEmpty()) { throw new MappingException( "Could not form a where clause for the primary key for an entity " + entity.getName()); } return result; }
From source file:nivance.jpa.cassandra.prepare.convert.MappingCassandraConverter.java
public List<Clause> getPrimaryKey(final CassandraPersistentEntity<?> entity, final Object id) { final List<Clause> result = new LinkedList<Clause>(); entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() { public void doWithPersistentProperty(CassandraPersistentProperty prop) { if (prop.isIdProperty()) { if (prop.hasEmbeddableType()) { if (!prop.getRawType().isAssignableFrom(id.getClass())) { throw new MappingException( "id class " + id.getClass() + " can not be converted to embeddedid property " + prop.getColumnName() + " in the entity " + entity.getName()); }//from ww w . j a v a 2 s. co m embeddedPrimaryKey(prop.getRawType(), id, result, false); } else { result.add(QueryBuilder.eq(prop.getColumnName(), id)); } } } }); if (result.isEmpty()) { throw new MappingException( "Could not form a where clause for the primary key for an entity " + entity.getName()); } return result; }
From source file:com.joyveb.dbpimpl.cass.prepare.convert.MappingCassandraConverter.java
@Override public List<Clause> getPartitionKey(final CassandraPersistentEntity<?> entity, final Object id) { final List<Clause> result = new LinkedList<Clause>(); entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() { public void doWithPersistentProperty(CassandraPersistentProperty prop) { if (prop.isIdProperty()) { if (prop.hasEmbeddableType()) { if (!prop.getRawType().isAssignableFrom(id.getClass())) { throw new MappingException( "id class " + id.getClass() + " can not be converted to embeddedid property " + prop.getColumnName() + " in the entity " + entity.getName()); }//from www . j a va2 s . co m embeddedPrimaryKey(prop.getRawType(), id, result, true); } else { result.add(QueryBuilder.eq(prop.getColumnName(), id)); } } } }); return result; }