Skip to content

Commit 68c2521

Browse files
committed
Querying - starting point
1 parent d20271a commit 68c2521

File tree

11 files changed

+294
-74
lines changed

11 files changed

+294
-74
lines changed

Diff for: lesson05-infinispan-updated/carmart/pom.xml

+21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<jboss-as-management-host>localhost</jboss-as-management-host>
1616
<jbossas.version>7.5.Final</jbossas.version>
1717
<infinispan.version>6.0.0.CR1</infinispan.version>
18+
<version.hibernate.search>4.4.0.Beta1</version.hibernate.search>
1819
</properties>
1920

2021
<dependencies>
@@ -48,6 +49,26 @@
4849
<version>${jsf.version}</version>
4950
<scope>provided</scope>
5051
</dependency>
52+
<dependency>
53+
<groupId>org.hibernate</groupId>
54+
<artifactId>hibernate-search-engine</artifactId>
55+
<version>${version.hibernate.search}</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.hibernate</groupId>
59+
<artifactId>hibernate-search-analyzers</artifactId>
60+
<version>${version.hibernate.search}</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.hibernate</groupId>
64+
<artifactId>hibernate-search-infinispan</artifactId>
65+
<version>${version.hibernate.search}</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.infinispan</groupId>
69+
<artifactId>infinispan-query</artifactId>
70+
<version>${infinispan.version}</version>
71+
</dependency>
5172
</dependencies>
5273

5374
<repositories>

Diff for: lesson05-infinispan-updated/carmart/src/main/java/com/jboss/datagrid/carmart/jsf/PopulateCache.java

+59-20
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import javax.enterprise.context.spi.CreationalContext;
4040
import javax.enterprise.inject.spi.Bean;
4141
import javax.enterprise.inject.spi.BeanManager;
42+
import javax.transaction.UserTransaction;
4243

