argos/notification-service/notification/src/main/java/gr/cite/notification/model/builder/UserNotificationPreferenceB...

58 lines
3.1 KiB
Java

package gr.cite.notification.model.builder;
import com.fasterxml.jackson.databind.ObjectMapper;
import gr.cite.notification.authorization.AuthorizationFlags;
import gr.cite.notification.convention.ConventionService;
import gr.cite.notification.data.UserNotificationPreferenceEntity;
import gr.cite.notification.model.tenantconfiguration.TenantConfiguration;
import gr.cite.notification.model.UserNotificationPreference;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.DataLogEntry;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
import java.util.*;
@Component
@RequestScope
public class UserNotificationPreferenceBuilder extends BaseBuilder<UserNotificationPreference, UserNotificationPreferenceEntity> {
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
private final ObjectMapper mapper;
@Autowired
public UserNotificationPreferenceBuilder(ConventionService conventionService) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(UserNotificationPreferenceBuilder.class)));
this.mapper = new ObjectMapper();
}
public UserNotificationPreferenceBuilder authorize(EnumSet<AuthorizationFlags> values){
this.authorize = values;
return this;
}
@Override
public List<UserNotificationPreference> build(FieldSet fields, List<UserNotificationPreferenceEntity> data) throws MyApplicationException {
this.logger.debug("building for {} items requesting {} fields", Optional.ofNullable(data).map(List::size).orElse(0),Optional.ofNullable(fields).map(FieldSet::getFields).map(Set::size) .orElse(0));
this.logger.trace(new DataLogEntry("requested fields",fields));
if(fields == null || data == null || fields.isEmpty()) return new ArrayList<>();
List<UserNotificationPreference> models = new ArrayList<>();
for(UserNotificationPreferenceEntity d : data){
UserNotificationPreference m = new UserNotificationPreference();
if(fields.hasField(this.asIndexer(UserNotificationPreference._id))) m.setId(d.getId());
if(fields.hasField(this.asIndexer(UserNotificationPreference._userId))) m.setUserId(d.getUserId());
if(fields.hasField(this.asIndexer(UserNotificationPreference._tenantId))) m.setTenantId(d.getTenantId());
if(fields.hasField(this.asIndexer(UserNotificationPreference._type))) m.setType(d.getType());
if(fields.hasField(this.asIndexer(UserNotificationPreference._channel))) m.setChannel(d.getChannel());
if(fields.hasField(this.asIndexer(UserNotificationPreference._ordinal))) m.setOrdinal(d.getOrdinal());
if(fields.hasField(this.asIndexer(TenantConfiguration._createdAt))) m.setCreatedAt(d.getCreatedAt());
models.add(m);
}
this.logger.debug("build {} items",Optional.of(models).map(List::size).orElse(0));
return models;
}
}