PageRenderTime 262ms CodeModel.GetById 120ms app.highlight 6ms RepoModel.GetById 99ms app.codeStats 0ms

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

http://thoughtsite.googlecode.com/
Java | 92 lines | 45 code | 11 blank | 36 comment | 6 complexity | de4c0e46bcdb5fd56d68c82b861f779e 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.ShardedCounterDao;
19import com.google.ie.business.domain.ShardedCounter;
20
21import java.util.List;
22
23import javax.jdo.Query;
24
25/**
26 * A data access object specification base class for all data access objects
27 * that use JDO to interact with the datastore.
28 * 
29 * @author gmaurya
30 * 
31 */
32public class ShardedCounterDaoImpl extends BaseDaoImpl implements ShardedCounterDao {
33
34    /**
35     * Get the shards related to an entity.
36     * 
37     * @param parentKey the key of the parent entity
38     * 
39     * @return List of {@link ShardedCounter} objects
40     */
41    @SuppressWarnings("unchecked")
42    public List<ShardedCounter> getShardsByParentKey(String parentKey) {
43        Query query = null;
44        List<ShardedCounter> shards = null;
45        try {
46            query = getJdoTemplate().getPersistenceManagerFactory()
47                            .getPersistenceManager().newQuery(ShardedCounter.class);
48            query.setFilter("parentKey == :parentName");
49            shards = (List<ShardedCounter>) query.execute(parentKey);
50        } finally {
51            if (query != null)
52                query.closeAll();
53        }
54        return shards;
55    }
56
57    /**
58     * Get shards by parent key and shard number.
59     * 
60     * @param parentKey the key of the parent entity
61     * @param shardNum an int specifying the shard number
62     * 
63     * @return List of {@link ShardedCounter} objects
64     */
65    @SuppressWarnings("unchecked")
66    public List<ShardedCounter> getShardsByParentKeyAndShardNum(String parentKey, int shardNum) {
67        Query query = null;
68        List<ShardedCounter> shards = null;
69        try {
70            query = getJdoTemplate().getPersistenceManagerFactory()
71                            .getPersistenceManager().newQuery(ShardedCounter.class);
72            query.setFilter("parentKey == '" + parentKey + "' && " + "shardNumber == " + shardNum);
73            shards = (List<ShardedCounter>)
74                            query.execute();
75        } finally {
76            if (query != null)
77                query.closeAll();
78        }
79        return shards;
80    }
81
82    @Override
83    public ShardedCounter createOrUpdateShardedCounter(ShardedCounter shardedCounter) {
84        if (shardedCounter == null)
85            return null;
86        shardedCounter = getJdoTemplate().makePersistent(shardedCounter);
87
88        return shardedCounter;
89    }
90
91}
92