Java tutorial
/** * PureInfo Quake * @(#)ZjuOutlayCardCodeGenerator.java 1.0 Oct 30, 2005 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.outlay.model.impl; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.lang.StringUtils; import com.pureinfo.ark.content.ArkContentHelper; import com.pureinfo.force.exception.PureException; import com.pureinfo.srm.org.model.Organization; import com.pureinfo.srm.outlay.domain.IIDTableMgr; import com.pureinfo.srm.outlay.model.IDTable; import com.pureinfo.srm.outlay.model.IOutlayCardCodeGenerator; import com.pureinfo.srm.project.model.Project; /** * <P> * Created on Oct 30, 2005 8:24:25 PM <BR> * Last modified on Oct 30, 2005 * </P> * * @author Freeman.Hu * @version 1.0, Oct 30, 2005 * @since Quake 1.0 */ public class ZjuOutlayCardCodeGenerator implements IOutlayCardCodeGenerator { private static DateFormat dateFormatter = new SimpleDateFormat("yy"); private static NumberFormat numFormatter = new DecimalFormat("00"); /** * @see com.pureinfo.srm.outlay.model.IOutlayCardCodeGenerator#generateCode(com.pureinfo.srm.project.model.Project, * String) */ public String generateCode(Project _prj, String _sOutlayType) throws PureException { StringBuffer sbuff = new StringBuffer(); IIDTableMgr mgr = (IIDTableMgr) ArkContentHelper.getContentMgrOf(IDTable.class); String sIdName = getCodePrefix(_prj, _sOutlayType); try { sbuff.append(sIdName); String sFlowid = numFormatter.format(mgr.getIdValue(sIdName)); sbuff.append(sFlowid); return sbuff.toString(); } finally { sbuff.setLength(0); } } public String getCodePrefix(Project _prj, String _sOutlayType) throws PureException { Organization org = _prj.getDepartment(); String sDeptCode; if (org == null) { sDeptCode = "XXXXX"; } else { sDeptCode = StringUtils.rightPad(org.getCode(), 5, '0'); } return getCodePrefix(_prj, _sOutlayType, sDeptCode); } public String getCodePrefix(Project _prj, String _sOutlayType, String sOrgCode) throws PureException { String[] fixes = getFixes(_sOutlayType); Organization college = _prj.getCollege(); Date date = _prj.getStartDate(); if (date == null) date = new Date(); String sDateString = dateFormatter.format(date); StringBuffer sbuff = new StringBuffer(); try { sbuff.append(fixes[0]); if (college != null) { sbuff.append(college.getCode()); } sbuff.append(sOrgCode); sbuff.append(fixes[1]); sbuff.append(sDateString); return sbuff.toString(); } finally { sbuff.setLength(0); } } /** * @param _nOutlayType * @return */ public String[] getFixes(String _sOutlayType) { String[] fixes = new String[2]; if (_sOutlayType.matches("[0-9a-zA-Z]+(-){1}[0-9a-zA-Z]+")) { fixes[0] = _sOutlayType.substring(0, 3); fixes[1] = _sOutlayType.substring(_sOutlayType.length() - 2); } else if (_sOutlayType.matches("[0-9a-zA-Z]+")) { fixes[0] = _sOutlayType; fixes[1] = ""; } else { fixes[0] = ""; fixes[1] = ""; } return fixes; } }