Java tutorial
/** * PureInfo Quake * @(#)ShowReserchCenterSelectorFunctionHandler.java 1.0 2005-9-25 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.view.function; import org.apache.commons.lang.ArrayUtils; import com.pureinfo.ark.content.ArkContentHelper; import com.pureinfo.dolphin.model.IObjects; import com.pureinfo.dolphin.script.function.handler.FunctionHandlerUtil; import com.pureinfo.dolphinview.context.model.IDVContext; import com.pureinfo.dolphinview.parser.function.FunctionHandlerDVImplBase; import com.pureinfo.force.exception.PureException; import com.pureinfo.srm.auth.domain.IUserOrgMapMgr; import com.pureinfo.srm.auth.model.SRMUser; import com.pureinfo.srm.auth.model.UserOrgMap; import com.pureinfo.srm.org.OrganizationConstants; import com.pureinfo.srm.org.domain.IInstituteMgr; import com.pureinfo.srm.org.domain.IResearchCenterMgr; import com.pureinfo.srm.org.model.Institute; import com.pureinfo.srm.org.model.ResearchCenter; /** * <P> * Created on 2005-9-25 14:51:04 <BR> * Last modified on 2005-9-25 * </P> * dispaly <OPTION>part to select reserch centers * * @author elmar.chen * @version 1.0, 2005-9-25 * @since Quake 1.0 */ public class ShowReserchCenterSelectorFunctionHandler extends FunctionHandlerDVImplBase { /** * @see com.pureinfo.dolphinview.parser.function.FunctionHandlerDVImplBase#perform(java.lang.Object[], * com.pureinfo.dolphinview.context.model.IDVContext) */ public Object perform(Object[] _args, IDVContext _context) throws PureException { int nType = OrganizationConstants.ORG_TYPE_RESERCH_CENTER; if (_args.length > 0) { nType = FunctionHandlerUtil.getIntArg(_args, 0, nType); } int _sUserId = getUserId(_context); Integer[] nValues = findOrgIds(_sUserId, nType); Object[][] orgDatas = getOrgDatas(nType); return makeOptions(orgDatas, nValues); } private Object[][] getOrgDatas(int _nType) throws PureException { return _nType == OrganizationConstants.ORG_TYPE_LAB ? getLabs() : getRCs(); } private Object[][] getLabs() throws PureException { IObjects inses = null; try { IInstituteMgr mgr = (IInstituteMgr) ArkContentHelper.getContentMgrOf(Institute.class); inses = mgr.findAllLabs(); Object[][] datas = new Object[3][inses.getSize()]; Institute ins = null; for (int i = 0; i < datas[0].length; i++) { ins = (Institute) inses.next(); datas[0][i] = new Integer(ins.getId()); datas[1][i] = ins.getName(); datas[2][i] = ins.getCode(); } return datas; } catch (PureException ex) { throw new PureException(PureException.DATABASE_ACCESS, "failed to find all labs.", ex); } finally { if (inses != null) inses.clear(); } } private String makeOptions(Object[][] _sOrgDatas, Integer[] _sValues) { StringBuffer sbuff = new StringBuffer(); for (int i = 0; i < _sOrgDatas[0].length; i++) { sbuff.append("<OPTION value=\"").append(_sOrgDatas[0][i]).append("\""); if (ArrayUtils.contains(_sValues, _sOrgDatas[0][i])) { sbuff.append(" SELECTED"); } sbuff.append(">").append(_sOrgDatas[1][i]).append("\n"); } return sbuff.toString(); } private Object[][] getRCs() throws PureException { IObjects rcs = null; try { IResearchCenterMgr mgr = (IResearchCenterMgr) ArkContentHelper.getContentMgrOf(ResearchCenter.class); rcs = mgr.findAll(null); Object[][] datas = new Object[3][rcs.getSize()]; ResearchCenter rc = null; for (int i = 0; i < datas[0].length; i++) { rc = (ResearchCenter) rcs.next(); datas[0][i] = new Integer(rc.getId()); datas[1][i] = rc.getName(); datas[2][i] = rc.getCode(); } return datas; } catch (PureException ex) { throw new PureException(PureException.DATABASE_ACCESS, "failed to find all research center.", ex); } finally { if (rcs != null) rcs.clear(); } } private int getUserId(IDVContext _context) throws PureException { Object obj = _context.getObject(); int _sUserId = -1; if (obj != null && (obj instanceof SRMUser)) { _sUserId = ((SRMUser) obj).getId(); } return _sUserId; } private Integer[] findOrgIds(int _sUserId, int _nType) throws PureException { if (_sUserId < 0) return null; Integer[] nValues = null; IUserOrgMapMgr uommgr = (IUserOrgMapMgr) ArkContentHelper.getContentMgrOf(UserOrgMap.class); IObjects uoms = null; try { uoms = uommgr.findAllByUserId(_sUserId, _nType); if (uoms != null) { nValues = new Integer[uoms.getSize()]; UserOrgMap uom = null; int i = 0; while ((uom = (UserOrgMap) uoms.next()) != null) { nValues[i++] = new Integer(uom.getOrgId()); } } } finally { if (uoms != null) { uoms.clear(); } } return nValues; } }