4344
/**
4445
* Populates a cache with initial data. We need to obtain BeanManager from JNDI and create an instance of CacheContainerProvider
@@ -52,6 +53,8 @@ public class PopulateCache implements SystemEventListener {
5253
private Logger log = Logger.getLogger(this.getClass().getName());
5354

5455
private CacheContainerProvider provider;
56+
57+
private UserTransaction utx;
5558

5659
@Override
5760
public void processEvent(SystemEvent event) throws AbortProcessingException {
@@ -60,27 +63,51 @@ public void processEvent(SystemEvent event) throws AbortProcessingException {
6063
}
6164

6265
public void startup() {
63-
BasicCache<String, Object> cars = provider.getCacheContainer().getCache(CarManager.CACHE_NAME);
66+
BasicCache<String, Object> cars = provider.getCacheContainer().getCache(CarManager.CAR_CACHE_NAME);
67+
BasicCache<String, Object> carList = provider.getCacheContainer().getCache(CarManager.CAR_LIST_CACHE_NAME);
6468
List<String> carNumbers = new ArrayList<String>();
65-
66-
Car c = new Car("Ford Focus", 1.6, CarType.COMBI, "white", "FML 23-25", Country.CZECH_REPUBLIC);
67-
carNumbers.add(c.getNumberPlate());
68-
cars.put(CarManager.encode(c.getNumberPlate()), c);
69-
c = new Car("BMW X3", 2.0, CarType.SEDAN, "gray", "1P3 2632", Country.CZECH_REPUBLIC);
70-
carNumbers.add(c.getNumberPlate());
71-
cars.put(CarManager.encode(c.getNumberPlate()), c);
72-
c = new Car("Ford Mondeo", 2.2, CarType.COMBI, "blue", "1B2 1111", Country.USA);
73-
carNumbers.add(c.getNumberPlate());
74-
cars.put(CarManager.encode(c.getNumberPlate()), c);
75-
c = new Car("Mazda MX-5", 1.8, CarType.CABRIO, "red", "6T4 2526", Country.USA);
76-
carNumbers.add(c.getNumberPlate());
77-
cars.put(CarManager.encode(c.getNumberPlate()), c);
78-
c = new Car("VW Golf", 1.6, CarType.HATCHBACK, "yellow", "2B2 4946", Country.GERMANY);
79-
carNumbers.add(c.getNumberPlate());
80-
cars.put(CarManager.encode(c.getNumberPlate()), c);
81-
// insert a list of cars' number plates
82-
cars.put(CarManager.CAR_NUMBERS_KEY, carNumbers);
83-
log.info("Successfully imported data!");
69+
70+
utx = getUserTransactionFromJNDI();
71+
72+
try {
73+
utx.begin();
74+
Car c = new Car("Ford Focus", 1.6, CarType.COMBI, "white", "FML 23-25", Country.CZECH_REPUBLIC);
75+
carNumbers.add(c.getNumberPlate());
76+
cars.put(CarManager.encode(c.getNumberPlate()), c);
77+
c = new Car("BMW X3", 2.0, CarType.SEDAN, "gray", "1P3 2632", Country.CZECH_REPUBLIC);
78+
carNumbers.add(c.getNumberPlate());
79+
cars.put(CarManager.encode(c.getNumberPlate()), c);
80+
c = new Car("Ford Mondeo", 2.2, CarType.COMBI, "blue", "1B2 1111", Country.USA);
81+
carNumbers.add(c.getNumberPlate());
82+
cars.put(CarManager.encode(c.getNumberPlate()), c);
83+
c = new Car("Mazda MX-5", 1.8, CarType.CABRIO, "red", "6T4 2526", Country.USA);
84+
carNumbers.add(c.getNumberPlate());
85+
cars.put(CarManager.encode(c.getNumberPlate()), c);
86+
c = new Car("VW Golf", 1.6, CarType.HATCHBACK, "yellow", "2B2 4946", Country.GERMANY);
87+
carNumbers.add(c.getNumberPlate());
88+
cars.put(CarManager.encode(c.getNumberPlate()), c);
89+
c = new Car("VW Passat", 2.0, CarType.COMBI, "black", "7T7 7777", Country.GERMANY);
90+
carNumbers.add(c.getNumberPlate());
91+
cars.put(CarManager.encode(c.getNumberPlate()), c);
92+
c = new Car("Renault Clio", 1.8, CarType.HATCHBACK, "blue", "8E6 8456", Country.CZECH_REPUBLIC);
93+
carNumbers.add(c.getNumberPlate());
94+
cars.put(CarManager.encode(c.getNumberPlate()), c);
95+
c = new Car("Renault Megane", 2.0, CarType.SEDAN, "red", "6A6 6666", Country.CZECH_REPUBLIC);
96+
carNumbers.add(c.getNumberPlate());
97+
cars.put(CarManager.encode(c.getNumberPlate()), c);
98+
// insert a list of cars' number plates
99+
carList.put(CarManager.CAR_NUMBERS_KEY, carNumbers);
100+
utx.commit();
101+
log.info("Successfully imported data!");
102+
} catch (Exception e) {
103+
log.warning("An exception occured while populating the database! Rolling back the transaction.");
104+
if (utx != null) {
105+
try {
106+
utx.rollback();
107+
} catch (Exception e1) {
108+
}
109+
}
110+
}
84111
}
85112

86113
private BeanManager getBeanManagerFromJNDI() {
@@ -94,6 +121,18 @@ private BeanManager getBeanManagerFromJNDI() {
94121
}
95122
return (BeanManager) result;
96123
}
124+
125+
private UserTransaction getUserTransactionFromJNDI() {
126+
InitialContext context;
127+
Object result;
128+
try {
129+
context = new InitialContext();
130+
result = context.lookup("java:comp/UserTransaction"); // lookup in JBossAS
131+
} catch (NamingException ex) {
132+
throw new RuntimeException("UserTransaction could not be found in JNDI", ex);
133+
}
134+
return (UserTransaction) result;
135+
}
97136

98137
@SuppressWarnings("unchecked")
99138
public <T> T getContextualInstance(final BeanManager manager, final Class<T> type) {

Diff for: lesson05-infinispan-updated/carmart/src/main/java/com/jboss/datagrid/carmart/model/Car.java

+52-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
*/
2222
package com.jboss.datagrid.carmart.model;
2323

