Java tutorial
/* * Copyright (C) 2014 Zhou_Rui * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.rui.common_delete; import com.candy.db.HibernateBase; import com.candy.middle.GlobalConfig; import java.util.HashMap; import java.util.List; import java.util.Map; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author zho55500 */ public class XbrlMappingProc extends HibernateBase { private HashMap<String, String> xbrlNameMapping = new HashMap(); private static class Holder { static final XbrlMappingProc INSTANCE = new XbrlMappingProc(); } public static XbrlMappingProc getInstance() { return Holder.INSTANCE; } private XbrlMappingProc() { } public void reset() { writeAllToDB(); xbrlNameMapping.clear(); List<Xbrlmapping> results = loadAllRecordsFromDB(); if (results != null) { for (Xbrlmapping xm : results) { xbrlNameMapping.put(xm.getXname(), xm.getDescription()); } } } /** * query xbrl name * @param xname * @return description */ public String queryByXbrlName(String xname) { return xbrlNameMapping.get(xname); } public boolean saveXbrlName(String xname, String display) { if (xname.length() > GlobalConfig.MAX_XBRLNAME_LENGTH) xname = xname.substring(0, GlobalConfig.MAX_XBRLNAME_LENGTH); String oriValue = xbrlNameMapping.get(xname); if (oriValue != null && !oriValue.equalsIgnoreCase(display)) { //System.out.println("ERROR- try to insert a different xbrl description with same xbrl name"); return false; } else { xbrlNameMapping.put(xname, display); return true; } } /** * write everything back to DB * @return */ public boolean writeAllToDB() { if (xbrlNameMapping.size() == 0) return true; try { Session session = sf.openSession(); Transaction tx = session.beginTransaction(); int count = 0; for (Map.Entry<String, String> pair : xbrlNameMapping.entrySet()) { Xbrlmapping xm = new Xbrlmapping(); xm.setXname(pair.getKey()); xm.setDescription(pair.getValue()); session.save(xm); if (++count % 20 == 0) { session.flush(); session.clear(); } } tx.commit(); session.close(); return true; } catch (HibernateException e) { // e.printStackTrace(); return false; } } /** * load records from db * @return */ private List<Xbrlmapping> loadAllRecordsFromDB() { try { begin(); Query q = getSession().createQuery("from xbrlmapping"); List<Xbrlmapping> results = q.list(); commit(); if (results.isEmpty()) return null; else { return results; } } catch (HibernateException e) { return null; } } public void readAllRecords() { Session session = getSession(); } }