/**
 * The MongoDB implementation of the StorageService.
 *
 * This class does not provide a MongoTemplate.update method as all the 
 * relevant data required for record creation is available when using the 
 * StorageService.
 * A remove-insert implementation is used instead. The itemId is assumed to be 
 * globally unique. This is important as MongoDB may otherwise apply changes to
 * multiple records.
 *
 * TODO include support for secure mode (connection with authentication).
 */
package de.ddb.pdc.storage;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

@Component
public class MongoStorageServiceImpl implements StorageService {

  private final MongoTemplate mongoTemplate;
  private final String collectionName;

  /**
   * @Autowired ensures Spring injects the required Bean. @Value reads the
   * related key-value from the properties file.
   * @param mongoTemplate
   * @param collectionName
   */
  @Autowired
  public MongoStorageServiceImpl(MongoTemplate mongoTemplate,
          @Value("${collection.name:pdcData}") String collectionName) {
    this.mongoTemplate = mongoTemplate;
    this.collectionName = collectionName;
  }

  /**
   * Stores a single MongoDataModel record in the collection.
   *
   * @param record
   */
  @Override
  public void store(StorageModel record) {
    mongoTemplate.insert(record, collectionName);
  }
    
  /**
   * Updates a record by removing the existing record and calling the
   * {@link #store(StorageModel) store()} method to store the new record. 
   * The record that is removed is the first record that matches the itemId.
   * 
   * TODO implement inspection of the WriteResult to ensure the old record was
   * indeed removed.
   * 
   * @param newRecord
   */
  @Override
  public void update(StorageModel newRecord) {
    Query query = new Query();
    query.addCriteria(Criteria.where("itemId").is(newRecord.getItemId()));
    mongoTemplate.remove(query, collectionName);
    this.store(newRecord);
  }

  /**
   * Fetches the first StorageModel record from the collection that matches
   * the query. Implements the Query class so SQL-like constructs can be used.
   *
   * @param itemId
   * @return record
   */
  @Override
  public StorageModel fetch(String itemId) {
    Query query = new Query();
    query.addCriteria(Criteria.where("itemId").is(itemId));
    return mongoTemplate.findOne(query, StorageModel.class, collectionName);
  }

  /**
   * Fetch all MongoDataModel records from the collection.
   *
   * @return list of MongoDataModel records
   */
  @Override
  public List<StorageModel> fetchAll() {
    return mongoTemplate.findAll(StorageModel.class, collectionName);
  }

  /**
   * Delete all records by dropping the collection.
   */
  @Override
  public void deleteAll() {
    mongoTemplate.dropCollection(collectionName);
  }
}

@@ -1,1230 +1,949 @@
-/**%0D%0A * The MongoDB implementation of the StorageService.%0D%0A *%0D%0A * This class does not provide a MongoTemplate.update method as all the %0D%0A * relevant data required for record creation is available when using the %0D%0A * StorageService.%0D%0A * A remove-insert implementation is used instead. The itemId is assumed to be %0D%0A * globally unique. This is important as MongoDB may otherwise apply changes to%0D%0A * multiple records.%0D%0A *%0D%0A * TODO include support for secure mode (connection with authentication).%0D%0A */%0D%0Apackage de.ddb.pdc.storage;%0D%0A%0D%0Aimport java.util.List;%0D%0Aimport org.springframework.beans.factory.annotation.Autowired;%0D%0Aimport org.springframework.beans.factory.annotation.Value;%0D%0Aimport org.springframework.data.mongodb.core.MongoTemplate;%0D%0Aimport org.springframework.data.mongodb.core.query.Criteria;%0D%0Aimport org.springframework.data.mongodb.core.query.Query;%0D%0Aimport org.springframework.stereotype.Component;%0D%0A%0D%0A@Component%0D%0Apublic class MongoStorageServiceImpl implements StorageService %7B%0D%0A%0D%0A  private final MongoTemplate mongoTemplate;%0D%0A  private final String collectionName;%0D%0A%0D%0A  /**%0D%0A   * @Autowired ensures Spring injects the required Bean. @Value reads the%0D%0A   * related key-value from the properties file.%0D%0A   * @param
+package de.ddb.pdc.storage;%0D%0A%0D%0Aimport java.util.List;%0D%0Aimport org.springframework.beans.factory.annotation.Autowired;%0D%0Aimport org.springframework.beans.factory.annotation.Value;%0D%0Aimport org.springframework.data.mongodb.core.MongoTemplate;%0D%0Aimport org.springframework.data.mongodb.core.query.Criteria;%0D%0Aimport org.springframework.data.mongodb.core.query.Query;%0D%0Aimport org.springframework.stereotype.Component;%0D%0A%0D%0A/**%0D%0A * The MongoDB implementation of the StorageService.%0D%0A *%0D%0A * This class does not provide a MongoTemplate.update method as all the%0D%0A * relevant data required for record creation is available when using the%0D%0A * StorageService.%0D%0A * A remove-insert implementation is used instead. The itemId is assumed to be%0D%0A * globally unique. This is important as MongoDB may otherwise apply changes to%0D%0A * multiple records.%0D%0A *%0D%0A */%0D%0A@Component%0D%0Apublic class MongoStorageServiceImpl implements StorageService %7B%0D%0A%0D%0A  private final MongoTemplate
  mon
@@ -952,29 +952,41 @@
 Template
+;
 %0D%0A  
- * @param
+private final String
  collect
@@ -992,23 +992,19 @@
 tionName
+;
 %0D%0A
-   */
 %0D%0A  @Aut
@@ -1454,20 +1454,16 @@
 ;%0D%0A  %7D%0D%0A
-    
 %0D%0A  /**%0D
@@ -1607,17 +1607,16 @@
  record.
- 
 %0D%0A   * T
@@ -1695,118 +1695,8 @@
    *
- %0D%0A   * TODO implement inspection of the WriteResult to ensure the old record was%0D%0A   * indeed removed.%0D%0A   * 
 %0D%0A  

make changes based on Denis Review