PageRenderTime 204ms CodeModel.GetById 80ms app.highlight 15ms RepoModel.GetById 106ms app.codeStats 0ms

/src/main/java/com/google/ie/business/dao/impl/IdeaDaoImpl.java

http://thoughtsite.googlecode.com/
Java | 188 lines | 122 code | 19 blank | 47 comment | 17 complexity | f4c81037f99e16017270532d4d14a3f6 MD5 | raw file
  1/* Copyright 2010 Google Inc.
  2 * 
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 * 
  7 *      http://www.apache.org/licenses/LICENSE-2.0
  8 * 
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS.
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License
 14 */
 15
 16package com.google.ie.business.dao.impl;
 17
 18import com.google.ie.business.dao.IdeaDao;
 19import com.google.ie.business.domain.Idea;
 20import com.google.ie.business.domain.User;
 21import com.google.ie.dto.RetrievalInfo;
 22
 23import org.springframework.transaction.annotation.Propagation;
 24import org.springframework.transaction.annotation.Transactional;
 25
 26import java.util.ArrayList;
 27import java.util.Collection;
 28import java.util.HashMap;
 29import java.util.List;
 30import java.util.Map;
 31import java.util.Set;
 32
 33import javax.jdo.Query;
 34
 35/**
 36 * A JDO implementation object for IdeaDao.
 37 * 
 38 * @author Sachneet
 39 */
 40
 41public class IdeaDaoImpl extends BaseDaoImpl implements IdeaDao {
 42
 43    @Override
 44    public Idea getIdea(Idea idea) {
 45        return getJdoTemplate().getObjectById(Idea.class, idea.getKey());
 46    }
 47
 48    @Override
 49    public List<Idea> getIdeas(RetrievalInfo retrievalInfo, Set<String> statusOfIdeas) {
 50        /* Execute query and return published ideas */
 51        return executeGetIdeas(null, retrievalInfo, statusOfIdeas);
 52    }
 53
 54    @Override
 55    public List<Idea> getUserIdeas(User user, Set<String> statusOfIdeas, RetrievalInfo retrievalInfo) {
 56        /* Execute query and return result */
 57        return executeGetIdeas(user, retrievalInfo,
 58                        statusOfIdeas);
 59    }
 60
 61    @SuppressWarnings("unchecked")
 62    public List<Idea> getIdeasByTagKey(String tagKey, Set<String> statusOfIdeas,
 63                    RetrievalInfo retrievalInfo) {
 64        Query query = getJdoTemplate().getPersistenceManagerFactory()
 65                        .getPersistenceManager().newQuery(Idea.class);
 66        /*
 67         * Add the start index to the number of records required since
 68         * internally the second argument is treated as the index up to which
 69         * the entities are to be fetched
 70         */
 71        query.setRange(retrievalInfo.getStartIndex(), retrievalInfo.getStartIndex()
 72                        + retrievalInfo.getNoOfRecords());
 73        query.setOrdering("" + retrievalInfo.getOrderBy() + " " + retrievalInfo.getOrderType());
 74        query.setFilter("tagKeys == :tagKey && status == :statusOfIdeas");
 75        Map<String, Object> mapOfValues = new HashMap<String, Object>();
 76        mapOfValues.put("statusOfIdeas", statusOfIdeas);
 77        mapOfValues.put("tagKey", tagKey);
 78        Collection<Idea> collection = getJdoTemplate()
 79                        .find(query.toString(), mapOfValues);
 80        if (collection != null) {
 81            return new ArrayList<Idea>(collection);
 82        }
 83        return null;
 84    }
 85
 86    /**
 87     * Method to retrieve the list of ideas for different cases.
 88     * 
 89     * @param user The user object.
 90     * @param retrievalInfo the {@link RetrievalInfo} object having information
 91     *        of startIndex and total number of records.
 92     * @param statusOfProject {@link Set} of strings holding the Idea
 93     *        status.
 94     * @return List of idea objects
 95     */
 96    @SuppressWarnings("unchecked")
 97    private List<Idea> executeGetIdeas(User user, RetrievalInfo retrievalInfo,
 98                    Set<String> statusOfIdeas) {
 99        Query query = null;
100        Collection<Idea> collection;
101        try {
102            query = getJdoTemplate().getPersistenceManagerFactory()
103                            .getPersistenceManager().newQuery(Idea.class);
104            /*
105             * Add the start index to the number of records required since
106             * internally the second argument is treated as the index up to
107             * which the entities are to be fetched
108             */
109            query.setRange(retrievalInfo.getStartIndex(), retrievalInfo.getStartIndex()
110                            + retrievalInfo.getNoOfRecords());
111            query.setOrdering("" + retrievalInfo.getOrderBy() + " " + retrievalInfo.getOrderType());
112            Map<String, Object> mapOfFilterValues = new HashMap<String, Object>();
113            if (user != null && user.getUserKey() != null) {
114                query.setFilter("status == :statusOfIdeas && creatorKey == :creatorKeyParam");
115                mapOfFilterValues.put("creatorKeyParam", user.getUserKey());
116            } else {
117                query.setFilter("status == :statusOfIdeas");
118            }
119            mapOfFilterValues.put("statusOfIdeas", statusOfIdeas);
120            collection = getJdoTemplate()
121                            .find(query.toString(), mapOfFilterValues);
122            return new ArrayList<Idea>(collection);
123        } finally {
124            if (query != null) {
125                query.closeAll();
126            }
127        }
128
129    }
130
131    @Override
132    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
133    public Idea saveIdea(Idea idea) {
134        idea = getJdoTemplate().makePersistent(idea);
135        return idea;
136    }
137
138    @Override
139    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
140    public boolean updateStatus(Idea idea) {
141        String status = idea.getStatus();
142        idea = getJdoTemplate().getObjectById(Idea.class, idea.getKey());
143        idea.setStatus(status);
144        idea = getJdoTemplate().makePersistent(idea);
145        if (idea != null)
146            return true;
147        return false;
148    }
149
150    /**
151     * Update of the idea vote points of the given idea.
152     * 
153     * @param idea Idea object containing updated status and key.
154     * @return boolean return true or false on the basis of successful update.
155     */
156    @Override
157    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
158    public boolean updateIdeaPoints(final Idea idea) {
159        Idea originalIdea = getJdoTemplate().getObjectById(Idea.class, idea.getKey());
160        boolean flag = false;
161        if (originalIdea != null) {
162            originalIdea.setTotalVotes(idea.getTotalVotes());
163            originalIdea.setTotalPositiveVotes(idea.getTotalPositiveVotes());
164            originalIdea.setTotalNegativeVotes(idea.getTotalNegativeVotes());
165            if (getJdoTemplate().makePersistent(originalIdea) != null) {
166                flag = true;
167            }
168        }
169        return flag;
170    }
171
172    @SuppressWarnings("unchecked")
173    @Override
174    public List<Idea> getIdeasByCategoryKey(String ideaCategoryKey, String statusOfIdeas,
175                    RetrievalInfo retrievalInfo) {
176        Query query = getJdoTemplate().getPersistenceManagerFactory()
177                        .getPersistenceManager().newQuery(Idea.class);
178        query.setFilter("ideaCategoryKey == '" + ideaCategoryKey + "' && status == '"
179                        + statusOfIdeas + "'");
180
181        List<Idea> collection = (List<Idea>) query.execute();
182        if (collection != null) {
183            return new ArrayList<Idea>(collection);
184        }
185        return null;
186    }
187}
188