Java tutorial
/* * Copyright (C) 2018 * * 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/>. */ /*********************************************************************** * Module: RecallAction.java * Author: qianzheng_677 * Purpose: Defines the Class RecallAction ***********************************************************************/ package com.hyron.poscafe.service.action; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.hyron.poscafe.CashRegister; import com.hyron.poscafe.SysConfigure; import com.hyron.poscafe.dao.TransDao; import com.hyron.poscafe.dao.TransDaoImpl; import com.hyron.poscafe.hw.key.VirtualKey; import com.hyron.poscafe.model.Trans; import com.hyron.poscafe.model.TransId; import com.hyron.poscafe.service.BaseService; import com.hyron.poscafe.service.exception.OperationDeniedException; import com.hyron.poscafe.service.exception.ServiceException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.HibernateException; public class RecallAction extends BaseService implements Action { private static final Log log = LogFactory.getLog(LogInOutAction.class); private static final String regexp = "\\d+RECALL"; private SysConfigure sysConfigure = SysConfigure.getInstance(); private ResourceBundle bundle = ResourceBundle.getBundle(this.getClass().getPackage().getName() + ".Action", sysConfigure.getLocale());; private List<ActionObserver> observers = new ArrayList<ActionObserver>(); private Trans trans; private long recallTransNo = 0; private TransDao transDao = new TransDaoImpl(); @Override public void execute(List<VirtualKey> params) throws OperationDeniedException, ServiceException { try { super.beginTrans(); trans = transDao.findById(new TransId(sysConfigure.getShopNo(), sysConfigure.getPosNo(), recallTransNo), getSession()); if (trans != null) { super.commitTrans(); CashRegister.getInstance().setTrans(trans); } else { super.commitTrans(); recallTransNo = 0; //TODO i18n throw new ServiceException(""); } } catch (HibernateException e) { super.rollbackTrans(); } } @Override public boolean parseParams(List<VirtualKey> params) { StringBuffer tmpBuf = new StringBuffer(); for (VirtualKey vk : params) { tmpBuf.append(vk.getValue().isEmpty() ? vk.getKeyType().getType() : vk.getValue()); } Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(tmpBuf); if (!matcher.matches()) { return false; } int pos = tmpBuf.indexOf("RECALL"); String tmpStr = tmpBuf.substring(0, pos); recallTransNo = Long.valueOf(tmpStr); return true; } @Override public void undo() throws OperationDeniedException, UnsupportedOperationException { // TODO Auto-generated method stub } @Override public void saveNVRam() throws IOException { // TODO Auto-generated method stub } public Trans getTrans() { return trans; } public void setTrans(Trans trans) { this.trans = trans; } @Override public void registerObserver(ActionObserver observer) { if (!observers.contains(observer)) observers.add(observer); } @Override public void removeObserver(ActionObserver observer) { observers.remove(observer); } public void notifyObservers(Object arg) { for (ActionObserver observer : observers) { observer.update(this, arg); } } @Override public String getActionName() { return this.getClass().getName(); } }