Sample Java interface

This section describes IdValidateInterface, IdGenerateCustom, and ID generator.

Interface - IdValidate


package com.unicacorp.uap.common.template;
import java.util.HashMap;
/**
This is an interface to be implemented by the end user of a Unica Plan 
system for the purpose of validating system generated id values 
as per business logic. 
Implementations of this Interface are called by the Unica Plan Server.
*/
public interface IdValidate
{
/**
Returns true if the specified attribute values are valid.
*
* @param id - current project or program id. 	This will be the
							value if it is new project/program
* @param values - This is a set of name/value pairs, referring to
									a current database connection, the appropriate
									template id and another HashMap that contains
									name/value pairs, corresponding to the fields and
									values on the screen.
* @return true						 - if it is valid; otherwise returns false or throws
								exception.
* @throws					 com.unicacorp.uap.user.IdValidateException
*									Should contain a message value that is meaningful
									about what went wrong.
*/

public boolean isValid(int id, HashMap values) throws
		IdValidateException;
/**
		The name of the hashkey in the HashMap passed to IdValidate.isValid(..)
		that refers to a current database connection to the Unica Plan 
		system tables.
		This connection is available for use to implementations of this
		interface.
*/
		public final String PLAN_DB_CONNECTION	= "dbconnection";
		/**
		 * The name of the hashkey in the HashMap passed to
			idValidate.isValid(..) that refers to the id of the related
			template.
		 */
	public final String OBJECT_TEMPLATE_ID	= "templateid";
		/**
		 * The name of the hashkey in the HashMap pass to
		 * IdValidate.isValid(..) that refers to another Hashmap which
		 * contains name/value pairs. The name corresponds to a field on
		 * the screen for project/program and the value corresponds to the
		 * user entered text or selection.
		 */
	public final String OBJECT_ATTRIB_VALUES	= "attributeValues";
	}
	

Interface - IdGenerate


package com.unicaorp.uap.common.template;
import java.util.HashMap;
/* This is an interface to be implemented by the end user 
* of a Unica Plan
* system for the purpose of generating unique Project Code (PIDs). The intent
* is to allow users to attach to existing enterprise systems to help make
* project IDs meaningful in their enterprise.
*
* Implementations of this Interface are called by the Unica Plan Server.
* It is the responsibility of the Unica Plan Server 
* to assure that there is
* only one ID being generated at a time. When implementation of this
* interface are called, they can assume that there are no other IDs
* that are being generated concurrently.
*/

public interface IdGenerate {
    /**
		* Returns a string code used to define a Project object with Unica Plan
		*
		* @param uniqueId - This is an integer value that is generated by 
		*		the Unica Plan system. This is guaranteed to be unique across
		*		the system; hence, if the project ID returned is the string
		*		representation of this integer, it will be a unique
		*		Project Code (PID).
		*
		* @param values - This is a set of name/value pairs, referring to the current
		*		database connection, appropriate template id, code prefix, 
		*		request flag, and another HashMap that contains name/value
		*		pairs, corresponding to the fields and values on the screen.
		*
		* @param uniqueChecker - An implementation used to verify the uniqueness of 
		*		of ID's generated by this instance. 
		*
		* @return			 - A string that represents the ID of the project we are 
										creating.
		*
		* @throws com.unicacorp.uap.user.IdGenerateException
		*		Should contain a message value that is meaningful about
		*		what went wrong 
*/

    public String generateID (int uniqueId, HashMap values, IdUniqueChecker
		uniqueChecker)
		throws IdGenerateException;
	/**
	 * The name of the hashkey in the HashMap passed to IdValidate.isValid(..)
	 * that refers to a current database connection to the Unica Plan 
	 * system tables.
	 * This connection is available for use to implementations of this interface.
	 */
	public final String PLAN_DB_CONNECTION	= "dbconnection";
	/**
	 * The name of the hashkey in the HashMap passed to IdValidate.isValid(..)
	 * that refers to the id of the related template.
	 */

	public final String OBJECT_TEMPLATE_ID	= "templateid";
	/**
	 * The name of the hashkey in the HashMap passed to IdValidate.isValid(..)
	 * that refers to the desired string prefix to prepend the generated id.
	 */
	public final String OBJECT_CODE_PREFIX	= "pidprefix";
	/**
	 * The name of the hashkey in the HashMap passed to IdValidate.isValid(..)
	 * that refers that indicates whether the calling object is a request.
	 */

	public final String OBJECT_REQUEST_FLAG	= "flagprojectrequest";
	/**
	 * The name of the hashkey in the HashMap pass to IdValidate.isValid(..)
	 * that refers to another Hashmap which contains name/value pairs. The name
	 * corresponds to a field on the screen for project/program and the value
	 * corresponds to the user entered text or selection.
	 */
	public final String OBJECT_ATTRIB_VALUES	= "attributeValues";
	/**
	 * Default start plan code start number
	 */

	public final int PLAN_CODE_SUFFIX_START = 1000;
	/**
	 * Default start program code start number
	 */
	public final int PROGRAM_CODE_SUFFIX_START = 1000;
	/**
	 * Default start project code start number
	 */
	public final int PROJECT_CODE_SUFFIX_START = 1000;
	/**
	 * Default start rfq code start number
	 */
	public final int RFQ_CODE_SUFFIX_START = 1000;}

Custom ID generator


package com.unica.uap.component.helper;
import com.unicacorp.uap.common.db.*;
import com.unicacorp.uap.common.template.*;
import org.apache.commons.lang.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Properties;

