Skip to content

Commit 3ec1c30

Browse files
committed
DAO Access made easy from Service Layer.
getId generalized in BaseDTO save method renamed to saveOrUpdate
1 parent a7eff85 commit 3ec1c30

13 files changed

Lines changed: 279 additions & 33 deletions

JavaSource/com/acminds/acuteauto/persistence/BaseDAO.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package com.acminds.acuteauto.persistence;
55

66
import java.util.Collection;
7-
import java.util.List;
87

98
import javax.persistence.Query;
109
import javax.persistence.TypedQuery;
@@ -26,17 +25,20 @@ public <T>T get(Class<T> clazz, Integer id) {
2625
return PersistenceManager.getEntityManager().find(clazz, id);
2726
}
2827

29-
public void save(BaseDTO entity, boolean commit) {
28+
public void saveOrUpdate(BaseDTO entity, boolean commit) {
3029
beginTxn();
30+
if(!PersistenceManager.getEntityManager().contains(entity)) {
31+
entity = PersistenceManager.getEntityManager().merge(entity);
32+
}
3133
PersistenceManager.getEntityManager().persist(entity);
3234
if(commit) commit();
3335
}
3436

3537
@SuppressWarnings("rawtypes")
36-
public void saveAll(Collection entities, boolean commit) {
38+
public void saveOrUpdateAll(Collection entities, boolean commit) {
3739
beginTxn();
3840
for(Object entity:entities)
39-
PersistenceManager.getEntityManager().persist(entity);
41+
saveOrUpdate((BaseDTO)entity, false);
4042
if(commit) commit();
4143
}
4244

@@ -46,9 +48,10 @@ public void delete(BaseDTO entity, boolean commit) {
4648
if(commit) commit();
4749
}
4850

49-
public void deleteAll(List<BaseDTO> entities, boolean commit) {
51+
@SuppressWarnings("rawtypes")
52+
public void deleteAll(Collection entities, boolean commit) {
5053
beginTxn();
51-
for(BaseDTO entity:entities)
54+
for(Object entity:entities)
5255
PersistenceManager.getEntityManager().remove(entity);
5356
if(commit) commit();
5457
}
@@ -57,6 +60,10 @@ public void detach(BaseDTO entity) {
5760
PersistenceManager.getEntityManager().detach(entity);
5861
}
5962

63+
public boolean isPersistent(BaseDTO entity) {
64+
return PersistenceManager.getEntityManager().contains(entity);
65+
}
66+
6067

6168
/*
6269
* ************************************************************************

JavaSource/com/acminds/acuteauto/persistence/BaseDTO.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,27 @@
33
*/
44
package com.acminds.acuteauto.persistence;
55

6+
import java.lang.reflect.Method;
7+
8+
import javax.persistence.Id;
9+
610
/**
711
* @author MANSUR
812
*
913
*/
1014
public class BaseDTO {
15+
16+
public Integer getId() {
17+
for(Method m:getClass().getMethods()) {
18+
if(m.isAnnotationPresent(Id.class)) {
19+
try {
20+
return (Integer)m.invoke(this, new Object[]{});
21+
} catch (Exception e) {
22+
return null;
23+
}
24+
}
25+
}
26+
return null;
27+
}
1128

1229
}

JavaSource/com/acminds/acuteauto/persistence/entities/AbstractUserInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void setLastLoginDate(Date lastLoginDate) {
167167
}
168168

169169
@Temporal(TemporalType.TIMESTAMP)
170-
@Column(name = "CREATE_DATE", nullable = false, length = 19)
170+
@Column(name = "CREATE_DATE", nullable = false, insertable=false, updatable=false, length = 19)
171171
public Date getCreateDate() {
172172
return this.createDate;
173173
}

JavaSource/com/acminds/acuteauto/service/BaseService.java

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,122 @@
33
*/
44
package com.acminds.acuteauto.service;
55

6+
import java.util.Collection;
7+
8+
import javax.persistence.Query;
9+
import javax.persistence.TypedQuery;
10+
11+
import org.apache.commons.logging.Log;
12+
import org.apache.commons.logging.LogFactory;
13+
614
import com.acminds.acuteauto.persistence.BaseDAO;
15+
import com.acminds.acuteauto.persistence.BaseDTO;
716

817
/**
918
* @author Mansur
1019
*
1120
*/
1221
public class BaseService {
13-
private BaseDAO baseDao = BaseDAO.getInstance();
22+
protected Log logger = LogFactory.getLog(this.getClass());
23+
24+
protected BaseDAO baseDao = BaseDAO.getInstance();
1425
public BaseDAO getBaseDao() {
1526
return baseDao;
1627
}
28+
29+
public <T>T get(Class<T> clazz, Integer id) {
30+
return baseDao.get(clazz, id);
31+
}
32+
33+
public void saveOrUpdate(BaseDTO entity, boolean commit) {
34+
baseDao.saveOrUpdate(entity, commit);
35+
}
36+
37+
@SuppressWarnings("rawtypes")
38+
public void saveOrUpdateAll(Collection entities, boolean commit) {
39+
baseDao.saveOrUpdateAll(entities, commit);
40+
}
41+
42+
public void delete(BaseDTO entity, boolean commit) {
43+
baseDao.delete(entity, commit);
44+
}
45+
46+
@SuppressWarnings("rawtypes")
47+
public void deleteAll(Collection entities, boolean commit) {
48+
baseDao.deleteAll(entities, commit);
49+
}
50+
51+
public void detach(BaseDTO entity) {
52+
baseDao.detach(entity);
53+
}
54+
55+
public boolean isPersistent(BaseDTO entity) {
56+
return baseDao.isPersistent(entity);
57+
}
58+
/*
59+
* ************************************************************************
60+
* ************************************************************************
61+
*
62+
* Transaction management: begin, commit, rollback, flush, clear
63+
*
64+
* ************************************************************************
65+
* ************************************************************************
66+
*/
67+
68+
69+
public void commit() {
70+
baseDao.commit();
71+
}
1772

73+
public void beginTxn() {
74+
baseDao.beginTxn();
75+
}
76+
77+
public final void rollback()
78+
{
79+
baseDao.rollback();
80+
}
81+
82+
public final void flush()
83+
{
84+
baseDao.flush();
85+
}
86+
87+
public final void clearEntityManager()
88+
{
89+
baseDao.clearEntityManager();
90+
}
91+
92+
/*
93+
* ************************************************************************
94+
* ************************************************************************
95+
*
96+
* Queries
97+
*
98+
* ************************************************************************
99+
* ************************************************************************
100+
*/
101+
102+
public Query createQuery(String query) {
103+
return baseDao.createQuery(query);
104+
}
105+
106+
public <T>TypedQuery<T> createQuery(String query, Class<T> clazz) {
107+
return baseDao.createQuery(query, clazz);
108+
}
109+
110+
public Query createNamedQuery(String name) {
111+
return baseDao.createNamedQuery(name);
112+
}
113+
114+
public <T>TypedQuery<T> createNamedQuery(String query, Class<T> clazz) {
115+
return baseDao.createNamedQuery(query, clazz);
116+
}
117+
118+
public Query createNativeQuery(String query) {
119+
return baseDao.createNativeQuery(query);
120+
}
121+
122+
18123

19124
}

JavaSource/com/acminds/acuteauto/service/InventoryService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,17 @@ public List<Vehicle> getCarsForAdvertisement() {
5252
cars.add(a.getVehicle());
5353
return cars;
5454
}
55+
56+
public void saveOrUpdateCar() {
57+
58+
}
59+
60+
public void saveOrUpdateCarImages() {
61+
62+
}
63+
64+
public void deleteCar() {
65+
66+
}
5567

5668
}

JavaSource/com/acminds/acuteauto/ui/BaseController.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import java.util.ArrayList;
77
import java.util.List;
88

9+
import javax.faces.bean.ManagedProperty;
910
import javax.faces.model.SelectItem;
1011

12+
import com.acminds.acuteauto.persistence.dto.UserInfo;
13+
import com.acminds.acuteauto.service.BaseService;
1114
import com.acminds.acuteauto.utils.Utils;
1215
import com.acminds.acuteauto.utils.WebUtils;
1316

@@ -16,7 +19,17 @@
1619
*
1720
*/
1821
public class BaseController {
22+
protected BaseService baseService = new BaseService();
1923

24+
@ManagedProperty(value="#{authorizedUser}")
25+
protected UserInfo authorizedUser;
26+
public UserInfo getAuthorizedUser() {
27+
return authorizedUser;
28+
}
29+
public void setAuthorizedUser(UserInfo authorizedUser) {
30+
this.authorizedUser = authorizedUser;
31+
}
32+
2033
private List<SelectItem> years = new ArrayList<SelectItem>();
2134
private List<SelectItem> prices = new ArrayList<SelectItem>();
2235

@@ -41,5 +54,16 @@ public List<SelectItem> getPrices(boolean isDefault, String defaultValue) {
4154
}
4255
return prices;
4356
}
57+
58+
public String beginTxn() {
59+
baseService.beginTxn();
60+
return null;
61+
}
62+
63+
public String flush() {
64+
baseService.flush();
65+
baseService.commit();
66+
return null;
67+
}
4468

4569
}

JavaSource/com/acminds/acuteauto/ui/controller/HomePageController.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55

66
import java.util.List;
77

8-
import javax.faces.bean.ApplicationScoped;
8+
import javax.faces.application.FacesMessage;
99
import javax.faces.bean.ManagedBean;
10+
import javax.faces.bean.SessionScoped;
11+
12+
import org.apache.commons.logging.Log;
13+
import org.apache.commons.logging.LogFactory;
1014

1115
import com.acminds.acuteauto.persistence.dto.Category;
1216
import com.acminds.acuteauto.persistence.dto.Client;
@@ -21,9 +25,11 @@
2125
* @author Mansur
2226
*
2327
*/
24-
@ManagedBean(name="hmpCtrl", eager=true)
25-
@ApplicationScoped
26-
public class HomePageController extends BaseController{
28+
@ManagedBean(name="hmpCtrl")
29+
@SessionScoped
30+
public class HomePageController extends BaseController{
31+
private Log logger = LogFactory.getLog(this.getClass());
32+
2733
private InventoryService service = new InventoryService();
2834
private List<Category> menuGroup;
2935
private List<Category> homeGroup;
@@ -84,10 +90,27 @@ public List<Vehicle> getCarsForAdvertisement() {
8490

8591
public String reset() {
8692
this.dealer = null;
93+
this.menuGroup = null;
8794
this.homeGroup = null;
8895
this.carsForBanner = null;
8996
this.featuredCars = null;
9097
this.carsForAdvertisement = null;
9198
return null;
9299
}
100+
101+
public String saveMenuGroup() {
102+
try {
103+
service.saveOrUpdateAll(menuGroup, false);
104+
service.flush();
105+
service.commit();
106+
this.menuGroup = null;
107+
logger.info("Menu saved successfully.");
108+
WebUtils.addMessage(FacesMessage.SEVERITY_INFO, "menuSaved");
109+
} catch(Exception e) {
110+
service.rollback();
111+
logger.error("Menu could not be saved due to an internal error.", e);
112+
WebUtils.addMessage(FacesMessage.SEVERITY_ERROR, "submitFailed");
113+
}
114+
return "/sec/adminConsole";
115+
}
93116
}

JavaSource/com/acminds/acuteauto/ui/controller/InquiryController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public String findVehicle() {
4545
try {
4646
FindVehicle fv = inquiry.getFindVehicles().get(0);
4747
fv.setModel(service.getBaseDao().get(Model.class, getModelId()));
48-
service.getBaseDao().save(inquiry, false);
49-
service.getBaseDao().saveAll(inquiry.getFindVehicles(), true);
48+
service.getBaseDao().saveOrUpdate(inquiry, false);
49+
service.getBaseDao().saveOrUpdateAll(inquiry.getFindVehicles(), true);
5050
WebUtils.addMessage(FacesMessage.SEVERITY_INFO, "findVehSuccess");
5151
init();
5252
logger.info("Find Vehicle Inquiry submitted successfully.");
5353
} catch (Exception e) {
5454
logger.error("Find Vehicle Inquiry failed due to an internal error.", e);
5555
service.getBaseDao().rollback();
56-
WebUtils.addMessage(FacesMessage.SEVERITY_INFO, "submitFailed");
56+
WebUtils.addMessage(FacesMessage.SEVERITY_ERROR, "submitFailed");
5757
}
5858
return null;
5959
}
@@ -62,14 +62,14 @@ public String submitInquiry() {
6262
try {
6363
inquiry.getFindVehicles().clear();
6464
inquiry.setVehicle(getCar());
65-
service.getBaseDao().save(inquiry, true);
65+
service.getBaseDao().saveOrUpdate(inquiry, true);
6666
WebUtils.addMessage(FacesMessage.SEVERITY_INFO, "inqSuccess");
6767
init();
6868
logger.info("Inquiry submitted successfully.");
6969
} catch (Exception e) {
7070
logger.error("Inquiry failed due to an internal error.", e);
7171
service.getBaseDao().rollback();
72-
WebUtils.addMessage(FacesMessage.SEVERITY_INFO, "submitFailed");
72+
WebUtils.addMessage(FacesMessage.SEVERITY_ERROR, "submitFailed");
7373
}
7474
return "/pub/inv/invList";
7575
}

JavaSource/com/acminds/acuteauto/ui/controller/LoanAppController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ public void init() {
6666
public String submitLoan() {
6767
try {
6868
loanApp.setVehicle(getCar());
69-
service.getBaseDao().save(loanApp, false);
69+
service.getBaseDao().saveOrUpdate(loanApp, false);
7070
for(Applicant app:loanApp.getApplicants()) {
71-
service.getBaseDao().save(app, false);
71+
service.getBaseDao().saveOrUpdate(app, false);
7272
for(Residence r:app.getResidences()) {
73-
service.getBaseDao().save(r, false);
74-
service.getBaseDao().save(r.getLocation(), false);
73+
service.getBaseDao().saveOrUpdate(r, false);
74+
service.getBaseDao().saveOrUpdate(r.getLocation(), false);
7575
}
7676
}
77-
service.getBaseDao().saveAll(loanApp.getTradeinInfos(), false);
77+
service.getBaseDao().saveOrUpdateAll(loanApp.getTradeinInfos(), false);
7878
service.getBaseDao().commit();
7979
init();
8080
WebUtils.addMessage(FacesMessage.SEVERITY_INFO, "loanAppSuccess");

0 commit comments

Comments
 (0)