Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package benedict.zhang.addon.persistence; import benedict.zhang.addon.common.UserPreferenceConstants; import benedict.zhang.addon.preferences.UserPreference; import benedict.zhang.addon.roleplaying.model.AddonDescription; import benedict.zhang.addon.roleplaying.model.RolePlayingConfiguration; import benedict.zhang.addon.roleplaying.model.SpellConfiguration; import benedict.zhang.addon.soundmanager.model.Sound; import java.util.ArrayList; import java.util.List; import org.h2.util.StringUtils; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; /** * * @author bzhang097 */ public class PersistenceManager { private static PersistenceManager manager; private SessionFactory sessionFactory; private Session session; private static final String updateAddonDescriptionBeanId = "addonDescription"; private PersistenceManager() { this.sessionFactory = HibernateUtil.getSessionFactory(); } public static PersistenceManager getInstance() { if (manager == null) { manager = new PersistenceManager(); } return manager; } public void persistRolePlaying(RolePlayingConfiguration config) { session = sessionFactory.openSession(); session.beginTransaction(); for (SpellConfiguration spellConfig : config.getSpells()) { spellConfig.setRoleplayingConfig(config); } session.saveOrUpdate(config); session.getTransaction().commit(); session.close(); } public List loadRolePlaying() { session = sessionFactory.openSession(); session.beginTransaction(); List resultList = session.createQuery("From RolePlayingConfiguration").list(); session.getTransaction().commit(); session.close(); return resultList; } public List loadRolePlaying(String name) { if (StringUtils.isNullOrEmpty(name)) { return loadRolePlaying(); } session = sessionFactory.openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(RolePlayingConfiguration.class); criteria.add(Restrictions.like("addonName", "%" + name + "%")); List resultList = criteria.list(); session.getTransaction().commit(); session.close(); return resultList; } public void persistSoundConfig(Sound sound) { session = sessionFactory.openSession(); session.beginTransaction(); session.saveOrUpdate(sound); session.getTransaction().commit(); session.close(); } public Sound loadSound(String soundName, String actor) { session = sessionFactory.openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(Sound.class); criteria.add(Restrictions.eq("soundName", soundName)); criteria.add(Restrictions.and(Restrictions.eq("actor", actor))); Sound sound = (Sound) criteria.uniqueResult(); session.getTransaction().commit(); session.close(); String path = UserPreference.getUserPreference().getSetting(UserPreferenceConstants.SOUND_LIBRARY) + sound.getActor() + System.getProperty("file.separator") + sound.getSoundName() + ".mp3"; sound.setSoundPath(path); return sound; } public List querySounds(String soundName, String actor) { session = sessionFactory.openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(Sound.class); if (soundName != null || !"".equals(soundName)) { criteria.add(Restrictions.like("soundName", "%" + soundName + "%")); } if (actor != null || !"".equals(actor)) { criteria.add(Restrictions.like("actor", "%" + actor + "%")); } List sounds = criteria.list(); session.getTransaction().commit(); session.close(); return sounds; } public void removeSound(Sound sound) { session = sessionFactory.openSession(); session.beginTransaction(); session.delete(sound); session.getTransaction().commit(); session.close(); } public AddonDescription loadAddonDescription() { session = sessionFactory.openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(AddonDescription.class); criteria.addOrder(Order.desc("version")); criteria.setMaxResults(1); List addonDescriptions = criteria.list(); AddonDescription addonDescription = null; if (addonDescriptions.isEmpty()) { addonDescription = new AddonDescription(); session.save(addonDescription); } else { addonDescription = (AddonDescription) addonDescriptions.get(0); } session.getTransaction().commit(); session.close(); return addonDescription; } public void addonDescriptionUpdater(String path) { session = sessionFactory.openSession(); session.beginTransaction(); ApplicationContext context = new FileSystemXmlApplicationContext(path); AddonDescription addonDescription = context.getBean(updateAddonDescriptionBeanId, AddonDescription.class); session.save(addonDescription); session.getTransaction().commit(); session.close(); } }