/**
* The Class CustomComponentPidGenerateImpl.
*/
public class CustomComponentPidGenerateImpl implements IdGenerate,
	IdUniqueChecker {
	/** The lower limit. */
	public static int LOWER_LIMIT = 0;
	/** The upper limit. */
	public static int UPPER_LIMIT = 0;
	static {
		Properties attrPro = new Properties();
		try {
			String planHome = System.getProperty("plan.home");
			System.out.println("planHome : " + planHome);
			File file = new File(planHome + "/unwar/WEB-INF/IDRange.properties");
			FileInputStream fi = new FileInputStream(file);
			if (fi != null) {
				attrPro.load(fi);
				String min = (String) attrPro.get("mktOBJId.min");
				String max = (String) attrPro.get("mktOBJId.max");
				LOWER_LIMIT = Integer.parseInt(min);
				UPPER_LIMIT = Integer.parseInt(max);
				System.out.println("Lower Limit :" + LOWER_LIMIT);
				System.out.println("Upper Limit :" + UPPER_LIMIT);
			} else {
				System.out.println("IDRange Property file can not be found");
				throw new RuntimeException("IDRange Property file can not be found");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("IDRange Property file can not be found");
		}
	}
    

	/**
	 * The Constructor.
	 */
	public CustomComponentPidGenerateImpl() {
	}
	/**
	 * Generate ID.
	 *
	 * @param uniqueChecker the unique checker
	 * @param values the values
	 * @param instanceId the instance id
	 *
	 * @return the string
	 *
	 * @throws IdGenerateException the id generate exception
	 */
	 	public synchronized String generateID(int instanceId, HashMap values,
		IdUniqueChecker uniqueChecker) throws IdGenerateException {
		print("inside 'generateID' method");
		print("instanceId : " + instanceId);
		print("####################\n" + values + "####################\n");
		String prefix = (String) values.get("pidprefix");
		print("prefix : " + prefix);
		String templateid = (String) values.get("templateid");
		print("templateid : " + templateid);
		Connection con = (Connection) values.get("dbconnection");
		//int nextValue = -1;
		boolean isEmptyPrefix = false;
		try {
			if (StringUtils.isEmpty(prefix)) {
				isEmptyPrefix = true;
			}
			    

			//GET THE CURRENT VALUE OF THE TEMPLATE ID - from CUST_GENIDS table
			String sqlString = "SELECT ID_VALUE FROM CUST_GENIDS WHERE ENTITY_NAME = ?";
			print("sqlString : " + sqlString);
			PreparedStatement ps = null;
			ResultSet rs = null;
			int cnt = 0;
			try {
				ps = new UAPSQLPreparedStatement(con, sqlString);
				UAPSQLUtils.setupPreparedStatement(ps, 1, templateid, "string");
				rs = ps.executeQuery();
				if (rs.next()) {
					cnt = rs.getInt(1);
				}
				print("current ID vlaue :" + cnt);
				UAPSQLUtils.closeResultSet(rs, ps);
			} catch (SQLException ex) {
				ex.printStackTrace();
				UAPSQLUtils.closeResultSet(rs, ps);
				throw new RuntimeException(ex);
			} catch (Exception exception) {
				exception.printStackTrace();
				UAPSQLUtils.closeResultSet(rs, ps);
				throw new RuntimeException(exception);
			}
			if (cnt == 0) {
				//insert first new record for the template id into table
				cnt = LOWER_LIMIT;
				String sqlInsertStr = "INSERT INTO CUST_GENIDS values (?,?)";
				print("sqlInsertStr : " + sqlInsertStr);
				ps = new UAPSQLPreparedStatement(con, sqlInsertStr);
				ps.setString(1, templateid);
				ps.setInt(2, cnt);
        } 
    
        
        else if ((cnt >= LOWER_LIMIT) && (cnt < UPPER_LIMIT)) {
				//increase the counter and update the row for the template id
				cnt++;
				String sqlUpdateStr =
					"UPDATE CUST_GENIDS SET ID_VALUE= ? WHERE ENTITY_NAME = ?";
				print("Update : " + sqlUpdateStr);
				ps = new UAPSQLPreparedStatement(con, sqlUpdateStr);
				ps.setInt(1, cnt);
				ps.setString(2, templateid);
			} else {
				print("Current ID is out of range, ID Range [" + LOWER_LIMIT +
					"-" + UPPER_LIMIT + "]");
					//throw exception that can not generate id, limit is over
					throw new IdGenerateException(
						"Current ID is out of range, ID Range [" + LOWER_LIMIT +
						"-" + UPPER_LIMIT + "]");
        }
			//UAPSQLUtils.beginTransaction(con);
			ps.execute();
			//UAPSQLUtils.endTransaction(con, true);
			String pid = (isEmptyPrefix ? "" : prefix) + cnt;
			print("return from 'generateID'  method with pid : " + pid);
			return pid;
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new IdGenerateException(ex);
		}
	}
	    

	/**
	 * Checks if is unique.
	 *
	 * @param values the values
	 * @param Id the Id
	 *
	 * @return true, if is unique
	 */
	public boolean isUnique(String Id, HashMap values) {
		print("inside 'isUnique' method");
		//provide actual implementation for uniqueness check
		return true;
		}
	/**
	 * Print.
	 *
	 * @param str the str
	 */
	private void print(String str) {
		System.out.println(str);
	}