summaryrefslogtreecommitdiffstats
path: root/src/beans/CategoryManager.java
blob: cfa8bbaf11cc1d15922ca584d42dbca8cf64fb3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * $RCSFile$
 *
 * Created on 08.03.2007
 * for Project: 
 * by steins
 *
 * (C) 2005-2006 by 
 */
package beans;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import jpa.Category;
import jpa.EntityManagerFactorySingleton;

@ManagedBean(name = "CategoryManager")
@SessionScoped
public class CategoryManager {
	private Category current;

	public Category getCurrent() {
		return current;
	}

	public void setCurrent(Category current) {
		this.current = current;
	}

	public Collection getCategories() {
		EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory()
				.createEntityManager();

		Collection categories = manager.createQuery("SELECT c FROM Category c ").getResultList();
		manager.close();

		return categories;
	}

	public void select(javax.faces.event.ActionEvent actionEvent) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		Map params = facesContext.getExternalContext().getRequestParameterMap();
		Integer selectedId = Integer.valueOf((String) params.get("selectedId"));
		// System.out.println(selectedId);

		EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory()
				.createEntityManager();

		EntityTransaction tx = manager.getTransaction();
		tx.begin();
		try {
			current = manager.find(Category.class, selectedId);
			// System.out.println(current.getName());
			tx.commit();
		} catch (Exception ex) {
			ex.printStackTrace(System.err);
			tx.rollback();
		}

		FacesContext context = FacesContext.getCurrentInstance();
		try {
			context.getExternalContext().redirect("products.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
		context.responseComplete();
		manager.close();
	}

	public void edit(javax.faces.event.ActionEvent actionEvent) {
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> params = context.getExternalContext().getRequestParameterMap();
		Integer selectedId = Integer.valueOf((String) params.get("selectedId"));
		// System.out.println(selectedId);

		EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory()
				.createEntityManager();

		EntityTransaction tx = manager.getTransaction();
		tx.begin();
		try {
			current = manager.find(Category.class, selectedId);
			// System.out.println(current.getName());
			tx.commit();
		} catch (Exception ex) {
			ex.printStackTrace(System.err);
			tx.rollback();
		}

		try {
			context.getExternalContext().redirect("category.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
		context.responseComplete();
	}

	public void deleteCurrentCategory(javax.faces.event.ActionEvent actionEvent) {
		FacesContext context = FacesContext.getCurrentInstance();

		EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory()
				.createEntityManager();
		EntityTransaction tx = manager.getTransaction();
		tx.begin();
		try {
			current = manager.find(Category.class, current.getId());
			manager.remove(current);
			current = null;

			tx.commit();
		} catch (Exception ex) {
			ex.printStackTrace(System.err);
			if (tx.isActive())
				tx.rollback();
		}

		try {
			context.getExternalContext().redirect("categories.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
		context.responseComplete();
	}

	public void createCategory() {
		current = new Category();

		FacesContext context = FacesContext.getCurrentInstance();

		try {
			context.getExternalContext().redirect("category.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
		context.responseComplete();
	}

	public void saveCategory() {
		EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory()
				.createEntityManager();

		EntityTransaction tx = manager.getTransaction();
		try {
			tx.begin();
			current = manager.merge(current);

			tx.commit();
		} catch (Exception ex) {
			ex.printStackTrace(System.err);
			tx.rollback();
		}

		FacesContext context = FacesContext.getCurrentInstance();

		try {
			context.getExternalContext().redirect("categories.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
		context.responseComplete();
	}
}