24+
import org.hibernate.search.annotations.DocumentId;
25+
import org.hibernate.search.annotations.Field;
26+
import org.hibernate.search.annotations.FieldBridge;
27+
import org.hibernate.search.annotations.Indexed;
28+
import org.hibernate.search.annotations.ProvidedId;
29+
import org.hibernate.search.bridge.builtin.StringBridge;
30+
2431
import java.io.Serializable;
2532

2633
/**
@@ -29,6 +36,7 @@
2936
* @author Martin Gencur
3037
*
3138
*/
39+
@Indexed(index="CarIndex")
3240
public class Car implements Serializable {
3341

3442
private static final long serialVersionUID = 188164481825309731L;
@@ -38,7 +46,7 @@ public enum CarType {
3846
}
3947

4048
public enum Country {
41-
CZECH_REPUBLIC, USA, GERMANY
49+
Unused, CZECH_REPUBLIC, USA, GERMANY
4250
}
4351

4452
public Car() {
@@ -53,19 +61,30 @@ public Car(String brand, double displacement, CarType type, String color, String
5361
this.country = country;
5462
}
5563

64+
@Field
5665
private String brand;
66+
67+
@Field
5768
private double displacement;
69+
70+
@Field
5871
private CarType type;
72+
73+
@Field
5974
private String color;
60-
private String numberPlate;
75+
76+
@Field
6177
private Country country;
6278

79+
@Field
80+
private String numberPlate;
81+
6382
public String getBrand() {
6483
return brand;
6584
}
6685

6786
public void setBrand(String brand) {
68-
this.brand = brand;
87+
this.brand = brand.trim();
6988
}
7089

7190
public CarType getType() {
@@ -81,15 +100,15 @@ public String getNumberPlate() {
81100
}
82101

83102
public void setNumberPlate(String numberPlate) {
84-
this.numberPlate = numberPlate;
103+
this.numberPlate = numberPlate.trim();
85104
}
86105

87106
public String getColor() {
88107
return color;
89108
}
90109

91110
public void setColor(String color) {
92-
this.color = color;
111+
this.color = color.trim();
93112
}
94113

95114
public double getDisplacement() {
@@ -107,4 +126,32 @@ public void setCountry(Country country) {
107126
public Country getCountry() {
108127
return country;
109128
}
129+
130+
@Override
131+
public boolean equals(Object o) {
132+
if (this == o) return true;
133+
if (o == null || getClass() != o.getClass()) return false;
134+
Car car = (Car) o;
135+
if (Double.compare(car.displacement, displacement) != 0) return false;
136+
if (brand != null ? !brand.equals(car.brand) : car.brand != null) return false;
137+
if (color != null ? !color.equals(car.color) : car.color != null) return false;
138+
if (country != car.country) return false;
139+
if (numberPlate != null ? !numberPlate.equals(car.numberPlate) : car.numberPlate != null) return false;
140+
if (type != car.type) return false;
141+
return true;
142+
}
143+
144+
@Override
145+
public int hashCode() {
146+
int result;
147+
long temp;
148+
result = brand != null ? brand.hashCode() : 0;
149+
temp = displacement != +0.0d ? Double.doubleToLongBits(displacement) : 0L;
150+
result = 31 * result + (int) (temp ^ (temp >>> 32));
151+
result = 31 * result + (type != null ? type.hashCode() : 0);
152+
result = 31 * result + (color != null ? color.hashCode() : 0);
153+
result = 31 * result + (country != null ? country.hashCode() : 0);
154+
result = 31 * result + (numberPlate != null ? numberPlate.hashCode() : 0);
155+
return result;
156+
}
110157
}

0 commit comments

Comments
 (0)