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.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 IdClass entity is a class that is * annotated with {@link javax.persistence.IdClass} 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 IdClassEntitySpecification<T, ID extends Serializable> implements EntitySpecification<T, ID> { static final Logger LOGGER = LoggerFactory.getLogger(IdClassEntitySpecification.class); private final Class<T> entityClazz; private final Class<ID> keyClazz; private final ColumnFamily<String, String> columnFamily; private final String table; private final List<String> keyColumns; private final Map<String, AttributeAccessor> attributeAccessors; private final List<String> mappedColumns; /** * Constructor. * @param entityClazz . * @param keyClazz . */ IdClassEntitySpecification(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.keyColumns = EntitySpecificationUtils.getCompoundKeyColumnNames(this.keyClazz); this.attributeAccessors = EntitySpecificationUtils.createAccessors(this.keyClazz); this.attributeAccessors.putAll(EntitySpecificationUtils.createAccessors(this.entityClazz)); this.mappedColumns = Lists.newArrayList(); for (String columnName : this.attributeAccessors.keySet()) { this.mappedColumns.add(EntitySpecificationUtils.normalizeCqlElementName(columnName)); } } /** * {@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) { return (ID) entity; } /** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public List<ID> getKey(List<T> entities) { List<ID> keys = Lists.newArrayListWithExpectedSize(entities.size()); for (T entity : entities) { keys.add((ID) entity); } return keys; } /** * {@inheritDoc} */ @Override public Map<String, ByteBuffer> getSerializedKeyValues(ID key) { Preconditions.checkNotNull(key); Map<String, ByteBuffer> result = Maps.newLinkedHashMap(); for (String keyColumn : keyColumns) { result.put(keyColumn, EntitySpecificationUtils.serializeAttribute(attributeAccessors.get(keyColumn), key)); } return result; } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public Map<String, ByteBuffer> getSerializedKeyValues(T entity) { Preconditions.checkArgument(keyClazz.isAssignableFrom(entityClazz)); return getSerializedKeyValues((ID) entity); } /** * {@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 : attributeAccessors.entrySet()) { result.put(entry.getKey(), EntitySpecificationUtils.serializeAttribute(entry.getValue(), entity)); } return result; } /** * {@inheritDoc} */ @Override public T map(ColumnList<String> columns) { T entity = BeanUtils.instantiateClass(entityClazz); for (Column<String> column : columns) { String normalizedColumnName = EntitySpecificationUtils.normalizeCqlElementName(column.getName()); if (!attributeAccessors.containsKey(normalizedColumnName)) { LOGGER.warn("The CQL3 column {} isn't mapped to any entity field", normalizedColumnName); continue; } AttributeAccessor accessor = attributeAccessors.get(normalizedColumnName); EntitySpecificationUtils.deserializeAttribute(column, accessor, entity); } return entity; } /** * {@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} */ @Override public List<ByteBuffer> map(T entity) { List<ByteBuffer> serializedValues = Lists.newArrayListWithExpectedSize(attributeAccessors.size()); for (Map.Entry<String, AttributeAccessor> entry : attributeAccessors.entrySet()) { AttributeAccessor accessor = entry.getValue(); ByteBuffer buf = EntitySpecificationUtils.serializeAttribute(accessor, entity); serializedValues.add(buf); } return serializedValues; } /** * {@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; } }