org.ednovo.gooru.domain.service.annotation.AnnotationServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.ednovo.gooru.domain.service.annotation.AnnotationServiceImpl.java

Source

/////////////////////////////////////////////////////////////
// AnnotationServiceImpl.java
// gooru-api
// Created by Gooru on 2014
// Copyright (c) 2014 Gooru. All rights reserved.
// http://www.goorulearning.org/
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////
package org.ednovo.gooru.domain.service.annotation;

import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.ednovo.gooru.application.util.UserContentRelationshipUtil;
import org.ednovo.gooru.core.api.model.Annotation;
import org.ednovo.gooru.core.api.model.AnnotationType;
import org.ednovo.gooru.core.api.model.ContentType;
import org.ednovo.gooru.core.api.model.Sharing;
import org.ednovo.gooru.core.api.model.UserContentAssoc.RELATIONSHIP;
import org.ednovo.gooru.core.constant.ParameterProperties;
import org.ednovo.gooru.infrastructure.persistence.hibernate.BaseRepository;
import org.ednovo.gooru.infrastructure.persistence.hibernate.content.ContentRepository;
import org.ednovo.gooru.infrastructure.persistence.hibernate.resource.ResourceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.Errors;

@Service
public class AnnotationServiceImpl implements AnnotationService, ParameterProperties {
    @Autowired
    private ContentRepository contentRepository;

    @Autowired
    private BaseRepository baseRepository;

    @Autowired
    private ResourceRepository resourceRepository;

    @Override
    public void create(Annotation annotation, String type, Errors errors) {
        if (annotation.getUser() == null) {
            errors.reject(USER, INVALID_USER);
        }
        if (annotation.getResource() == null || annotation.getResource().getGooruOid() == null) {
            errors.reject(RESOURCE, INVALID_RESOURCE);
        }
        if (errors.hasErrors()) {
            return;
        }

        AnnotationType annotationType = (AnnotationType) this.getBaseRepository().get(AnnotationType.class, type);
        ContentType contentTypeAnnotation = (ContentType) this.getBaseRepository().get(ContentType.class,
                ContentType.ANNOTATION);

        annotation.setGooruOid(UUID.randomUUID().toString());
        annotation.setAnnotationType(annotationType);
        annotation.setContentType(contentTypeAnnotation);
        annotation.setSharing(StringUtils.defaultIfEmpty(annotation.getSharing(), Sharing.PRIVATE.getSharing()));
        annotation.setCreatedOn(new Date(System.currentTimeMillis()));
        annotation.setLastModified(new Date(System.currentTimeMillis()));

        this.getContentRepository().save(annotation);

        UserContentRelationshipUtil.updateUserContentRelationship(annotation.getResource(), annotation.getUser(),
                (type.equals(AnnotationType.Type.SUBSCRIPTION.getType())) ? RELATIONSHIP.SUBSCRIBE
                        : RELATIONSHIP.QUOTE);

    }

    public ContentRepository getContentRepository() {
        return contentRepository;
    }

    public void setContentRepository(ContentRepository contentRepository) {
        this.contentRepository = contentRepository;
    }

    public BaseRepository getBaseRepository() {
        return baseRepository;
    }

    public void setBaseRepository(BaseRepository baseRepository) {
        this.baseRepository = baseRepository;
    }

    public ResourceRepository getResourceRepository() {
        return resourceRepository;
    }

    public void setResourceRepository(ResourceRepository resourceRepository) {
        this.resourceRepository = resourceRepository;
    }

}