All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clusterra.pmbok.document.domain.service.template.TemplateDomainServiceImpl Maven / Gradle / Ivy

Go to download

A application used as an example on how to set up pushing its components to the Central Repository.

There is a newer version: 1.1.3.RELEASE
Show newest version
/*
 * Copyright (c) 2014.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.clusterra.pmbok.document.domain.service.template;

import com.clusterra.iam.core.application.tenant.TenantId;
import com.clusterra.pmbok.document.domain.model.document.Version;
import com.clusterra.pmbok.document.domain.model.template.SectionTemplateAlreadyExistsException;
import com.clusterra.pmbok.document.domain.model.template.SectionTemplateNotFoundException;
import com.clusterra.pmbok.document.domain.model.template.Template;
import com.clusterra.pmbok.document.domain.model.template.TemplateId;
import com.clusterra.pmbok.document.domain.model.template.event.TemplateCreatedEvent;
import com.clusterra.pmbok.document.domain.model.template.event.TemplateDeletedEvent;
import com.clusterra.pmbok.document.domain.model.template.event.TemplateDeletingEvent;
import com.clusterra.pmbok.document.domain.model.template.repo.SectionTemplateRepository;
import com.clusterra.pmbok.document.domain.model.template.repo.TemplateRepository;
import com.clusterra.pmbok.document.domain.model.template.repo.TemplateSearchBySpecification;
import com.clusterra.pmbok.document.domain.model.template.repo.TemplateTenantSpecification;
import com.clusterra.pmbok.document.domain.model.template.section.SectionTemplate;
import com.clusterra.pmbok.document.domain.model.template.section.SectionTemplateId;
import com.clusterra.pmbok.document.domain.model.template.section.SectionType;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import com.clusterra.pmbok.document.domain.model.template.event.TemplateUpdatedEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author Denis Kuchugurov.
 *         11.07.2014.
 */
@Service
@Transactional(propagation = Propagation.MANDATORY)
public class TemplateDomainServiceImpl implements TemplateDomainService {

    @Autowired
    private TemplateRepository templateRepository;

    @Autowired
    private SectionTemplateRepository sectionTemplateRepository;

    @Autowired
    private ApplicationEventPublisher publisher;

    public Template createTemplate(TenantId tenantId, Integer majorVersion, Integer minorVersion, String name) throws TemplateAlreadyExistsException {
        Validate.notNull(tenantId);
        Validate.notNull(majorVersion);
        Validate.notNull(minorVersion);
        Validate.notNull(name);

        Validate.inclusiveBetween(Integer.valueOf(0), Integer.valueOf(100), majorVersion);
        Validate.inclusiveBetween(Integer.valueOf(0), Integer.valueOf(100), minorVersion);

        Version version = new Version(majorVersion, minorVersion);

        Template existing = templateRepository.findByTenantIdByName(tenantId.getId(), name);
        if (existing != null) {
            throw new TemplateAlreadyExistsException(tenantId, name);
        }

        Template template = templateRepository.save(new Template(tenantId, version, name));

        publisher.publishEvent(new TemplateCreatedEvent(this, template.getTemplateId()));
        return template;
    }

    public Template updateTemplateName(TemplateId templateId, String name) throws TemplateNotFoundException {
        Validate.notNull(name);
        Validate.notNull(templateId);
        Template template = findDocumentTemplate(templateId);

        template.setName(name);
        templateRepository.save(template);
        return template;
    }

    public void deleteTemplate(TemplateId templateId) throws TemplateNotFoundException {
        Validate.notNull(templateId);
        Template template = findDocumentTemplate(templateId);
        publisher.publishEvent(new TemplateDeletingEvent(this, template.getTemplateId()));
        template.removeAllSections(sectionTemplateRepository);
        templateRepository.delete(template);
        publisher.publishEvent(new TemplateDeletedEvent(this, template.getTemplateId()));
    }

    public Template findBy(TemplateId templateId) throws TemplateNotFoundException {
        return findDocumentTemplate(templateId);
    }

    public Template findByName(TenantId tenantId, String name) {
        Validate.notNull(tenantId);
        return templateRepository.findByTenantIdByName(tenantId.getId(), name);
    }

    public Page