Search in sources :

Example 1 with MdsVersionedEntity

use of org.motechproject.mds.domain.MdsVersionedEntity in project motech by motech.

the class EntityServiceImpl method createEntity.

@Override
@Transactional
public EntityDto createEntity(EntityDto entityDto) {
    String packageName = ClassName.getPackage(entityDto.getClassName());
    boolean fromUI = StringUtils.isEmpty(packageName);
    String username = getUsername();
    if (fromUI) {
        String className;
        if (entityDto.getName().contains(" ")) {
            entityDto.setName(entityDto.getName().trim());
            StringBuilder stringBuilder = new StringBuilder();
            String[] nameParts = entityDto.getName().split(" ");
            for (String part : nameParts) {
                if (part.length() > 0) {
                    stringBuilder.append(Character.toUpperCase(part.charAt(0)));
                    if (part.length() > 1) {
                        stringBuilder.append(part.substring(1));
                    }
                }
            }
            className = String.format("%s.%s", Constants.PackagesGenerated.ENTITY, stringBuilder.toString());
        } else {
            // in this situation entity.getName() returns a simple name of class
            className = String.format("%s.%s", Constants.PackagesGenerated.ENTITY, entityDto.getName());
        }
        entityDto.setClassName(className);
    }
    if (allEntities.contains(entityDto.getClassName())) {
        throw new EntityAlreadyExistException(entityDto.getName());
    }
    Entity entity = allEntities.create(entityDto);
    LOGGER.debug("Adding default fields to the entity, since it doesn't extend MdsEntity or MdsVersionedEntity");
    if (!MdsEntity.class.getName().equalsIgnoreCase(entityDto.getSuperClass()) && !MdsVersionedEntity.class.getName().equalsIgnoreCase(entityDto.getSuperClass())) {
        EntityHelper.addDefaultFields(entity, allTypes);
    }
    if (username != null) {
        allEntityAudits.createAudit(entity, username);
    }
    return entity.toDto();
}
Also used : EntityAlreadyExistException(org.motechproject.mds.exception.entity.EntityAlreadyExistException) MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) MdsEntity(org.motechproject.mds.domain.MdsEntity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Entity (org.motechproject.mds.domain.Entity)1 MdsEntity (org.motechproject.mds.domain.MdsEntity)1 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)1 EntityAlreadyExistException (org.motechproject.mds.exception.entity.EntityAlreadyExistException)1 Transactional (org.springframework.transaction.annotation.Transactional)1