Etc Id Generator

From Documentation
< User:Jimmyshiau
Revision as of 09:08, 18 June 2013 by Samchuang (talk | contribs) (→‎IdAsUuidGenerator)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

zk.xml Setting

<system-config>
    <id-generator-class>foo.jmtest.SequenceIdGenerator</id-generator-class>
</system-config>

SequenceIdGenerator

For Jmeter stree test: Execute a Loading or Performance Test on ZK using JMeter

import javax.servlet.http.HttpServletRequest;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.sys.IdGenerator;
import org.zkoss.zk.ui.metainfo.ComponentInfo;

public class SequenceIdGenerator implements IdGenerator{
	public String nextComponentUuid(Desktop desktop, Component comp) {
		String number;
		if ((number = (String)desktop.getAttribute("Id_Num")) == null) {
			number = "0";
			desktop.setAttribute("Id_Num", number);
		}
		int i = Integer.parseInt(number);
		i++;// Start from 1
		desktop.setAttribute("Id_Num", String.valueOf(i));
		return "t_" + i;
	}
	
	public String nextComponentUuid(Desktop desktop, Component comp,
			ComponentInfo info) {
		return nextComponentUuid(desktop, comp);
	}
 
	public String nextDesktopId(Desktop desktop) {
		HttpServletRequest req = (HttpServletRequest)Executions.getCurrent().getNativeRequest();
		String dtid = req.getParameter("tdtid");
		if(dtid!=null){
			System.out.println(" use client dtid "+dtid + "\turl: " + req.getParameter("pageURL"));
			
		}
		return dtid==null?null:dtid;
	}

	public String nextPageUuid(Page page) {
		return null;
	}
}


SimpleIdGenerator

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.sys.IdGenerator;

public class SimpleIdGenerator implements IdGenerator {

	private static final String PREFIX = "zk_comp_";

	public String nextComponentUuid(Desktop desktop, Component comp) {
		int i = Integer.parseInt(desktop.getAttribute("Id_Num").toString());
		i++;// Start from 1
		desktop.setAttribute("Id_Num", String.valueOf(i));
		return PREFIX + i;
	}
	
	public String nextComponentUuid(Desktop desktop, Component comp,
			ComponentInfo info) {
		return nextComponentUuid(desktop, comp);
	}

	public String nextDesktopId(Desktop desktop) {
		if (desktop.getAttribute("Id_Num") == null) {
			String number = "0";
			desktop.setAttribute("Id_Num", number);
		}
		return null;
	}

	public String nextPageUuid(Page page) {
		return null;
	}
}

IdAsUuidGenerator

import java.util.Iterator;

import org.zkoss.lang.Strings;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.Property;
import org.zkoss.zk.ui.sys.IdGenerator;

public class IdAsUuidGenerator implements IdGenerator {

	public String nextComponentUuid(Desktop desktop, Component comp,
			ComponentInfo compInfo) {
		if (compInfo != null) {
			for (Iterator it = compInfo.getProperties().iterator(); it.hasNext();) {
				Property p = (Property) it.next();
				if ("id".equals(p.getName())) {
					return p.getRawValue();
				}
			}
		}
		return null;
	}

	public String nextDesktopId(Desktop desktop) {
		return null;
	}

	public String nextPageUuid(Page page) {
		return null;
	}

}

ComponentTypeIdgenerator

Jimmy idgenerator component.png

import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.zkoss.lang.Strings;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.ext.Scope;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.sys.IdGenerator;
import org.zkoss.zul.*;

public class ComponentTypeIdgenerator implements IdGenerator {

	private final static String ID_NUMBER = "zk_id_num";
	private final static String ID_GENERATOR_KEY = "id_generator_key_";
	private NumberFormat formatter = new DecimalFormat("0000");

	public String nextComponentUuid(Desktop desktop, Component comp) {
		
		Component p = comp.getParent();
		if (p != null && !(comp instanceof Grid) && !(comp instanceof Grid) 
				&& !(comp instanceof Grid) && !(comp instanceof Grid)) {
			String uuidPrefox = "";
			
			String pUuid = p.getUuid();
			if (pUuid.startsWith("grid")) {
				p = getCollectionComponent(Grid.class, comp);
				uuidPrefox = getCollectionComponentUuidPrefix(pUuid, "row");
			} else if (pUuid.startsWith("listbox")) {
				p = getCollectionComponent(Listbox.class, comp);
				uuidPrefox = getCollectionComponentUuidPrefix(pUuid, "listitem");
			} else if (pUuid.startsWith("tree")) {
				p = getCollectionComponent(Tree.class, comp);
				uuidPrefox = getCollectionComponentUuidPrefix(pUuid, "treeitem");
			} else if (pUuid.startsWith("combobox")) {
				p = getCollectionComponent(Combobox.class, comp);
				uuidPrefox = getCollectionComponentUuidPrefix(pUuid, "comboitem");
			}
			
			if (!Strings.isBlank(uuidPrefox))
				return uuidPrefox + getComponentName(comp) + "_" + 
					formatter.format(getComponentIdIndex(p, comp));
			
		}
		
		return getComponentName(comp) + "_" + formatter.format(getComponentIdIndex(desktop, comp));
	}
	

	private Component getCollectionComponent(Class cls, Component comp) {
		Component p = comp.getParent();
		while (!p.getClass().equals(cls)) {
			p = p.getParent();
		}
		return p;
	}


	private String getCollectionComponentUuidPrefix(String pUuid, String item) {
		int index = pUuid.indexOf(item);
		if (index > 0) {
			return pUuid.substring(0, index + item.length() + 5) + "_";
		} else {
			return pUuid.substring(0, pUuid.indexOf("_") + 5) + "_";
		}
	}

	private String getComponentName(Component comp) {
		return comp.getClass().getSimpleName().toLowerCase();
	}
	
	private Integer getComponentIdIndex(Scope p, Component comp) {
		String key  = ID_GENERATOR_KEY + getComponentName(comp);
		Object index = p.getAttribute(key);
		
		if (index == null) {
			index = "0";
		}
		
		int i = Integer.parseInt(index.toString());
		i++;// Start from 1
		p.setAttribute(key, String.valueOf(i));
		return i;
	}
	
	
	public String nextComponentUuid(Desktop desktop, Component comp,
			ComponentInfo info) {
		return nextComponentUuid(desktop, comp);
	}

	public String nextDesktopId(Desktop desktop) {
		if (desktop.getAttribute(ID_NUMBER) == null) {
			desktop.setAttribute(ID_NUMBER, "0");		
		}
		return null;
	}

	public String nextPageUuid(Page page) {
		return null;
	}
}