Java tutorial
/* * Copyright 2013 EK3 Technologies, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ei.ne.ke.cassandra.cql3; import java.io.Serializable; import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.util.List; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.netflix.astyanax.model.Column; import com.netflix.astyanax.model.ColumnFamily; import com.netflix.astyanax.model.ColumnList; import com.netflix.astyanax.model.Row; import com.netflix.astyanax.model.Rows; /** * Helper class for AstyanaxCql3Repository. An embedded entity is a class that * is annotated with {@link javax.persistence.Embeddable} and whose fields are * all annotated only with {@link javax.persistence.Column}. * @param <T> the entity type * @param <ID> the key type */ public final class CompoundEntitySpecification<T, ID extends Serializable> implements EntitySpecification<T, ID> { static final Logger LOGGER = LoggerFactory.getLogger(CompoundEntitySpecification.class); private final Class<T> entityClazz; private final Class<ID> keyClazz; private final ColumnFamily<String, String> columnFamily; private final String table; private final Field keyField; private final List<String> keyColumns; private final Map<String, AttributeAccessor> simpleAttributeAccessors; private final Map<String, AttributeAccessor> embeddedAttributeAccessors; private final List<String> mappedColumns; /** * Constructor. * @param entityClazz . * @param keyClazz . */ CompoundEntitySpecification(Class<T> entityClazz, Class<ID> keyClazz) { this.entityClazz = Preconditions.checkNotNull(entityClazz); this.keyClazz = Preconditions.checkNotNull(keyClazz); this.columnFamily = EntitySpecificationUtils.inferColumnFamily(this.entityClazz); this.table = this.columnFamily.getName(); this.keyField = EntitySpecificationUtils.findCompoundKeyField(this.entityClazz); this.keyColumns = EntitySpecificationUtils.getCompoundKeyColumnNames(this.keyField.getType()); this.simpleAttributeAccessors = EntitySpecificationUtils.createAccessors(this.entityClazz); this.embeddedAttributeAccessors = EntitySpecificationUtils.createAccessors(this.keyClazz); this.mappedColumns = Lists.newArrayList(); for (Map.Entry<String, AttributeAccessor> entry : this.embeddedAttributeAccessors.entrySet()) { this.mappedColumns.add(entry.getKey()); } for (Map.Entry<String, AttributeAccessor> entry : this.simpleAttributeAccessors.entrySet()) { this.mappedColumns.add(entry.getKey()); } } /** * {@inheritDoc} */ @Override public ColumnFamily<String, String> getColumnFamily() { return columnFamily; } /** * {@inheritDoc} */ @Override public String getTable() { return table; } /** * {@inheritDoc} */ @Override public List<String> getKeyColumns() { return keyColumns; } /** * {@inheritDoc} */ @Override public List<String> getMappedColumns() { return mappedColumns; } /** * {@inheritDoc} */ @Override public ID constructKey() { return BeanUtils.instantiateClass(keyClazz); } /** * {@inheritDoc} */ @Override public T constructEntity() { return BeanUtils.instantiateClass(entityClazz); } /** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public ID getKey(T entity) { try { return (ID) keyField.get(entity); } catch (IllegalAccessException e) { throw new IllegalStateException("Couldn't access key field annotated", e); } } /** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public List<ID> getKey(List<T> entities) { try { List<ID> keys = Lists.newArrayListWithExpectedSize(entities.size()); for (T entity : entities) { keys.add((ID) keyField.get(entity)); } return keys; } catch (IllegalAccessException e) { throw new IllegalStateException("Couldn't access key field annotated", e); } } /** * {@inheritDoc} */ @Override public Map<String, ByteBuffer> getSerializedKeyValues(ID key) { Preconditions.checkNotNull(key); Map<String, ByteBuffer> result = Maps.newLinkedHashMap(); for (Map.Entry<String, AttributeAccessor> entry : embeddedAttributeAccessors.entrySet()) { AttributeAccessor accessor = entry.getValue(); result.put(entry.getKey(), EntitySpecificationUtils.serializeAttribute(accessor, key)); } return result; } /** * {@inheritDoc} */ @Override public Map<String, ByteBuffer> getSerializedKeyValues(T entity) { Preconditions.checkNotNull(entity); Map<String, ByteBuffer> result = Maps.newLinkedHashMap(); ID key = getKey(entity); for (Map.Entry<String, AttributeAccessor> entry : embeddedAttributeAccessors.entrySet()) { result.put(entry.getKey(), EntitySpecificationUtils.serializeAttribute(entry.getValue(), key)); } return result; } /** * {@inheritDoc} */ @Override public Map<String, ByteBuffer> serialize(T entity) { Preconditions.checkNotNull(entity); Map<String, ByteBuffer> result = Maps.newLinkedHashMap(); result.putAll(getSerializedKeyValues(entity)); for (Map.Entry<String, AttributeAccessor> entry : simpleAttributeAccessors.entrySet()) { result.put(entry.getKey(), EntitySpecificationUtils.serializeAttribute(entry.getValue(), entity)); } return result; } /** * {@inheritDoc} */ @Override public T map(ColumnList<String> columns) { try { T entity = BeanUtils.instantiateClass(entityClazz); ID embeddedEntity = BeanUtils.instantiateClass(keyClazz); for (Column<String> column : columns) { String normalizedColumnName = EntitySpecificationUtils.normalizeCqlElementName(column.getName()); if (simpleAttributeAccessors.containsKey(normalizedColumnName)) { AttributeAccessor accessor = simpleAttributeAccessors.get(normalizedColumnName); EntitySpecificationUtils.deserializeAttribute(column, accessor, entity); } else if (embeddedAttributeAccessors.containsKey(normalizedColumnName)) { AttributeAccessor accessor = embeddedAttributeAccessors.get(normalizedColumnName); EntitySpecificationUtils.deserializeAttribute(column, accessor, embeddedEntity); } else { LOGGER.warn("The CQL3 column {} isn't mapped to any entity field", normalizedColumnName); } } keyField.set(entity, embeddedEntity); return entity; } catch (IllegalAccessException e) { throw new IllegalStateException("Couldn't access field annotated with @EmbeddedId", e); } } /** * {@inheritDoc} */ @Override public List<T> map(Rows<String, String> rows) { List<T> entities = Lists.newArrayListWithExpectedSize(rows.size()); for (Row<String, String> row : rows) { entities.add(map(row.getColumns())); } return entities; } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public List<ByteBuffer> map(T entity) { try { List<ByteBuffer> serializedValues = Lists.newArrayListWithExpectedSize( embeddedAttributeAccessors.size() + simpleAttributeAccessors.size()); ID embeddedEntity = (ID) keyField.get(entity); for (Map.Entry<String, AttributeAccessor> entry : embeddedAttributeAccessors.entrySet()) { AttributeAccessor accessor = entry.getValue(); ByteBuffer buf = EntitySpecificationUtils.serializeAttribute(accessor, embeddedEntity); serializedValues.add(buf); } for (Map.Entry<String, AttributeAccessor> entry : simpleAttributeAccessors.entrySet()) { AttributeAccessor accessor = entry.getValue(); ByteBuffer buf = EntitySpecificationUtils.serializeAttribute(accessor, entity); serializedValues.add(buf); } return serializedValues; } catch (IllegalAccessException e) { throw new IllegalStateException("Couldn't access field annotated with @Column", e); } } /** * {@inheritDoc} */ @Override public List<List<ByteBuffer>> map(List<T> entities) { List<List<ByteBuffer>> serializedEntities = Lists.newArrayListWithExpectedSize(entities.size()); for (T entity : entities) { serializedEntities.add(map(entity)); } return serializedEntities; } }