
Day 10
This blog is not restricted with JSF and Rich Faces though it will have a maximum posts regarding it.The posts will generally help a developer on its way for achieving generalized tasks required to implement in a project.
<f:subview id="converter3">
<h:form>
<br/>
<br/>
<h:outputLabel value="UpperCase Converter :"/>
<h:inputText id="text1" converter="uppercaseConverter"
converterMessage="Not a String"
value="#{startPage.converterChangeCase}"
onchange="submit()"/>
<h:message for="text1"/>
</h:form>
</f:subview>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee">
<managed-bean>
<managed-bean-name>startPage</managed-bean-name>
<managed-bean-class>com.java.jsf.view.backing.StartPage</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<!--oracle-jdev-comment:managed-bean-jsp-link:1WEB-INF/pages/start_page.jspx-->
</managed-bean>
<converter>
<converter-id>uppercaseConverter</converter-id>
<converter-class> com.java.jsf.view.util.UpperCaseConverter
</converter-class>
</converter>
</faces-config>
public class UpperCaseConverter implements Converter{
public Object getAsObject(FacesContext facesContext,UIComponent component,String stringValue){
return stringValue.toUpperCase();
}
public String getAsString(FacesContext facesContext,
UIComponent uiComponent, Object objectValue) {
return objectValue.toString();
}
}
public class StartPage{
private String converterChangeCase;
public void setConverterChangeCase(String converterChangeCase) {
System.out.println("Change Case : "+converterChangeCase);
this.converterChangeCase = converterChangeCase;
}
public String getConverterChangeCase(){
return converterChangeCase;
}
}
<f:view>
<f:subview id="converter1">
<h:form>
<br/>
<br/>
<h:outputLabel value="Date Converter :"/>
<h:inputText id="text"
value="#{startPage.converterDate}"
onchange="submit()">
<f:convertDateTime pattern="yyyy/MM/dd" type="date"/>
</h:inputText>
<h:message for="text"/>
<br/>
</h:form>
</f:subview>
<f:subview id="converter2">
<h:form>
<br/>
<br/>
<h:outputLabel value="Integer Converter :"/>
<h:inputText id="text2"
value="#{startPage.converterAge}"
onchange="submit()"
converter="javax.faces.Integer"/>
<h:message for="text2"/>
<br/>
</h:form>
</f:subview>
<f:view>
private Date converterDate;
private int converterAge;
public void setConverterDate(Date converterDate) {
System.out.println("Date Entered : "+converterDate);
this.converterDate = converterDate;
}
public Date getConverterDate() {
return converterDate;
}
public void setConverterAge(int converterAge) {
System.out.println("Age Entered : "+converterAge);
this.converterAge = converterAge;
}
public int getConverterAge() {
return converterAge;
}
public class MD5 {
public static String digest(String text) throws NoSuchAlgorithmException,
UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] md5hash = new byte[32];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
md5hash = md.digest();
return convertToHex(md5hash);
}
private static String convertToHex(byte[] b) {
StringBuilder result = new StringBuilder(32);
for (int i = 0; i < b.length; i++) {
result.append(Integer.toString((b[i] & 0xff) + 0x100, 16)
.substring(1));
}
System.out.println(result.toString());
return result.toString();
}
}
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<f:subview id="manyCheck">
<h:form>
<h:selectManyCheckbox id="contact" value="#{echoBean.contact}">
<f:selectItem itemLabel="All" itemValue="All" />
<f:selectItem itemLabel="Phone" itemValue="Phone" />
<f:selectItem itemLabel="Mob" itemValue="Mob" />
<f:selectItem itemLabel="Email" itemValue="Email" />
<f:selectItem itemLabel="Fax" itemValue="Fax" />
<a4j:support event="onchange" action="#{echoBean.saveManyCheckBox}" reRender="as"/>
</h:selectManyCheckbox>
</h:form>
</f:subview>
<f:subview id="manyCheck1">
<a4j:form id="as">
<h:selectManyCheckbox id="contact1" value="#{echoBean.resultList}" rendered="#!{echoBean.resultList}">
<f:selectItem itemLabel="All" itemValue="All" />
<f:selectItem itemLabel="Phone" itemValue="Phone" />
<f:selectItem itemLabel="Mob" itemValue="Mob" />
<f:selectItem itemLabel="Email" itemValue="Email" />
<f:selectItem itemLabel="Fax" itemValue="Fax" />
</h:selectManyCheckbox>
</a4j:form>
</f:subview>
private List contact;
private List resultList;
// Setters & Getters to be created
public void saveManyCheckBox() {
System.out.println("In saveManyCheck");
System.out.println("Before List Values:" + contact);
String str = this.list2String(this.contact);
System.out.println("STR Values:" + str);
resultList = this.string2List(str);
System.out.println("ResultList Values:" + resultList);
}
public List string2List(String str) {
List list = new ArrayList();
String str1 = str.replaceAll("[\\]\\[]", "");
StringTokenizer tokens = new StringTokenizer(str1, ",");
int i = 0;
while (tokens.hasMoreTokens()) {
String f = tokens.nextToken();
list.add(f.trim());
i++;
}
return list;
}
public String list2String(List list) {
String str = null;
str = list.toString();
return str;
}
package com.odisys.test;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class HelloSchedule {
public HelloSchedule() throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler schedule = sf.getScheduler();
schedule.start();
//long ts = Long.parseLong("October 24, 2009, 11:03:00 IST");
JobDetail jd = new JobDetail("myjob", schedule.DEFAULT_GROUP,
HelloJob.class);
SimpleTrigger st = new SimpleTrigger("mytrigger", schedule.DEFAULT_GROUP,new Date(getCurrentDate()), null, 4, 60L * 1000L);
schedule.scheduleJob(jd, st);
}
public static long getCurrentDate() throws ParseException {
DateFormat dateFormat = new SimpleDateFormat();
Date date = new Date();
String currentDate = (dateFormat.format(date));
Date dt2 = new SimpleDateFormat().parse(currentDate);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(dt2);
long milliseconds1 = calendar2.getTimeInMillis();
return milliseconds1;
}
public static void main(String args[]) {
try {
new HelloSchedule();
} catch (Exception e) {
}
}
}
package com.odisys.test;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
public class HelloJob implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Hello World Quartz Scheduler: " + new Date());
//System.exit(0);
}
}