[r2222]: trunk / samples / core / src / main / java / org / SampleCORE.java Maximize Restore History

Download this file

SampleCORE.java    410 lines (347 with data), 14.8 kB

  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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright 2011-2014 Pavel Ponec
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ujorm.Key;
import org.ujorm.Ujo;
import org.ujorm.Validator;
import org.ujorm.core.KeyRing;
import org.ujorm.core.UjoComparator;
import org.ujorm.core.UjoManagerCSV;
import org.ujorm.criterion.*;
import org.ujorm.validator.ValidationException;
import static org.Company.CITY;
import static org.Employee.*;
/**
* The tutorial in the class for the UJO CORE <br>
* ------------------------------------------ <br>
* Learn the basic skills in 15 minutes by a live Java code.
*
* Entities:
* <ul>
* <li>{@link Employee} [id, name, wage, company]</li>
* <li>{@link Company} [id, name, city, created]</li>
* </ul>
*
* Copyright 2011-2014, Pavel Ponec
*
* @see Employee
* @see Company
*/
public class SampleCORE {
// ======= TUTORIAL MENU: =======
public static void main(String[] args) {
SampleCORE sample = new SampleCORE();
try {
sample.readWriteBean();
sample.readWriteUjo();
sample.defaultValues();
sample.numericDefaultValues();
sample.copyAttributes();
sample.keyValidator();
sample.localizedMessageOfValidator();
sample.compositeKey();
sample.theCriterion();
sample.criterionAsFilter();
sample.criterionAsFilterWithKey();
sample.sortEmployeeList();
sample.keySerialization();
sample.importCSV();
} catch (Exception e) {
Logger.getLogger(SampleCORE.class.getName()).log(Level.SEVERE, "Sample CORE", e);
throw new IllegalStateException("Sample CORE", e);
}
}
// ======= CHAPTERS: =======
/** See a common data access for the JavaBean object */
public void readWriteBean() {
Employee person = new Employee();
// Write:
person.setId(7L);
person.setName("Pavel");
person.setWage(20.00);
person.setCompany(new Company());
// Read:
Long id = person.getId();
String name = person.getName();
double wage = person.getWage();
Company company = person.getCompany();
assert id == 7L;
assert name == "Pavel";
assert wage == 20.00;
assert company != null;
}
/** See a data access using API of the {@link Ujo} object */
public void readWriteUjo() {
Employee person = new Employee();
// Write:
person.set(ID, 7L);
person.set(NAME, "Pavel");
person.set(WAGE, 20.00);
person.set(COMPANY, new Company());
// Read:
Long id = person.get(ID);
String name = person.get(NAME);
double wage = person.get(WAGE); // result is not never null due the default value
Company company = person.get(COMPANY);
assert id == 7L;
assert name == "Pavel";
assert wage == 20.00;
assert company != null;
// === Code sample where the compiler fails: ===
// person.set(Company.ID, 7L); // Key from another domain is not allowed
// person.set(Employee.ID, "Pavel"); // Wrong data type of the argument value
// String id = person.get(Employee.ID); // Wrong return class
}
/** How to restore default values? */
@SuppressWarnings("unchecked")
public void defaultValues() {
Employee employee = service.getEmployee();
for (Key key : employee.readKeys()) {
employee.set(key, null);
}
assert employee.getWage() == WAGE.getDefault() : "Check the default value";
}
/** See how to restore a default value for all Numbers only.
* A type of the Key can be checked by the method {@link Key#isTypeOf(java.lang.Class) }.
*/
@SuppressWarnings("unchecked")
public void numericDefaultValues() {
Employee employee = service.getEmployee();
for (Key key : employee.readKeys()) {
if (key.isTypeOf(Number.class)) {
employee.set(key, null);
}
}
assert employee.getWage() == WAGE.getDefault() : "Check the default value";
}
/** How to copy all attributes from a source to a target object? */
public void copyAttributes() throws Exception {
Employee source = service.getEmployee();
Employee target = source.getClass().newInstance();
for (Key<Ujo,?> key : source.readKeys()) {
key.copy(source, target);
}
assert source.getId() == target.getId()
: "The same IDs";
}
/** There is a {@link Validator} class which checks an input data using the method
* {@link Validator#checkValue(java.lang.Object, org.ujorm.Key, org.ujorm.Ujo) checkValue(value ...)}
* to ensure an integration.
* <h3>Features:</h3>
* <ul>
* <li>two Validators can be joined using operator AND/OR to a new composite Validator</li>
* <li>one (composite) Validator can be assigned into an object type of {@link Key}</li>
* <li>the Validator can be assigned to the {@link Key} to check all input data in the <strong>writing time</strong> always</li>
* </ul>
* The {@link Employee#NAME} has got allowed max 7 characters length.
* <br/>The key definition in the {@link Employee} class is:
* <pre>{@code public static final Key<Employee, String> NAME = factory.newKey(length(7));
* }</pre>
*/
public void keyValidator() {
final String correctName = "1234567";
final String wrongName = "12345678";
Employee employee = new Employee();
employee.set(NAME, correctName);
try {
employee.set(NAME, wrongName);
} catch (ValidationException e) {
String expected
= "Text length for Employee.name must be between 0 and 7, "
+ "but the input length is: 8";
assert e.getMessage().equals(expected);
}
assert employee.getName() == correctName;
}
/** Messages from validator can be located using the templates */
public void localizedMessageOfValidator() {
final String wrongName = "12345678";
ValidationException exception = null;
try {
new Employee().set(NAME, wrongName);
} catch (ValidationException e) {
exception = e;
}
String template = "The name can be up to ${MAX} characters long, not ${LENGTH}.";
String expected = "The name can be up to 7 characters long, not 8.";
String result = exception.getError().getMessage(template);
assert expected.equals(result);
}
/** Two related keys can be joined to the new {@link Key} instance by the method {@link Key#add(org.ujorm.Key)}.
* A CompositeKey can be used to reading and writing attributes of related domain objects,
* however the setter with the CompositeKey <create>creates</create> missing domain relations automatically.
*/
public void compositeKey() {
Key<Employee, String> companyNameKey = COMPANY.add(Company.NAME);
Employee employee = new Employee();
String companyName = employee.get(companyNameKey); //!
assert companyName == null;
employee.set(companyNameKey, "Prague"); //!
companyName = employee.get(companyNameKey);
assert employee.getCompany() != null;
assert companyName == "Prague";
}
/** Employee theCriterion example */
public void theCriterion() {
Criterion<Employee> validator = Employee.WAGE.whereGt(100.0);
try {
validator.validate(service.getEmployee()
, "Minimal WAGE is: %s units"
, validator.getRightNode());
assert false : Employee.WAGE + " is not valid";
} catch (IllegalArgumentException e) {
assert e.getMessage() != null;
}
}
/** Filter all employees, where a city name of a company equals employee name. */
public void criterionAsFilter() {
List<Employee> employees = COMPANY.add(CITY)
.whereEq("Prague")
.evaluate(service.getEmployees());
for (Employee employee : employees) {
System.out.println(employee.getCompany().getCity() + " " + employee.getName());
}
assert employees.size() == 4;
}
/** Filter all employees, where a <strong>company.city</strong> equals to the <strong>employee's name</strong>.
* Note: the result if Employee's name is 'Prague'. */
public void criterionAsFilterWithKey() {
List<Employee> employees = COMPANY.add(CITY)
.whereEq(Employee.NAME) // The employee NAME can be a correct value of the Criterion
.evaluate(service.getEmployees());
for (Employee employee : employees) {
System.out.println(employee.getCompany().getCity() + " " + employee.getName());
}
assert employees.size() == 1 : "Check the result count";
assert employees.get(0).getName().equals("Prague") : "Check the result value";
}
/** How to sort the List? */
public void sortEmployeeList() {
List<Employee> employees = UjoComparator.of
( COMPANY.add(CITY)
, NAME.descending()
).sort(service.getEmployees());
for (Employee employee : employees) {
System.out.println(employee.getCompany().getCity() + " " + employee.getName());
}
assert employees.size() == 4 : "Check the result count";
}
/** Each direct Key has an unique instance in a classloader,
* similar like an item of the {@link Enum} type.
* For the serialization use a {@link KeyRing} envelope.
*/
public void keySerialization() {
final KeyRing<Employee> keyRing1, keyRing2;
keyRing1 = KeyRing.of(Employee.ID, Employee.COMPANY.add(Company.NAME));
keyRing2 = service.serialize(keyRing1);
assert keyRing1 != keyRing2 : "Different instances";
assert keyRing1.get(0) == keyRing2.get(0) : "The same direct keys";
assert keyRing1.get(1).equals(keyRing2.get(1)) : "The equal composite keys";
assert new Employee().readKeys() instanceof KeyRing : "readKeys() returns the KeyRing";
}
/** Import the CSV file using a Composite Keys from the file content:
* <pre>{@code id;name;companyId
* 1;Pavel;10
* 2;Petr;30
* 3;Kamil;50}</pre>
*/
public void importCSV() throws Exception {
Scanner scanner = new Scanner(getClass().getResourceAsStream("employee.csv"), "utf-8");
UjoManagerCSV<Employee> manager = UjoManagerCSV.of
( Employee.ID
, Employee.NAME
, Employee.COMPANY.add(Company.ID)
);
List<Employee> employes = manager.loadCSV(scanner, this);
assert employes.size() == 3;
assert employes.get(0).getId() .equals(1L);
assert employes.get(0).getName().equals("Pavel");
assert employes.get(0).getCompany().getId().equals(10L);
}
/** Samples of WeakKey using are located in a separated class. */
public void howToUseWeakKey() {
new SampleWeakKeyService().testWeakKeys2List();
new SampleWeakKeyService().testWeakKeys2Map();
new SampleWeakKeyService().testWeakKeyAttributes();
}
// ===========- HELPER CODE ===========-
/** Helper methods */
private final Service service = new Service();
/** Helper methods */
private static class Service {
/** Find an Employee somewhere */
private Employee getEmployee() {
return createEmployee(10L, "Pavel", 50.00, getCompany());
}
/** Create the List of Persons */
private Employee createEmployee(Long id, String name, Double wage, Company company) {
Employee person = new Employee();
person.set(ID, id);
person.set(NAME, name);
person.set(WAGE, wage);
person.set(COMPANY, company);
return person;
}
/** Find an Company somewhere */
private Company getCompany() {
return createCompany(20L, "My Company", "Prague");
}
/** Find an Company somewhere */
private Company createCompany(Long id, String name, String city) {
Company result = new Company();
result.set(Company.ID, id);
result.set(Company.NAME, name);
result.set(Company.CITY, city);
result.set(Company.CREATED, new Date());
return result;
}
/** Create the List of Persons */
private List<Employee> getEmployees() {
final List<Employee> result = new ArrayList<Employee>();
result.add(createEmployee(10L, "Pavel", 50.00, getCompany()));
result.add(createEmployee(20L, "Petr", 80.00, getCompany()));
result.add(createEmployee(30L, "Kamil", 20.00, getCompany()));
result.add(createEmployee(40L, "Prague", 00.00, getCompany()));
return result;
}
/** Object serialization */
@SuppressWarnings("unchecked")
private <T extends Serializable> T serialize(T object) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream(1000);
ObjectOutputStream encoder = new ObjectOutputStream(os);
encoder.writeObject(object);
encoder.close();
InputStream is = new ByteArrayInputStream(os.toByteArray());
ObjectInputStream decoder = new ObjectInputStream(is);
Object result = (Serializable) decoder.readObject();
decoder.close();
return (T) result;
} catch (Exception e) {
throw new IllegalStateException("Serializaton error", e);
}
}
}
}