0
\$\begingroup\$

ActiveObjects is a ORM similar to ActiveObject. This is a repository for it to be reused within my application.

I would like advise on:

  1. Naming of the CreatePeriodConfiguration and UpdatePeriodConfiguration, do you have better ideas? I don't like verbs in class/interface names.
  2. Do you think CreatePeriodConfiguration and UpdatePeriodConfiguration should be the same object, if so why?
  3. This repository is exposing Entity which gives others a way to save each entity. Do you think this is a bad thing? Should the PeriodConfigurationEntity be encapsulated?
  4. Naming of the repository does not match the PeriodConfigurationEntity but still PeriodConfigurationEntityRepository would be to long. Any suggestions for improvements?
  5. Should I be exposing a ActiveObject entity?

    @Component
    public class PeriodConfigurationRepositoryImpl implements 
        PeriodConfigurationRepository {
    
        private final ActiveObjects ao;
    
        @Autowired
        public PeriodConfigurationRepositoryImpl(ActiveObjects ao) {
            this.ao = ao;
        }
    
        @Override
        public PeriodConfigurationEntity create(CreatePeriodConfiguration periodConfiguration) {
            PeriodConfigurationEntity entity = ao.create(PeriodConfigurationEntity.class,
                    new DBParam(PeriodConfigurationEntity.PERIOD_CONFIG.START_DAY.name(), periodConfiguration.getStartDay()),
                    new DBParam(PeriodConfigurationEntity.PERIOD_CONFIG.START_MONTH.name(), periodConfiguration.getStartMonth()),
                    new DBParam(PeriodConfigurationEntity.PERIOD_CONFIG.DEFAULT_PERIOD.name(), periodConfiguration.isDefault())
            );
            return entity;
        }
    
        @Override
        public PeriodConfigurationEntity update(UpdatePeriodConfiguration periodConfiguration) {
            PeriodConfigurationEntity entity = get(periodConfiguration.getId());
            entity.setStartDay(periodConfiguration.getStartDay());
            entity.setStartMonth(periodConfiguration.getStartMonth());
            entity.save();
            return entity;
        }
    
        @Override
        public void delete(int id) {
            PeriodConfigurationEntity periodConfigurationEntity = get(id);
            for (PeriodEntity periodEntity : periodConfigurationEntity.getPeriods()) {
                ao.delete(periodEntity);
            }
            ao.delete(periodConfigurationEntity);
        }
    
        @Override
        public Collection<PeriodConfigurationEntity> getAll() {
            return getEntitiesByQuery(
                    Query.select(PeriodConfigurationEntity.PERIOD_CONFIG.valuesToString())
            );
        }
    
        @Override
        public PeriodConfigurationEntity get(int id) {
            PeriodConfigurationEntity entity = find(id);
            if(entity == null) {
                throw new IllegalArgumentException(String.format("PeriodConfiguration with id %s not found.", id));
            }
            return entity;
        }
    
        @Override
        public PeriodConfigurationEntity find(int id) {
            return ao.get(PeriodConfigurationEntity.class, id);
        }
    
        @Override
        public PeriodConfigurationEntity findDefault() {
            return findPeriodConfigurationByScope(true);
        }
    
        @Override
        public PeriodConfigurationEntity findSecond() {
            return findPeriodConfigurationByScope(false);
        }
    
        private PeriodConfigurationEntity findPeriodConfigurationByScope(boolean isDefault) {
            Collection<PeriodConfigurationEntity> periodConfigurationEntities = getEntitiesByQuery(
                    new QueryBuilder()
                            .add(new QueryBuilder.EqClause(PeriodConfigurationEntity.PERIOD_CONFIG.DEFAULT_PERIOD.name(), isDefault))
                            .build(PeriodConfigurationEntity.PERIOD_CONFIG.valuesToString())
            );
            if (periodConfigurationEntities.iterator().hasNext()) {
                return periodConfigurationEntities.iterator().next();
            }
            return null;
        }
    
        @Override
        public boolean doesExist(int id) {
            Query query = new QueryBuilder()
                    .add(new QueryBuilder.EqClause(PeriodConfigurationEntity.PERIOD_CONFIG.ID.name(), id))
                    .build(PeriodConfigurationEntity.PERIOD_CONFIG.ID.name());
            return ao.count(PeriodConfigurationEntity.class, query) > 0;
        }
    
        private Collection<PeriodConfigurationEntity> getEntitiesByQuery(Query query) {
            final Collection<PeriodConfigurationEntity> entities = Lists.newArrayList();
            ao.stream(PeriodConfigurationEntity.class, query, new EntityStreamCallback<PeriodConfigurationEntity, Integer>() {
                @Override
                public void onRowRead(PeriodConfigurationEntity periodConfigurationEntity) {
                    entities.add(periodConfigurationEntity);
                }
            });
            return entities;
        }
    }
    
    public class CreatePeriodConfiguration {
    
        private final int startDay;
        private final int startMonth;
        private final boolean isDefault;
    
        public CreatePeriodConfiguration(int startDay, int startMonth, boolean isDefault) {
            this.startDay = startDay;
            this.startMonth = startMonth;
            this.isDefault = isDefault;
        }
    
        public int getStartDay() {
            return startDay;
        }
    
        public int getStartMonth() {
            return startMonth;
        }
    
        public boolean isDefault() {
            return isDefault;
        }
    }
    
    public class UpdatePeriodConfiguration {
    
        private final int id;
        private final int startDay;
        private final int startMonth;
    
        public UpdatePeriodConfiguration(int id, int startDay, int startMonth) {
            this.id = id;
            this.startDay = startDay;
            this.startMonth = startMonth;
        }
    
        public int getId() {
            return id;
        }
    
        public int getStartDay() {
            return startDay;
        }
    
        public int getStartMonth() {
            return startMonth;
        }
    }
    
    @Table("PERIOD_CONFIG")
    public interface PeriodConfigurationEntity extends Entity {
    
        enum PERIOD_CONFIG {
            ID,
            START_DAY,
            START_MONTH,
            DEFAULT_PERIOD;
    
            public static String valuesToString() {
                return StringUtils.join(values(), ",");
            }
        }
    
        @NotNull
        int getStartDay();
        void setStartDay(int startDay);
    
        @NotNull
        int getStartMonth();
        void setStartMonth(int startMonth);
    
        @NotNull
        @Default("false")
        boolean isDefaultPeriod();
        void setDefaultPeriod(boolean defaultPeriod);
    }
    
\$\endgroup\$
1
\$\begingroup\$

First of all, it is generally recommended to use nouns for class names because usually a class holds multiple methods each describing some action (create, update, etc.). Your class names (CreatePeriodConfiguration and UpdatePeriodConfiguration) look more like method names because they describe a specific action. Furthermore, I do not see any functional differences between CreatePeriodConfiguration and UpdatePeriodConfiguration. So, I would be using only one class, not two.

As I understand, you use these two classes to encapsulate parameters passed to create and update a PeriodConfigurationEntity. For that reason, I would try to name such class a PeriodConfigurationParamatesrWrapper or shorter PeriodConfigParametersWrapper or PeriodConfigParametesrHolder.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.