📄 sessionfacadebean.java
字号:
package com.mydomain.ejb;
import com.mydomain.dvc.Location;
import java.util.*;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.naming.InitialContext;
public class SessionFacadeBean implements SessionBean {
private SessionContext ctx;
private CompanyHome compHome;
private EmployeeHome empHome;
private TaskHome taskHome;
public void ejbCreate() throws CreateException {
try {
InitialContext jndi = new InitialContext();
compHome = (CompanyHome) jndi.lookup("CMP-Example/CompanyEJB");
empHome = (EmployeeHome) jndi.lookup("CMP-Example/EmployeeEJB");
taskHome = (TaskHome) jndi.lookup("CMP-Example/TaskEJB");
} catch(Exception e) {
throw new CreateException("Error while looking up Home interfaces");
}
}
public void createAllEntities(String compname,String compcountry,String compaddress,
String compphone,String empname,String empsurname,
String empphone,String taskname) throws Exception {
try {
Company compRemote = createCompany(compname,compcountry,compaddress,compphone);
Employee empRemote = createEmployee(empname,empsurname,empphone);
Task taskRemote = createTask(taskname);
//codice per le associazioni
empRemote.setCompany(compRemote);
Collection c = taskHome.findEmpsTasks(empRemote.getName());
c.add(taskRemote);
empRemote.setTasks(c);
} catch(Exception exc) {
throw exc;
}
}
public void createCompanyEmployee(String compname,String compcountry,String compaddress,
String compphone,String empname,String empsurname,String empphone) throws Exception {
try {
Company compRemote = createCompany(compname,compcountry,compaddress,compphone);
Employee empRemote = createEmployee(empname,empsurname,empphone);
//codice per le associazioni
empRemote.setCompany(compRemote);
} catch(Exception exc) {
throw exc;
}
}
public void createEmployeeTask(String empname,String empsurname,String empphone,String taskname) throws Exception {
try {
Employee empRemote = createEmployee(empname,empsurname,empphone);
Task taskRemote = createTask(taskname);
//codice per le associazioni
Collection c = taskHome.findEmpsTasks(empRemote.getName());
c.add(taskRemote);
empRemote.setTasks(c);
} catch(Exception exc) {
throw exc;
}
}
public Company createCompany(String compname,String compcountry,
String compaddress,String compphone) throws Exception {
try {
Company compRemote = compHome.create(compname,new Location(compcountry,compaddress,compphone));
return compRemote;
} catch(CreateException ce) {
throw new Exception(ce.getMessage());
}
}
public Employee createEmployee(String empname,String empsurname,
String empphone) throws Exception {
try {
Employee empRemote = empHome.create(empname,empsurname,empphone);
return empRemote;
} catch(CreateException ce) {
throw new Exception(ce.getMessage());
}
}
public Task createTask(String taskname) throws Exception {
try {
Task taskRemote = taskHome.create(taskname);
return taskRemote;
} catch(CreateException ce) {
throw new Exception(ce.getMessage());
}
}
public List getAllEntities() throws Exception {
List allList = new ArrayList();
Collection compsColl = null;
Collection empsColl = null;
Collection tasksColl = null;
List compsList = new ArrayList();
List empsList = new ArrayList();
List tasksList = new ArrayList();
try {
compsColl = compHome.findAll();
empsColl = empHome.findAll();
tasksColl = taskHome.findAll();
} catch(FinderException fe) {
throw new Exception(fe.getMessage());
}
for(Iterator iter = compsColl.iterator();iter.hasNext();) {
Company compRemote = (Company)iter.next();
compsList.add(compRemote.getName());
System.out.println("company adding " + compRemote.getName() + " to list");
compsList.add(compRemote.getLocation().getCountry());
System.out.println("company adding " + compRemote.getLocation().getCountry() + " to list");
compsList.add(compRemote.getLocation().getAddress());
System.out.println("company adding " + compRemote.getLocation().getAddress() + " to list");
compsList.add(compRemote.getLocation().getPhone());
System.out.println("company adding " + compRemote.getLocation().getPhone() + " to list");
}
for(Iterator iter = empsColl.iterator();iter.hasNext();) {
Employee empRemote = (Employee)iter.next();
empsList.add(empRemote.getName());
System.out.println("employee adding " + empRemote.getName() + " to list");
empsList.add(empRemote.getSurname());
System.out.println("employee adding " + empRemote.getSurname() + " to list");
empsList.add(empRemote.getPhone());
System.out.println("employee adding " + empRemote.getPhone() + " to list");
}
for(Iterator iter = tasksColl.iterator();iter.hasNext();) {
Task taskRemote = (Task)iter.next();
tasksList.add(taskRemote.getName());
System.out.println("task adding " + taskRemote.getName() + " to list");
}
allList.add(compsList);
allList.add(empsList);
allList.add(tasksList);
return allList;
}
public List getCompEmpAssociations() throws Exception {
List myList = new ArrayList();
Collection emps = null;
try {
emps = empHome.findAll();
} catch(FinderException fe) {
throw new Exception(fe.getMessage());
}
for(Iterator iter = emps.iterator();iter.hasNext();) {
Employee empRemote = (Employee)iter.next();
if(empRemote.getCompany() != null) {
myList.add(empRemote.getName());
myList.add(empRemote.getCompany().getName());
}
}
return myList;
}
public List getEmpTaskAssociations() throws Exception {
List myList = new ArrayList();
Collection c = empHome.findAll();
for(Iterator iter = c.iterator();iter.hasNext();) {
Employee empRemote = (Employee)iter.next();
String empName = empRemote.getName();
Collection tasks = taskHome.findEmpsTasks(empName);
for(Iterator itero = tasks.iterator();itero.hasNext();) {
Task taskRemote = (Task)itero.next();
myList.add(empName);
myList.add(taskRemote.getName());
}
}
return myList;
}
public void associateCompanyEmployee(String compName,String empName) throws Exception {
Company compRemote = null;
Employee empRemote = null;
try {
compRemote = compHome.findByPrimaryKey(compName);
empRemote = empHome.findByPrimaryKey(empName);
} catch(FinderException fe) {
throw new Exception(fe.getMessage());
}
empRemote.setCompany(compRemote);
}
public void associateEmployeeTask(String empName,String taskName) throws Exception {
Employee empRemote = null;
Task taskRemote = null;
Collection c = null;
try {
taskRemote = taskHome.findByPrimaryKey(taskName);
empRemote = empHome.findByPrimaryKey(empName);
c = taskHome.findEmpsTasks(empRemote.getName());
c.add(taskRemote);
empRemote.setTasks(c);
} catch(FinderException fe) {
throw new Exception(fe.getMessage());
} catch(Exception exc) {
throw new Exception(exc.getMessage());
}
}
public void deleteCompany(String compName) throws Exception {
try {
Company compRemote = compHome.findByPrimaryKey(compName);
compRemote.remove();
} catch(FinderException fe) {
throw new Exception(fe.getMessage());
} catch(Exception exc) {
throw new Exception(exc.getMessage());
}
}
public void deleteEmployee(String empName) throws Exception {
try {
Employee empRemote = empHome.findByPrimaryKey(empName);
empRemote.remove();
} catch(FinderException fe) {
throw new Exception(fe.getMessage());
} catch(Exception exc) {
throw new Exception(exc.getMessage());
}
}
public void deleteTask(String taskName) throws Exception {
try {
Task taskRemote = taskHome.findByPrimaryKey(taskName);
taskRemote.remove();
} catch(FinderException fe) {
throw new Exception(fe.getMessage());
} catch(Exception exc) {
throw new Exception(exc.getMessage());
}
}
public void deleteCompanyEmployeeAssociation(String empName) throws Exception {
try {
Employee empRemote = empHome.findByPrimaryKey(empName);
empRemote.setCompany(null);
} catch(FinderException fe) {
throw new Exception(fe.getMessage());
} catch(Exception exc) {
throw new Exception(exc.getMessage());
}
}
public void deleteEmployeeTaskAssociation(String empName,String taskName) throws Exception {
try {
Employee empRemote = empHome.findByPrimaryKey(empName);
Task taskRemote = taskHome.findByPrimaryKey(taskName);
Collection c = taskHome.findEmpsTasks(empRemote.getName());
c.remove(taskRemote);
empRemote.setTasks(c);
} catch(FinderException fe) {
throw new Exception(fe.getMessage());
} catch(Exception exc) {
throw new Exception(exc.getMessage());
}
}
public void setSessionContext(SessionContext ctx) {
ctx = ctx;
}
public void ejbActivate() { }
public void ejbPassivate() { }
public void ejbRemove() { }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -