| 1 | /* |
| 2 | * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.datastax.driver.mapping; |
| 17 | |
| 18 | import com.datastax.driver.core.*; |
| 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | /** |
| 24 | * Serializes a class annotated with {@code @UDT} to the corresponding CQL user-defined type. |
| 25 | */ |
| 26 | class MappedUDTCodec<T> extends TypeCodec.AbstractUDTCodec<T> { |
| 27 | private final UserType cqlUserType; |
| 28 | private final Class<T> udtClass; |
| 29 | private final Map<String, ColumnMapper<T>> columnMappers; |
| 30 | private final CodecRegistry codecRegistry; |
| 31 | |
| 32 | public MappedUDTCodec(UserType cqlUserType, Class<T> udtClass, Map<String, ColumnMapper<T>> columnMappers, MappingManager mappingManager) { |
| 33 | super(cqlUserType, udtClass); |
| 34 | this.cqlUserType = cqlUserType; |
| 35 | this.udtClass = udtClass; |
| 36 | this.columnMappers = columnMappers; |
| 37 | this.codecRegistry = mappingManager.getSession().getCluster().getConfiguration().getCodecRegistry(); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | protected T newInstance() { |
| 42 | try { |
| 43 | return udtClass.newInstance(); |
| 44 | } catch (Exception e) { |
| 45 | throw new IllegalArgumentException("Error creating instance of @UDT-annotated class " + udtClass, e); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | Class<T> getUdtClass() { |
| 50 | return udtClass; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected ByteBuffer serializeField(T source, String fieldName, ProtocolVersion protocolVersion) { |
| 55 | // The parent class passes lowercase names unquoted, but in our internal map of mappers they are always quoted |
| 56 | if (!fieldName.startsWith("\"")) |
| 57 | fieldName = Metadata.quote(fieldName); |
| 58 | |
| 59 | ColumnMapper<T> columnMapper = columnMappers.get(fieldName); |
| 60 | |
| 61 | if (columnMapper == null) |
| 62 | return null; |
| 63 | |
| 64 | Object value = columnMapper.getValue(source); |
| 65 | |
| 66 | TypeCodec<Object> codec = columnMapper.getCustomCodec(); |
| 67 | if (codec == null) |
| 68 | codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType()); |
| 69 | |
| 70 | return codec.serialize(value, protocolVersion); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | protected T deserializeAndSetField(ByteBuffer input, T target, String fieldName, ProtocolVersion protocolVersion) { |
| 75 | if (!fieldName.startsWith("\"")) |
| 76 | fieldName = Metadata.quote(fieldName); |
| 77 | |
| 78 | ColumnMapper<T> columnMapper = columnMappers.get(fieldName); |
| 79 | if (columnMapper != null) { |
| 80 | TypeCodec<Object> codec = columnMapper.getCustomCodec(); |
| 81 | if (codec == null) |
| 82 | codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType()); |
| 83 | columnMapper.setValue(target, codec.deserialize(input, protocolVersion)); |
| 84 | } |
| 85 | return target; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected String formatField(T source, String fieldName) { |
| 90 | if (!fieldName.startsWith("\"")) |
| 91 | fieldName = Metadata.quote(fieldName); |
| 92 | |
| 93 | ColumnMapper<T> columnMapper = columnMappers.get(fieldName); |
| 94 | |
| 95 | if (columnMapper == null) |
| 96 | return null; |
| 97 | |
| 98 | Object value = columnMapper.getValue(source); |
| 99 | |
| 100 | TypeCodec<Object> codec = columnMapper.getCustomCodec(); |
| 101 | if (codec == null) |
| 102 | codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType()); |
| 103 | |
| 104 | return codec.format(value); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | protected T parseAndSetField(String input, T target, String fieldName) { |
| 109 | if (!fieldName.startsWith("\"")) |
| 110 | fieldName = Metadata.quote(fieldName); |
| 111 | |
| 112 | ColumnMapper<T> columnMapper = columnMappers.get(fieldName); |
| 113 | if (columnMapper != null) { |
| 114 | TypeCodec<Object> codec = columnMapper.getCustomCodec(); |
| 115 | if (codec == null) |
| 116 | codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType()); |
| 117 | columnMapper.setValue(target, codec.parse(input)); |
| 118 | } |
| 119 | return target; |
| 120 | } |
| 121 | } |