This is an automated email from the ASF dual-hosted git repository.
doebele pushed a commit to branch version3
in repository https://gitbox.apache.org/repos/asf/empire-db.git
The following commit(s) were added to refs/heads/version3 by this push:
new 485fd17 EMPIREDB-362 SampleApp improvment
485fd17 is described below
commit 485fd17bba1b25b76b7afed4bd6240511efed523
Author: Rainer Döbele <[email protected]>
AuthorDate: Sat Jan 29 21:19:14 2022 +0100
EMPIREDB-362 SampleApp improvment
---
.../org/apache/empire/samples/db/SampleApp.java | 23 ++-
.../org/apache/empire/samples/db/SampleDB.java | 6 -
.../apache/empire/samples/db/beans/Employee.java | 186 +++++++++++++++++++++
.../{SampleBean.java => beans/EmployeeQuery.java} | 16 +-
.../java/org/apache/empire/commons/DateUtils.java | 6 +-
5 files changed, 213 insertions(+), 24 deletions(-)
diff --git
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
index 3f2e51a..5f38a91 100644
---
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
+++
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
@@ -45,6 +45,8 @@ import org.apache.empire.dbms.h2.DBMSHandlerH2;
import org.apache.empire.dbms.hsql.DBMSHandlerHSql;
import org.apache.empire.dbms.postgresql.DBMSHandlerPostgreSQL;
import org.apache.empire.samples.db.SampleDB.Gender;
+import org.apache.empire.samples.db.beans.Employee;
+import org.apache.empire.samples.db.beans.EmployeeQuery;
import org.apache.empire.xml.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -133,15 +135,15 @@ public class SampleApp
// SECTION 7: Option 1: Query Records and print tab-separated
log.info("Step 8 Option 1: queryRecords() / Tab-Output");
- queryRecords(QueryType.Reader); // Tab-Output
+ queryExample(QueryType.Reader); // Tab-Output
// SECTION 7: Option 2: Query Records as a list of java beans
log.info("Step 8 Option 2: queryRecords() / Bean-List-Output");
- queryRecords(QueryType.BeanList); // Bean-List-Output
+ queryExample(QueryType.BeanList); // Bean-List-Output
// SECTION 7: Option 3: Query Records as XML
log.info("Step 8 Option 3: queryRecords() / XML-Output");
- queryRecords(QueryType.XmlDocument); // XML-Output
+ queryExample(QueryType.XmlDocument); // XML-Output
// SECTION 8: Use DataList query
queryDataList();
@@ -601,7 +603,7 @@ public class SampleApp
* Please note, that the XML not only contains the data but also the
field metadata.
* </PRE>
*/
- private static void queryRecords(QueryType queryType)
+ private static void queryExample(QueryType queryType)
{
int lastYear = LocalDate.now().getYear()-1;
@@ -631,7 +633,7 @@ public class SampleApp
// DBColumnExpr genderExpr =
cmd.select(EMP.GENDER.decode(EMP.GENDER.getOptions()).as(EMP.GENDER.getName()));
// Select Employee and Department columns
- cmd.select(EMP.ID, EMPLOYEE_FULLNAME);
+ cmd.select(EMP.ID.as("EMPLOYEE_ID"), EMPLOYEE_FULLNAME);
cmd.select(EMP.GENDER, EMP.PHONE_NUMBER, PHONE_EXT_NUMBER);
cmd.select(DEP.NAME.as("DEPARTMENT"));
cmd.select(DEP.BUSINESS_UNIT);
@@ -686,9 +688,9 @@ public class SampleApp
break;
case BeanList:
// Text-Output using a list of Java Beans supplied by the
DBReader
- List<SampleBean> beanList =
reader.getBeanList(SampleBean.class);
+ List<EmployeeQuery> beanList =
reader.getBeanList(EmployeeQuery.class);
// log.info(String.valueOf(beanList.size()) + "
SampleBeans returned from Query.");
- for (SampleBean b : beanList)
+ for (EmployeeQuery b : beanList)
{
System.out.println(b.toString());
}
@@ -711,7 +713,7 @@ public class SampleApp
{
SampleDB.Employees EMP = db.EMPLOYEES;
// Query all males
- BeanResult<SampleBean> result = new
BeanResult<SampleBean>(SampleBean.class, EMP);
+ BeanResult<Employee> result = new
BeanResult<Employee>(Employee.class, EMP);
result.getCommand().where(EMP.GENDER.is(Gender.M));
result.fetch(context);
@@ -820,6 +822,11 @@ public class SampleApp
}
// udpate the record
record.update();
+
+ // convert to bean
+ Employee employee = new Employee();
+ record.setBeanProperties(employee);
+ System.out.println(employee.toString());
}
}
}
diff --git
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleDB.java
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleDB.java
index b1f3e2a..0d8ec5e 100644
---
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleDB.java
+++
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleDB.java
@@ -96,15 +96,12 @@ public class SampleDB extends DBDatabase
public static class Employees extends DBTable
{
public final DBTableColumn ID;
- public final DBTableColumn SALUTATION;
public final DBTableColumn FIRSTNAME;
public final DBTableColumn LASTNAME;
public final DBTableColumn DATE_OF_BIRTH;
- public final DBTableColumn DATE_WITH_TIME;
public final DBTableColumn DEPARTMENT_ID;
public final DBTableColumn GENDER;
public final DBTableColumn PHONE_NUMBER;
- public final DBTableColumn EMAIL;
public final DBTableColumn SALARY;
public final DBTableColumn RETIRED;
public final DBTableColumn UPDATE_TIMESTAMP;
@@ -115,15 +112,12 @@ public class SampleDB extends DBDatabase
// ID
ID = addColumn("ID", DataType.AUTOINC,
0, true); // Optional Sequence name ("EMPLOYEE_ID_SEQUENCE") for some DBMS
(e.g. Oracle)
- SALUTATION = addColumn("SALUTATION", DataType.VARCHAR,
20, false);
FIRSTNAME = addColumn("FIRSTNAME", DataType.VARCHAR,
40, true);
LASTNAME = addColumn("LASTNAME", DataType.VARCHAR,
40, true);
DATE_OF_BIRTH = addColumn("DATE_OF_BIRTH", DataType.DATE,
0, false);
- DATE_WITH_TIME = addColumn("DATE_WITH_TIME", DataType.DATETIME,
0, false);
DEPARTMENT_ID = addColumn("DEPARTMENT_ID", DataType.INTEGER,
0, true);
GENDER = addColumn("GENDER", DataType.VARCHAR,
1, false, Gender.class);
PHONE_NUMBER = addColumn("PHONE_NUMBER", DataType.VARCHAR,
40, false);
- EMAIL = addColumn("EMAIL", DataType.VARCHAR,
80, false);
SALARY = addColumn("SALARY", DataType.DECIMAL,
10.2, false);
RETIRED = addColumn("RETIRED", DataType.BOOL,
0, true, false);
UPDATE_TIMESTAMP= addColumn("UPDATE_TIMESTAMP",
DataType.TIMESTAMP, 0, true);
diff --git
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/beans/Employee.java
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/beans/Employee.java
new file mode 100644
index 0000000..9e569ad
--- /dev/null
+++
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/beans/Employee.java
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.empire.samples.db.beans;
+
+import java.math.BigDecimal;
+import java.util.Date;
+import java.util.Locale;
+
+import org.apache.empire.commons.DateUtils;
+
+/**
+ * This is an employee entity bean
+ * @author doebele
+ *
+ */
+public class Employee
+{
+ private long id; // "ID"
+ private String firstname; // "FIRSTNAME"
+ private String lastname; // "LASTNAME"
+ private Date dateOfBirth; // "DATE_OF_BIRTH"
+ private long departmentId;// "DEPARTMENT_ID"
+ private String gender; // "GENDER"
+ private String phoneNumber; // "PHONE_NUMBER"
+ private BigDecimal salary; // "SALARY"
+ private boolean retired; // "RETIRED"
+
+ /**
+ * Creates a new Employee entity bean
+ * @param id
+ * @param firstname
+ * @param lastname
+ * @param dateOfBirth
+ * @param departmentId
+ * @param gender
+ * @param phoneNumber
+ * @param salary
+ * @param retired
+ */
+ public Employee(int id, String firstname, String lastname, Date
dateOfBirth, int departmentId, String gender, String phoneNumber,
+ BigDecimal salary, boolean retired)
+ {
+ this.id = id;
+ this.firstname = firstname;
+ this.lastname = lastname;
+ this.dateOfBirth = dateOfBirth;
+ this.departmentId = departmentId;
+ this.gender = gender;
+ this.phoneNumber = phoneNumber;
+ this.salary = salary;
+ this.retired = retired;
+ }
+
+ public Employee()
+ {
+ // Standard constructor
+ }
+
+ public long getId()
+ {
+ return id;
+ }
+
+ public void setId(long id)
+ {
+ this.id = id;
+ }
+
+ public String getFirstname()
+ {
+ return firstname;
+ }
+
+ public void setFirstname(String firstname)
+ {
+ this.firstname = firstname;
+ }
+
+ public String getLastname()
+ {
+ return lastname;
+ }
+
+ public void setLastname(String lastname)
+ {
+ this.lastname = lastname;
+ }
+
+ public Date getDateOfBirth()
+ {
+ return dateOfBirth;
+ }
+
+ public void setDateOfBirth(Date dateOfBirth)
+ {
+ this.dateOfBirth = dateOfBirth;
+ }
+
+ public long getDepartmentId()
+ {
+ return departmentId;
+ }
+
+ public void setDepartmentId(long departmentId)
+ {
+ this.departmentId = departmentId;
+ }
+
+ public String getGender()
+ {
+ return gender;
+ }
+
+ public void setGender(String gender)
+ {
+ this.gender = gender;
+ }
+
+ public String getPhoneNumber()
+ {
+ return phoneNumber;
+ }
+
+ public void setPhoneNumber(String phoneNumber)
+ {
+ this.phoneNumber = phoneNumber;
+ }
+
+ public BigDecimal getSalary()
+ {
+ return salary;
+ }
+
+ public void setSalary(BigDecimal salary)
+ {
+ this.salary = salary;
+ }
+
+ public boolean isRetired()
+ {
+ return retired;
+ }
+
+ public void setRetired(boolean retired)
+ {
+ this.retired = retired;
+ }
+
+ @Override
+ public String toString()
+ {
+ StringBuffer buf = new StringBuffer();
+ buf.append(id);
+ buf.append("\t");
+ buf.append(firstname);
+ buf.append("\t");
+ buf.append(lastname);
+ buf.append("\t");
+ buf.append(DateUtils.formatDate(dateOfBirth, Locale.US));
+ buf.append("\t");
+ buf.append(gender);
+ buf.append("\t");
+ buf.append(salary);
+ buf.append("\t");
+ buf.append(retired);
+ return buf.toString();
+ }
+
+}
+
diff --git
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleBean.java
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/beans/EmployeeQuery.java
similarity index 92%
rename from
empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleBean.java
rename to
empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/beans/EmployeeQuery.java
index aa5922e..2d2dbac 100644
---
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleBean.java
+++
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/beans/EmployeeQuery.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.empire.samples.db;
+package org.apache.empire.samples.db.beans;
import java.math.BigDecimal;
@@ -24,9 +24,9 @@ import java.math.BigDecimal;
* The SampleBean class is used to demonstrate JavaBean support for
SQL-Queries.
* The SampleBean is used in the SampleApp's queryRecords function.
*/
-public class SampleBean
+public class EmployeeQuery
{
- private int id;
+ private int employeeId;
private String fullName;
private String gender;
private String phoneNumber;
@@ -50,14 +50,14 @@ public class SampleBean
}
*/
- public int getId()
+ public int getEmployeeId()
{
- return id;
+ return employeeId;
}
- public void setId(int id)
+ public void setEmployeeId(int employeeId)
{
- this.id = id;
+ this.employeeId = employeeId;
}
public String getFullName()
@@ -124,7 +124,7 @@ public class SampleBean
public String toString()
{
StringBuffer buf = new StringBuffer();
- buf.append(id);
+ buf.append(employeeId);
buf.append("\t");
buf.append(fullName);
buf.append("\t");
diff --git a/empire-db/src/main/java/org/apache/empire/commons/DateUtils.java
b/empire-db/src/main/java/org/apache/empire/commons/DateUtils.java
index 17c754e..edba917 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/DateUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/DateUtils.java
@@ -201,10 +201,12 @@ public class DateUtils
return (locale==null) ? Locale.getDefault() : locale;
}
- public static String formatDate(Date d, Locale locale)
+ public static String formatDate(Date date, Locale locale)
{
+ if (date==null)
+ return StringUtils.EMPTY;
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM,
getSafeLocale(locale));
- return df.format(d);
+ return df.format(date);
}
public static String formatTime(Date d, Locale locale, boolean withSeconds)