|
| 1 | +package org.springframework.data.couchbase.core; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.springframework.context.ApplicationContext; |
| 6 | +import org.springframework.data.couchbase.core.convert.CouchbaseConverter; |
| 7 | +import org.springframework.data.couchbase.core.convert.join.N1qlJoinResolver; |
| 8 | +import org.springframework.data.couchbase.core.convert.translation.TranslationService; |
| 9 | +import org.springframework.data.couchbase.core.mapping.CouchbaseDocument; |
| 10 | +import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity; |
| 11 | +import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty; |
| 12 | +import org.springframework.data.couchbase.core.mapping.event.AfterSaveEvent; |
| 13 | +import org.springframework.data.couchbase.core.mapping.event.CouchbaseMappingEvent; |
| 14 | +import org.springframework.data.couchbase.repository.support.MappingCouchbaseEntityInformation; |
| 15 | +import org.springframework.data.couchbase.repository.support.TransactionResultHolder; |
| 16 | +import org.springframework.data.mapping.PersistentPropertyAccessor; |
| 17 | +import org.springframework.data.mapping.context.MappingContext; |
| 18 | +import org.springframework.data.mapping.model.ConvertingPropertyAccessor; |
| 19 | + |
| 20 | +import com.couchbase.client.core.error.CouchbaseException; |
| 21 | + |
| 22 | +public abstract class AbstractTemplateSupport { |
| 23 | + |
| 24 | + final CouchbaseConverter converter; |
| 25 | + final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext; |
| 26 | + final TranslationService translationService; |
| 27 | + ApplicationContext applicationContext; |
| 28 | + static final Logger LOG = LoggerFactory.getLogger(AbstractTemplateSupport.class); |
| 29 | + |
| 30 | + public AbstractTemplateSupport(CouchbaseConverter converter, TranslationService translationService) { |
| 31 | + this.converter = converter; |
| 32 | + this.mappingContext = converter.getMappingContext(); |
| 33 | + this.translationService = translationService; |
| 34 | + } |
| 35 | + |
| 36 | + abstract ReactiveCouchbaseTemplate getReactiveTemplate(); |
| 37 | + |
| 38 | + public <T> T decodeEntityBase(String id, String source, long cas, Class<T> entityClass, |
| 39 | + TransactionResultHolder txResultHolder) { |
| 40 | + |
| 41 | + final CouchbaseDocument converted = new CouchbaseDocument(id); |
| 42 | + converted.setId(id); |
| 43 | + CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entityClass); |
| 44 | + if (cas != 0 && persistentEntity.getVersionProperty() != null) { |
| 45 | + converted.put(persistentEntity.getVersionProperty().getName(), cas); |
| 46 | + } |
| 47 | + |
| 48 | + T readEntity = converter.read(entityClass, (CouchbaseDocument) translationService.decode(source, converted)); |
| 49 | + final ConvertingPropertyAccessor<T> accessor = getPropertyAccessor(readEntity); |
| 50 | + |
| 51 | + if (persistentEntity.getVersionProperty() != null) { |
| 52 | + accessor.setProperty(persistentEntity.getVersionProperty(), cas); |
| 53 | + } |
| 54 | + if (persistentEntity.transactionResultProperty() != null) { |
| 55 | + accessor.setProperty(persistentEntity.transactionResultProperty(), txResultHolder); |
| 56 | + } |
| 57 | + N1qlJoinResolver.handleProperties(persistentEntity, accessor, getReactiveTemplate(), id); |
| 58 | + return accessor.getBean(); |
| 59 | + } |
| 60 | + |
| 61 | + public <T> T applyResultBase(T entity, CouchbaseDocument converted, Object id, long cas, |
| 62 | + TransactionResultHolder txResultHolder) { |
| 63 | + ConvertingPropertyAccessor<Object> accessor = getPropertyAccessor(entity); |
| 64 | + |
| 65 | + final CouchbasePersistentEntity<?> persistentEntity = converter.getMappingContext() |
| 66 | + .getRequiredPersistentEntity(entity.getClass()); |
| 67 | + |
| 68 | + final CouchbasePersistentProperty idProperty = persistentEntity.getIdProperty(); |
| 69 | + if (idProperty != null) { |
| 70 | + accessor.setProperty(idProperty, id); |
| 71 | + } |
| 72 | + |
| 73 | + final CouchbasePersistentProperty versionProperty = persistentEntity.getVersionProperty(); |
| 74 | + if (versionProperty != null) { |
| 75 | + accessor.setProperty(versionProperty, cas); |
| 76 | + } |
| 77 | + |
| 78 | + final CouchbasePersistentProperty transactionResultProperty = persistentEntity.transactionResultProperty(); |
| 79 | + if (transactionResultProperty != null) { |
| 80 | + accessor.setProperty(transactionResultProperty, txResultHolder); |
| 81 | + } |
| 82 | + maybeEmitEvent(new AfterSaveEvent(accessor.getBean(), converted)); |
| 83 | + return (T) accessor.getBean(); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + public Long getCas(final Object entity) { |
| 88 | + final ConvertingPropertyAccessor<Object> accessor = getPropertyAccessor(entity); |
| 89 | + final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entity.getClass()); |
| 90 | + final CouchbasePersistentProperty versionProperty = persistentEntity.getVersionProperty(); |
| 91 | + |
| 92 | + long cas = 0; |
| 93 | + if (versionProperty != null) { |
| 94 | + Object casObject = accessor.getProperty(versionProperty); |
| 95 | + if (casObject instanceof Number) { |
| 96 | + cas = ((Number) casObject).longValue(); |
| 97 | + } |
| 98 | + } |
| 99 | + return cas; |
| 100 | + } |
| 101 | + |
| 102 | + public String getJavaNameForEntity(final Class<?> clazz) { |
| 103 | + final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz); |
| 104 | + MappingCouchbaseEntityInformation<?, Object> info = new MappingCouchbaseEntityInformation<>(persistentEntity); |
| 105 | + return info.getJavaType().getName(); |
| 106 | + } |
| 107 | + |
| 108 | + <T> ConvertingPropertyAccessor<T> getPropertyAccessor(final T source) { |
| 109 | + CouchbasePersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(source.getClass()); |
| 110 | + PersistentPropertyAccessor<T> accessor = entity.getPropertyAccessor(source); |
| 111 | + return new ConvertingPropertyAccessor<>(accessor, converter.getConversionService()); |
| 112 | + } |
| 113 | + |
| 114 | + public <T> TransactionResultHolder getTxResultHolder(T source) { |
| 115 | + final CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(source.getClass()); |
| 116 | + final CouchbasePersistentProperty transactionResultProperty = persistentEntity.transactionResultProperty(); |
| 117 | + if (transactionResultProperty == null) { |
| 118 | + throw new CouchbaseException("the entity class " + source.getClass() |
| 119 | + + " does not have a property required for transactions:\n\t@TransactionResult TransactionResultHolder txResultHolder"); |
| 120 | + } |
| 121 | + return getPropertyAccessor(source).getProperty(transactionResultProperty, TransactionResultHolder.class); |
| 122 | + } |
| 123 | + |
| 124 | + public void maybeEmitEvent(CouchbaseMappingEvent<?> event) { |
| 125 | + if (canPublishEvent()) { |
| 126 | + try { |
| 127 | + this.applicationContext.publishEvent(event); |
| 128 | + } catch (Exception e) { |
| 129 | + LOG.warn("{} thrown during {}", e, event); |
| 130 | + throw e; |
| 131 | + } |
| 132 | + } else { |
| 133 | + LOG.info("maybeEmitEvent called, but CouchbaseTemplate not initialized with applicationContext"); |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + private boolean canPublishEvent() { |
| 139 | + return this.applicationContext != null; |
| 140 | + } |
| 141 | +} |
0 commit comments