25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5 ■ Working with Collections and Custom Types<br />

public Document[] getAll() {<br />

if(log.isDebugEnabled())<br />

log.debug("Start Params: ");<br />

Document[] result = documents.toArray(new Document[documents.size()]);<br />

}<br />

if(log.isDebugEnabled())<br />

log.debug("End Result:" + result);<br />

return result;<br />

}<br />

You need to remember that in your previous DocumentDAO implementation you had several Document objects<br />

defined in your implementation. In this new DocumentRepository class (see Listing 5-1) you have removed those<br />

documents: doc1, doc2, and so on; you have also added a collection, in this case a List of type Document. Note that<br />

you are adding more logs to your class so you know what is going on. Let’s review Listing 5-2 and continue with the<br />

other classes.<br />

Listing 5-2. TypeDataDAO.java<br />

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

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

public interface TypeDataDAO {<br />

public Type[] getAll();<br />

public Type findById(String id);<br />

}<br />

In the TypeDataDAO (see Listing 5-2) interface, you have just added a new method, findById. This method will<br />

help you just to get the Type based on your id. With this new change, let’s modify the classes and implement this<br />

method. Now let’s take a look at Listing 5-3, your TypeDataDAO implementation.<br />

Listing 5-3. TypeDataRepository.java<br />

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

import java.util.Map;<br />

import org.slf4j.Logger;<br />

import org.slf4j.LoggerFactory;<br />

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

public class TypeDataRepository implements TypeDataDAO{<br />

private static final Logger log = LoggerFactory.getLogger(TypeDataRepository.class);<br />

private Map types = null;<br />

public Map getTypes() {<br />

return types;<br />

}<br />

54

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

Saved successfully!

Ooh no, something went wrong!