package tutorial.example;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebService(
portName = "SurveyAppPort",
serviceName = "SurveyAppService",
targetNamespace = "http://gmuswe645.edu/wsdl",
endpointInterface = "tutorial.example.StudentServiceEJBRemoteInt")
@Stateless
public class StudentServiceHibernateImplWS implements StudentServiceEJBRemoteInt {
public StudentServiceHibernateImplWS() {}
public void SaveStudentSurveyFormData(Student postData){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.getTransaction();
tx.begin();
session.saveOrUpdate(postData.getContact());
DetachedCriteria maxId = DetachedCriteria.forClass(Contact.class)
.setProjection( Projections.max("id"));
Contact con = (Contact) session.createCriteria(Contact.class)
.add(Property.forName("id").eq(maxId))
.uniqueResult();
postData.setContactId(con.getId());
session.saveOrUpdate(postData);
tx.commit();
}
public Student GetStudentSurveyData(Student student){
Surveys surveys = GetSurveys(student);
for(Student survey : surveys.data){
if(survey.getLastName().equalsIgnoreCase(student.getLastName()) && survey.getFirstName().equalsIgnoreCase(student.getFirstName())){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Contact con = (Contact) session.createCriteria(Contact.class)
.add(Property.forName("id").eq(survey.getContactId()))
.uniqueResult();
survey.setContact(con);
return survey;
}
}
return null;
}
public Surveys GetSurveys(Student searchCriteria){
Surveys surveys = new Surveys();
surveys.data = new ArrayList<Student>();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria query = session.createCriteria(Student.class);
if(searchCriteria != null){
if(searchCriteria.getFirstName() != null && !searchCriteria.getFirstName().isEmpty()){
String firstName = searchCriteria.getFirstName().replace('*', '%');
query = query.add(Restrictions.like("firstName", firstName).ignoreCase());
}
if(searchCriteria.getLastName() != null && !searchCriteria.getLastName().isEmpty()){
String lastName = searchCriteria.getLastName().replace('*', '%');
query = query.add(Restrictions.like("lastName", lastName).ignoreCase());
}
if(searchCriteria.getCity() != null && !searchCriteria.getCity().isEmpty()){
String city = searchCriteria.getCity().replace('*', '%');
query = query.add(Restrictions.like("city", city).ignoreCase());
}
if(searchCriteria.getState() != null && !searchCriteria.getState().isEmpty()){
String state = searchCriteria.getState().replace('*', '%');
query = query.add(Restrictions.like("state", state).ignoreCase());
}
}
query = query.addOrder(Property.forName("lastName").asc());
query = query.addOrder(Property.forName("firstName").asc());
@SuppressWarnings("unchecked")
List<Student> surveyList = (List<Student>)query.list();
tx.commit();
if(surveyList.size() > 0){
surveys.data = surveyList;
}
return surveys;
}
@WebMethod
public List<Student> GetStudents(@WebParam Student searchCriteria) throws IOException{
Surveys surveys = GetSurveys(searchCriteria);
return surveys.data;
}
public List<String> GetSurveyNames(){
List<String> surveyNames = new ArrayList<String>();
Surveys surveys = GetSurveys(null);
for(Student survey : surveys.data){
surveyNames.add(survey.getFirstName() + " " + survey.getLastName());
}
return surveyNames;
}
public WinningResult ComputeMeanAndStandardDeviation(String raffle){
if(StringUtils.isEmpty(raffle)){
return new WinningResult(0,0);
}
String[] numbers = raffle.split(",");
double sum = 0;
for(int i=0; i < numbers.length; i++){
sum += Double.valueOf(numbers[i].trim());
}
double mean = Math.round((sum/numbers.length) * 100)/100;
double temp = 0;
for(int i=0; i < numbers.length; i++){
double num = Double.valueOf(numbers[i].trim());
temp += (mean - num) * (mean - num);
}
double variance = temp/numbers.length;
double standardDeviation = Math.round(Math.sqrt(variance) * 100)/100 ;
WinningResult data = new WinningResult(mean, standardDeviation);
return data;
}
@Override
public void DeleteStudent(Student postData) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=session.getTransaction();
tx.begin();
Student student = (Student) session.createCriteria(Student.class)
.add(Property.forName("id").eq(postData.getId()))
.uniqueResult();
int contactId = student.getContactId();
Contact con = (Contact) session.createCriteria(Contact.class)
.add(Property.forName("id").eq(contactId))
.uniqueResult();
session.delete(student);
session.delete(con);
tx.commit();
}
}
Web Service Interface
package tutorial.example;
import java.io.IOException;
import java.util.List;
import javax.ejb.Remote;
import javax.jws.WebService;
@WebService(targetNamespace = "http://gmuswe645.edu/wsdl")
@Remote
public interface StudentServiceEJBRemoteInt {
public List<Student> GetStudents(Student searchCriteria) throws IOException;
public Student GetStudentSurveyData(Student student);
public WinningResult ComputeMeanAndStandardDeviation(String raffle);
public void SaveStudentSurveyFormData(Student postData);
public void DeleteStudent(Student student);
}
Action Class
package tutorial.serviceexample;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import tutorial.example.Student;
import tutorial.example.StudentServiceEJBRemoteInt;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class SurveyAction extends ActionSupport implements ModelDriven<Student> {
private static final long serialVersionUID = 1L;
private Student student;
private List<Student> surveysList;
private Service surveyService;
private StudentServiceEJBRemoteInt surveyWs;
public SurveyAction(){
try {
surveyService = Service.create(
new URL("http://localhost:8080/surveyapp-tkhuc-ejb/SurveyAppService/StudentServiceHibernateImplWS?wsdl"),
new QName("http://gmuswe645.edu/wsdl", "SurveyAppService"));
surveyWs = surveyService.getPort(StudentServiceEJBRemoteInt.class);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public String searchsurvey() throws Exception{
surveysList = (List<Student>) surveyWs.GetStudents(student);
return SUCCESS;
}
public void validate(){
}
public Student getStudent(){
return student;
}
public void setStudent(Student student){
this.student = student;
}
public List<Student> getSurveysList() {
return surveysList;
}
public void setSurveysList(List<Student> surveysList) {
this.surveysList = surveysList;
}
@Override
public Student getModel() {
student = new Student();
return student;
}
public int getSurveysListCount() {
int count = 0;
if(surveysList != null)
count = surveysList.size();
return count;
}
}
No comments:
Post a Comment