PageRenderTime 45ms CodeModel.GetById 29ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://thoughtsite.googlecode.com/
Java | 152 lines | 101 code | 18 blank | 33 comment | 11 complexity | e4006b4327f8035ee9129f3839ea3335 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.ProjectDao;
 19import com.google.ie.business.domain.Project;
 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 for ProjectDao.
 37 * 
 38 * @author Charanjeet singh
 39 */
 40
 41public class ProjectDaoImpl extends BaseDaoImpl implements ProjectDao {
 42    private static final String IDEA_KEY = "ideaKey";
 43
 44    @Override
 45    public Project saveProject(Project project) {
 46        project = getJdoTemplate().makePersistent(project);
 47        return project;
 48    }
 49
 50    @Override
 51    public List<Project> getProjects(RetrievalInfo retrievalInfo, Set<String> statusOfProjects) {
 52        /* Execute query and return projects */
 53        return executeGetProject(null, retrievalInfo, statusOfProjects);
 54    }
 55
 56    @Override
 57    public List<Project> getProjects(User user, RetrievalInfo retrievalInfo,
 58                    Set<String> statusOfProject) {
 59        return executeGetProject(user, retrievalInfo, statusOfProject);
 60    }
 61
 62    @Override
 63    public Project getProject(String projectKey) {
 64        return findEntityByPrimaryKey(Project.class, projectKey);
 65    }
 66
 67    @SuppressWarnings("unchecked")
 68    @Override
 69    public List<Project> getProjectsByIdeaKey(String ideaKey) {
 70        Query query = getJdoTemplate().getPersistenceManagerFactory()
 71                        .getPersistenceManager().newQuery(Project.class);
 72        Map<String, Object> mapOfFilterValues = new HashMap<String, Object>();
 73        query.setFilter(IDEA_KEY + " == '" + ideaKey + "'");
 74        mapOfFilterValues.put("ideaKey", ideaKey);
 75        Collection<Project> projects = getJdoTemplate()
 76                        .find(query.toString(), mapOfFilterValues);
 77        if (projects == null) {
 78            return null;
 79        }
 80        return new ArrayList<Project>(projects);
 81    }
 82
 83    /**
 84     * Method to retrieve the list of Projects.".
 85     * 
 86     * @param user The user object.
 87     * @param retrievalInfo RetrievalInfo object having information of
 88     *        startIndex and total number of records.
 89     * @param statusOfProject A set of strings holding the project
 90     *        status.
 91     * 
 92     * @return List of Project objects saved or published by user.
 93     */
 94
 95    @SuppressWarnings("unchecked")
 96    private List<Project> executeGetProject(User user, RetrievalInfo retrievalInfo,
 97                    Set<String> statusOfProject) {
 98        Query query = null;
 99        try {
100            query = getJdoTemplate().getPersistenceManagerFactory()
101                            .getPersistenceManager().newQuery(Project.class);
102            query.setRange(retrievalInfo.getStartIndex(), retrievalInfo.getStartIndex()
103                            + retrievalInfo.getNoOfRecords());
104            query.setOrdering("" + retrievalInfo.getOrderBy() + " " + retrievalInfo.getOrderType());
105            Map<String, Object> mapOfFilterValues = new HashMap<String, Object>();
106            if (user != null && user.getUserKey() != null) {
107                query.setFilter("status == :statusOfProject && creatorKey == :creatorKeyParam");
108                mapOfFilterValues.put("creatorKeyParam", user.getUserKey());
109            } else {
110                query.setFilter("status == :statusOfProject");
111            }
112            mapOfFilterValues.put("statusOfProject", statusOfProject);
113            Collection<Project> collection = getJdoTemplate()
114                            .find(query.toString(), mapOfFilterValues);
115            /* Detach the result from the corresponding persistence manager */
116            collection = getJdoTemplate().detachCopyAll(collection);
117            return new ArrayList<Project>(collection);
118        } finally {
119            if (query != null) {
120                query.closeAll();
121            }
122        }
123    }
124
125    @SuppressWarnings("unchecked")
126    @Override
127    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
128    public List<Project> getProjectsByKeys(Set<String> projectKeys, RetrievalInfo retrievalInfo) {
129        Query query = null;
130        try {
131            query = getJdoTemplate().getPersistenceManagerFactory()
132                            .getPersistenceManager().newQuery(Project.class);
133            query.setFilter("key == :projectKeys");
134            query.setRange(retrievalInfo.getStartIndex(), retrievalInfo.getNoOfRecords()
135                            + retrievalInfo.getStartIndex());
136            Map<String, Object> mapOfFilterValues = new HashMap<String, Object>();
137            mapOfFilterValues.put("projectKeys", projectKeys);
138
139            Collection<Project> projects = getJdoTemplate()
140                            .find(query.toString(), mapOfFilterValues);
141            /* Detach the result from the corresponding persistence manager */
142            projects = getJdoTemplate().detachCopyAll(projects);
143            return new ArrayList<Project>(projects);
144        } finally {
145            if (query != null) {
146                query.closeAll();
147            }
148        }
149
150    }
151}
152