Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Implementing the DocumentDAO Chapter 15 ■ Spring Data Within Your Spring Application Now let’s implement a DocumentDAO class. This class will have the same behavior as its counterpart (the JDBC implementation). Here you need to implement all the DocumentDAO interface methods (see Listing 15-3). Listing 15-3. MongoDocumentRepository.java package com.apress.isf.spring.mongo; import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Query.query; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Repository; import com.apress.isf.java.model.Document; import com.apress.isf.spring.data.DocumentDAO; @Repository("mongoDocumentDAO") public class MongoDocumentRespository implements DocumentDAO{ @Autowired private MongoOperations mongoTemplate; public List getAll() { return mongoTemplate.findAll(Document.class); } public Document save(String id, Document document) { Document _documentUpdate = findById(id); if(null==_documentUpdate){ mongoTemplate.insert(document); }else{ Query query = query(where("documentId").is(id)); Update update = new Update(); update.set("name",null == document.getName() ? _documentUpdate.getName():document.getName()); update.set("location",null == document.getLocation() ? _documentUpdate.getLocation():document.getLocation()); update.set("description",null == document.getDescription() ? _documentUpdate.getDescription() : document.getDescription()); update.set("type",null == document.getType() ? _documentUpdate.getType() : document.getType()); update.set("modified", new Date()); 207

Chapter 15 ■ Spring Data Within Your Spring Application } mongoTemplate.updateFirst(query, update, Document.class); document = findById(id); } return document; public Document findById(String id) { Query query = query(where("documentId").is(id)); return mongoTemplate.findOne(query, Document.class); } public Document removeById(String id) { Document document = findById(id); if(document!=null) mongoTemplate.remove(document); return document; } public List findByTypeName(String name) { Query query = query(where("documentId.type.name").is(name)); return mongoTemplate.find(query, Document.class); } } Listing 15-3 shows the DocumentDAO implementation. Now instead of using a jdbcTemplate (like you did in Chapter 9) you are using a mongoTemplate that is an instance of the MongoOperations class. Next is the implementation of the TypeDAO interface, as shown in Listing 15-4. Listing 15-4. MongoTypeRepository.java package com.apress.isf.spring.mongo; import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Query.query; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Repository; import com.apress.isf.java.model.Type; import com.apress.isf.spring.data.TypeDAO; @Repository("mongoTypeDAO") public class MongoTypeRepository implements TypeDAO { @Autowired private MongoOperations mongoTemplate; 208

Chapter 15 ■ <strong>Spring</strong> Data Within Your <strong>Spring</strong> Application<br />

}<br />

mongoTemplate.updateFirst(query, update, Document.class);<br />

document = findById(id);<br />

}<br />

return document;<br />

public Document findById(String id) {<br />

Query query = query(where("documentId").is(id));<br />

return mongoTemplate.findOne(query, Document.class);<br />

}<br />

public Document removeById(String id) {<br />

Document document = findById(id);<br />

if(document!=null)<br />

mongoTemplate.remove(document);<br />

return document;<br />

}<br />

public List findByTypeName(String name) {<br />

Query query = query(where("documentId.type.name").is(name));<br />

return mongoTemplate.find(query, Document.class);<br />

}<br />

}<br />

Listing 15-3 shows the DocumentDAO implementation. Now instead of using a jdbcTemplate (like you<br />

did in Chapter 9) you are using a mongoTemplate that is an instance of the MongoOperations class. Next is the<br />

implementation of the TypeDAO interface, as shown in Listing 15-4.<br />

Listing 15-4. MongoTypeRepository.java<br />

package com.apress.isf.spring.mongo;<br />

import static org.springframework.data.mongodb.core.query.Criteria.where;<br />

import static org.springframework.data.mongodb.core.query.Query.query;<br />

import java.util.List;<br />

import org.springframework.beans.factory.annotation.Autowired;<br />

import org.springframework.data.mongodb.core.MongoOperations;<br />

import org.springframework.data.mongodb.core.query.Query;<br />

import org.springframework.stereotype.Repository;<br />

import com.apress.isf.java.model.Type;<br />

import com.apress.isf.spring.data.TypeDAO;<br />

@Repository("mongoTypeDAO")<br />

public class MongoTypeRepository implements TypeDAO {<br />

@Autowired<br />

private MongoOperations mongoTemplate;<br />

208

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!