/*
* Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gtugs.repository;
import org.gtugs.domain.Chapter;
import org.gtugs.domain.Country;
import org.gtugs.domain.Stats;
import org.gtugs.domain.Topic;
import org.springframework.orm.jdo.support.JdoDaoSupport;
import org.springframework.stereotype.Repository;
import java.util.List;
import javax.jdo.PersistenceManager;
/**
* @author jasonacooper@google.com (Jason Cooper)
*/
@Repository
public class JdoStatsDao extends JdoDaoSupport {
public static enum Field {
NUM_CHAPTERS, NUM_COUNTRIES, NUM_EVENTS, CHAPTERS_SORTED_BY_NUM_EVENTS,
COUNTRIES_SORTED_BY_NUM_EVENTS, TOPICS_SORTED_BY_NUM_EVENTS
}
private static final String KEY_NAME = "stats_key";
public JdoStatsDao() {
super();
setPersistenceManagerFactory(PMF.get());
}
public Stats getStats() {
PersistenceManager pm = getPersistenceManager();
try {
return pm.getObjectById(Stats.class, KEY_NAME);
} catch (javax.jdo.JDOObjectNotFoundException e) {
return new Stats(KEY_NAME);
} finally {
pm.close();
}
}
public void updateField(Field field, Object value) {
Stats stats = getStats();
if (field.equals(Field.NUM_CHAPTERS)) {
stats.setNumChapters((Integer) value);
} else if (field.equals(Field.NUM_COUNTRIES)) {
stats.setNumCountries((Integer) value);
} else if (field.equals(Field.NUM_EVENTS)) {
stats.setNumEvents((Integer) value);
} else if (field.equals(Field.CHAPTERS_SORTED_BY_NUM_EVENTS)) {
stats.setChaptersSortedByNumEvents((List<Chapter>) value);
} else if (field.equals(Field.COUNTRIES_SORTED_BY_NUM_EVENTS)) {
stats.setCountriesSortedByNumEvents((List<Country>) value);
} else if (field.equals(Field.TOPICS_SORTED_BY_NUM_EVENTS)) {
stats.setTopicsSortedByNumEvents((List<Topic>) value);
}
getJdoTemplate().makePersistent(stats);
}
}