no.nlf.dal.LicenseController.java Source code

Java tutorial

Introduction

Here is the source code for no.nlf.dal.LicenseController.java

Source

package no.nlf.dal;

import java.util.ArrayList;
import java.util.List;

import no.nlf.dal.config.AppContext;
import no.nlf.dal.config.MongoCounter;
import no.nlf.models.classes.License;
import no.nlf.models.mongoclasses.MongoLicense;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * 
 * @author: Tore Buer, s180346
 * @author: Eivind Jacobsen, s173466
 * @author: Morten Kristoffersen, s169440
 * 
 * @since may.26.2014
 *
 */
@Service
public class LicenseController {

    @Autowired
    private AppContext appContext;

    @Autowired
    private MongoCounter mongoCounter;

    public List<License> getAll() {
        List<MongoLicense> mongoLicenses = appContext.mongoOperation().findAll(MongoLicense.class);
        List<License> licenses = new ArrayList<>();

        if (mongoLicenses == null) {
            return licenses;
        }

        for (MongoLicense ml : mongoLicenses) {
            licenses.add(ml.toLicense());
        }

        return (ArrayList<License>) licenses;
    }

    public License getOne(int id) {
        Query searchQuery = new Query(Criteria.where("id").is(id));
        MongoLicense mongoLicense = appContext.mongoOperation().findOne(searchQuery, MongoLicense.class);

        if (mongoLicense == null) {
            return new License();
        }

        return mongoLicense.toLicense();
    }

    public License save(License license) {
        MongoLicense mongoLicense = new MongoLicense(license);
        mongoLicense.setId(mongoCounter.getNextSequence("licenses"));
        appContext.mongoOperation().save(mongoLicense);
        return mongoLicense.toLicense();
    }

    public License update(License license) {
        MongoLicense mongoLicense = new MongoLicense(license);
        Query searchQuery = new Query(Criteria.where("id").is(mongoLicense.getId()));

        Update updateLicense = new Update();

        updateLicense.set("melwinId", mongoLicense.getMelwinId());
        updateLicense.set("licenseName", mongoLicense.getLicenseName());
        updateLicense.set("active", mongoLicense.isActive());

        mongoLicense = appContext.mongoOperation().findAndModify(searchQuery, updateLicense,
                new FindAndModifyOptions().returnNew(true), MongoLicense.class);

        if (mongoLicense == null) {
            return new License();
        }

        return mongoLicense.toLicense();
    }

    public License delete(int id) {
        Query searchQuery = new Query(Criteria.where("id").is(id));
        MongoLicense mongoLicense = appContext.mongoOperation().findOne(searchQuery, MongoLicense.class);

        if (mongoLicense == null) {
            return new License();
        }

        appContext.mongoOperation().remove(mongoLicense);
        return mongoLicense.toLicense();
    }

}