Java tutorial
/* * Copyright(C) 2014 * NEC Corporation All rights reserved. * * No permission to use, copy, modify and distribute this software * and its documentation for any purpose is granted. * This software is provided under applicable license agreement only. */ package com.nec.harvest.service.impl; import java.util.Calendar; import java.util.Date; import org.apache.commons.lang.StringUtils; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.nec.crud.hibernate.HibernateSessionManager; import com.nec.harvest.constant.Constants; import com.nec.harvest.exception.ServiceException; import com.nec.harvest.model.SerialNumber; import com.nec.harvest.repository.SerialNumberRepository; import com.nec.harvest.service.SerialNumberService; import com.nec.harvest.util.StringUtil; /** * * {@link SerialNumberService} * * @author huonghv * */ public class SerialNumberServiceImpl implements SerialNumberService { private static final Logger logger = LoggerFactory.getLogger(SerialNumberServiceImpl.class); @SuppressWarnings("unused") private SerialNumberRepository repository; public SerialNumberServiceImpl(SerialNumberRepository serialNumberRepository) { this.repository = serialNumberRepository; } @Override public synchronized String generateUUIDByRecID(String recID) throws ServiceException { Session session = null; Transaction tx = null; // New an instance SerialNumber serialNumber = null; // Trying to find the actual serial number by RecID {} logger.info("Trying to find the actual serial number by RecID {}", recID); try { session = HibernateSessionManager.getSession(); tx = session.beginTransaction(); serialNumber = (SerialNumber) session.get(SerialNumber.class, recID); tx.commit(); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException("An error occurred while trying to get serial number by recId " + recID, ex); } finally { tx = null; HibernateSessionManager.closeSession(session); } // If could not find any matched record, then create new one // with value is 0000000000000001 if (serialNumber == null) { final String REC_NO = "0000000000000001"; serialNumber = new SerialNumber(recID, REC_NO); // Create a new one serial number for RecID {} logger.info("Create a new one serial number for RecID {}", recID); try { // Create a new one transaction session = HibernateSessionManager.getSession(); tx = session.beginTransaction(); session.save(serialNumber); tx.commit(); return REC_NO; } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException( "An error occurred while trying to get maximum serial number by recId " + recID, ex); } finally { tx = null; HibernateSessionManager.closeSession(session); } } // Must be fetched the new instance of serial number directly from underlying database logger.info("Must be fetched the new instance of serial number directly from underlying database"); try { session = HibernateSessionManager.getSession(); tx = session.beginTransaction(); if (session.contains(serialNumber)) { session.refresh(serialNumber); } tx.commit(); } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } logger.warn(ex.getMessage(), ex); } finally { tx = null; HibernateSessionManager.closeSession(session); } // Actual Seq Number long actualSeqNo = serialNumber.getCurrentSeqNo(); // auto increase the sequence number actualSeqNo += 1; // Substring to get the actual table name String tableName = null; int indexOfDot = recID.indexOf("."); if (StringUtils.isNotEmpty(recID) && indexOfDot > 1) { // e.g: AT011.RecID tableName = recID.substring(0, indexOfDot); } logger.info("Trying to find the maximum sequence number already existing in the table {}", tableName); // If the maximum sequence number already existing in the table // , so have to automatically increase the sequence number until // its value not exist in that table if (StringUtils.isNotEmpty(tableName)) { try { session = HibernateSessionManager.getSession(); tx = session.beginTransaction(); Query query = session.createSQLQuery("SELECT MAX(RecID) AS MAX_RecID FROM " + tableName); // Actual maximum Seq number String actualMaximumSeqNo = (String) query.uniqueResult(); if (StringUtils.isNotEmpty(actualMaximumSeqNo)) { long actualMaxSeqNo = Long.valueOf(actualMaximumSeqNo); if (actualSeqNo < actualMaxSeqNo) { logger.info("The maximum sequence number already existing in the {} is {}", tableName, actualMaximumSeqNo); // actualSeqNo = actualMaxSeqNo + 1; } } tx.commit(); } catch (HibernateException ex) { logger.warn(ex.getMessage()); if (tx != null) { tx.rollback(); } } finally { tx = null; HibernateSessionManager.closeSession(session); } } // Format the actual sequence number from {} to {} String maximumSeqNo = StringUtil.numberToStringWithUserFillUp(Constants.DEFAULT_SEQUENCE_CHARACTER_FILL, Constants.DEFAULT_SEQUENCE_LENGTH, actualSeqNo); logger.info("Format the actual sequence number from {} to {}", actualSeqNo, maximumSeqNo); try { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); serialNumber.setSeqNo(maximumSeqNo); serialNumber.setTimeU(calendar); session = HibernateSessionManager.getSession(); tx = session.beginTransaction(); session.update(serialNumber); tx.commit(); // The current serial number is {} logger.info("The current serial number is {}", maximumSeqNo); return maximumSeqNo; } catch (HibernateException ex) { if (tx != null) { tx.rollback(); } throw new ServiceException( "An error occurred while trying to get maximum serial number by recId " + recID, ex); } finally { tx = null; HibernateSessionManager.closeSession(session); } } }