Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'com.flexcodelabs'
version = '0.0.33'
version = '0.0.34'
description = 'Flextuma App'

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ protected void onPostSave(T entity) {
}

protected void initializeAssociationsForResponse(Object entity, int depth) {
if (entity == null || depth < 0) {
if (!shouldProcessEntity(entity, depth)) {
return;
}

Expand All @@ -456,33 +456,50 @@ protected void initializeAssociationsForResponse(Object entity, int depth) {
return;
}

processAssociations(entity, managedType, depth);
}

private boolean shouldProcessEntity(Object entity, int depth) {
return entity != null && depth >= 0;
}

private void processAssociations(Object entity, ManagedType<?> managedType, int depth) {
BeanWrapper wrapper = new BeanWrapperImpl(entity);
for (Attribute<?, ?> attribute : managedType.getAttributes()) {
if (!attribute.isAssociation() || !wrapper.isReadableProperty(attribute.getName())) {
if (!isProcessableAssociation(attribute, wrapper)) {
continue;
}

Object value = wrapper.getPropertyValue(attribute.getName());
if (value == null) {
continue;
if (value != null) {
Hibernate.initialize(value);
processAssociationValue(value, depth);
}
}
}

Hibernate.initialize(value);
if (depth == 0) {
continue;
}
private boolean isProcessableAssociation(Attribute<?, ?> attribute, BeanWrapper wrapper) {
return attribute.isAssociation() && wrapper.isReadableProperty(attribute.getName());
}

if (value instanceof Collection<?> collection) {
for (Object item : collection) {
initializeSingularAssociations(item, depth - 1);
}
continue;
}
private void processAssociationValue(Object value, int depth) {
if (depth == 0) {
return;
}

if (value instanceof Collection<?> collection) {
processCollectionAssociations(collection, depth);
} else {
initializeSingularAssociations(value, depth - 1);
}
}

private void processCollectionAssociations(Collection<?> collection, int depth) {
for (Object item : collection) {
initializeSingularAssociations(item, depth - 1);
}
}

private void initializeSingularAssociations(Object entity, int depth) {
if (entity == null || depth < 0) {
return;
Expand All @@ -496,18 +513,17 @@ private void initializeSingularAssociations(Object entity, int depth) {

BeanWrapper wrapper = new BeanWrapperImpl(entity);
for (Attribute<?, ?> attribute : managedType.getAttributes()) {
if (!attribute.isAssociation() || attribute.isCollection() || !wrapper.isReadableProperty(attribute.getName())) {
if (!attribute.isAssociation() || attribute.isCollection()
|| !wrapper.isReadableProperty(attribute.getName())) {
continue;
}

Object value = wrapper.getPropertyValue(attribute.getName());
if (value == null) {
continue;
}

Hibernate.initialize(value);
if (depth > 0) {
initializeSingularAssociations(value, depth - 1);
if (value != null) {
Hibernate.initialize(value);
if (depth > 0) {
initializeSingularAssociations(value, depth - 1);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;
import java.util.Optional;

import org.hibernate.Hibernate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -146,6 +147,28 @@ private SmsLog processAndSaveSms(User user, SmsConnector connector, String phone
}
}

return logRepository.save(log);
SmsLog savedLog = logRepository.save(log);
initializeForResponse(savedLog);
return savedLog;
}
}

private void initializeForResponse(SmsLog smsLog) {
Hibernate.initialize(smsLog);

if (smsLog.getCreatedBy() != null) {
Hibernate.initialize(smsLog.getCreatedBy());
}

if (smsLog.getUpdatedBy() != null) {
Hibernate.initialize(smsLog.getUpdatedBy());
}

if (smsLog.getConnector() != null) {
Hibernate.initialize(smsLog.getConnector());
}

if (smsLog.getTemplate() != null) {
Hibernate.initialize(smsLog.getTemplate());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.flexcodelabs.flextuma.core.entities.finance.Wallet;
import com.flexcodelabs.flextuma.core.entities.sms.SmsConnector;
import com.flexcodelabs.flextuma.core.entities.sms.SmsLog;
import com.flexcodelabs.flextuma.core.enums.SmsCampaignStatus;
import com.flexcodelabs.flextuma.core.enums.SmsLogStatus;
import com.flexcodelabs.flextuma.core.helpers.CurrentUserResolver;
import com.flexcodelabs.flextuma.core.repositories.SmsCampaignRepository;
Expand Down