Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 153 and 154: Chapter 11 ■ Integrating Your Spr
- Page 155 and 156: Chapter 11 ■ Integrating Your Spr
- Page 157 and 158: Chapter 11 ■ Integrating Your Spr
- Page 159 and 160: Chapter 11 ■ Integrating Your Spr
- Page 161 and 162: Chapter 11 ■ Integrating Your Spr
- Page 163 and 164: Chapter 11 ■ Integrating Your Spr
- Page 165 and 166: Chapter 11 ■ Integrating Your Spr
- Page 167 and 168: Chapter 12 Exposing a REST API Nowa
- Page 169 and 170: Chapter 12 ■ Exposing a REST API
- Page 171 and 172: Chapter 12 ■ Exposing a REST API
- Page 173 and 174: Chapter 12 ■ Exposing a REST API
- Page 175 and 176: Chapter 12 ■ Exposing a REST API
- Page 177 and 178: Chapter 12 ■ Exposing a REST API
- Page 179 and 180: HTTP-DELETE Chapter 12 ■ Exposing
- Page 181 and 182: Chapter 13 Adding E-mail and Schedu
- Page 183 and 184: Chapter 13 ■ Adding E-mail and Sc
- Page 185 and 186: Chapter 13 ■ Adding E-mail and Sc
- Page 187 and 188: Chapter 13 ■ Adding E-mail and Sc
- Page 189 and 190: Chapter 13 ■ Adding E-mail and Sc
- Page 191 and 192: Part III Spring Framework Advanced
- Page 193 and 194: Chapter 14 ■ Using Dynamic Langua
- Page 195 and 196: Chapter 14 ■ Using Dynamic Langua
- Page 197 and 198: Chapter 14 ■ Using Dynamic Langua
- Page 199 and 200: Chapter 14 ■ Using Dynamic Langua
- Page 201 and 202: Chapter 15 ■ Spring Data Within Y
- Page 203: Chapter 15 ■ Spring Data Within Y
- Page 207 and 208: Chapter 15 ■ Spring Data Within Y
- Page 209 and 210: Chapter 15 ■ Spring Data Within Y
- Page 211 and 212: Chapter 15 ■ Spring Data Within Y
- Page 213 and 214: Chapter 15 ■ Spring Data Within Y
- Page 215 and 216: Chapter 16 ■ Messaging with Your
- Page 217 and 218: Chapter 16 ■ Messaging with Your
- Page 219 and 220: Chapter 16 ■ Messaging with Your
- Page 221 and 222: Chapter 16 ■ Messaging with Your
- Page 223 and 224: Chapter 16 ■ Messaging with Your
- Page 225 and 226: Chapter 16 ■ Messaging with Your
- Page 227 and 228: Chapter 17 Be Social and Go Mobile
- Page 229 and 230: Chapter 17 ■ Be Social and Go Mob
- Page 231 and 232: Chapter 17 ■ Be Social and Go Mob
- Page 233 and 234: Chapter 17 ■ Be Social and Go Mob
- Page 235 and 236: Chapter 17 ■ Be Social and Go Mob
- Page 237 and 238: Chapter 17 ■ Be Social and Go Mob
- Page 239 and 240: Chapter 17 ■ Be Social and Go Mob
- Page 241 and 242: Chapter 17 ■ Be Social and Go Mob
- Page 243 and 244: Chapter 17 ■ Be Social and Go Mob
- Page 245 and 246: Chapter 17 ■ Be Social and Go Mob
- Page 247 and 248: Part IV Spring I/O Spring I/O is no
- Page 249 and 250: Chapter 18 ■ Spring and Groovy
- Page 251 and 252: Chapter 18 ■ Spring and Groovy Li
- Page 253 and 254: Chapter 18 ■ Spring and Groovy cl
Implementing the DocumentDAO<br />
Chapter 15 ■ <strong>Spring</strong> Data Within Your <strong>Spring</strong> Application<br />
Now let’s implement a DocumentDAO class. This class will have the same behavior as its counterpart (the JDBC<br />
implementation). Here you need to implement all the DocumentDAO interface methods (see Listing 15-3).<br />
Listing 15-3. MongoDocumentRepository.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.Date;<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.data.mongodb.core.query.Update;<br />
import org.springframework.stereotype.Repository;<br />
import com.apress.isf.java.model.Document;<br />
import com.apress.isf.spring.data.DocumentDAO;<br />
@Repository("mongoDocumentDAO")<br />
public class MongoDocumentRespository implements DocumentDAO{<br />
@Autowired<br />
private MongoOperations mongoTemplate;<br />
public List getAll() {<br />
return mongoTemplate.findAll(Document.class);<br />
}<br />
public Document save(String id, Document document) {<br />
Document _documentUpdate = findById(id);<br />
if(null==_documentUpdate){<br />
mongoTemplate.insert(document);<br />
}else{<br />
Query query = query(where("documentId").is(id));<br />
Update update = new Update();<br />
update.set("name",null == document.getName() ?<br />
_documentUpdate.getName():document.getName());<br />
update.set("location",null == document.getLocation() ?<br />
_documentUpdate.getLocation():document.getLocation());<br />
update.set("description",null == document.getDescription() ?<br />
_documentUpdate.getDescription() : document.getDescription());<br />
update.set("type",null == document.getType() ?<br />
_documentUpdate.getType() : document.getType());<br />
update.set("modified", new Date());<br />
207