com.cloudbees.demo.beesshop.domain.ProductRepository.java Source code

Java tutorial

Introduction

Here is the source code for com.cloudbees.demo.beesshop.domain.ProductRepository.java

Source

/*
 * Copyright 2010-2013, CloudBees Inc.
 *
 * 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.cloudbees.demo.beesshop.domain;

import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.mongodb.*;
import com.mongodb.gridfs.GridFS;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Repository;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
 */
@Repository
public class ProductRepository {
    final Logger logger = LoggerFactory.getLogger(getClass());
    private DB db;
    private DBCollection collection;

    @Autowired
    public ProductRepository(@Nonnull DB db) throws UnknownHostException {
        this.db = Preconditions.checkNotNull(db, "'db' can not be null");
        this.collection = db.getCollection("catalog");
    }

    @Nonnull
    public Collection<Product> find(@Nullable String name) {
        DBCursor dbObjects;
        if (name == null || name.isEmpty()) {
            dbObjects = collection.find();
        } else {
            BasicDBObject query = new BasicDBObject("name", name);
            dbObjects = collection.find(query);
        }

        logger.trace("find({}): {}", name, dbObjects);
        return Lists.newArrayList(Iterables.transform(dbObjects, new Product.FromDBObject()));
    }

    @Nullable
    public Product get(String id) {
        BasicDBObject query = new BasicDBObject();
        query.put("_id", new ObjectId(id));

        DBObject dbObject = collection.findOne(query);
        return new Product.FromDBObject().apply(dbObject);
    }

    public List<String> suggestProductKeywords(String query) {
        return Collections.singletonList("#TODO#");
    }

    public List<String> suggestProductWords(String query) {
        return Collections.singletonList("#TODO#");
    }

    public void update(@Nonnull Product product) {
        collection.save(new Product.ToDBObjectFunction().apply(product), WriteConcern.SAFE);
    }

    public void insert(@Nonnull Product product) {
        DBObject dbObject = new Product.ToDBObjectFunction().apply(product);
        collection.insert(WriteConcern.SAFE, dbObject);
        product.setId((ObjectId) dbObject.get("_id"));
    }
}