http://git-wip-us.apache.org/repos/asf/empire-db/blob/cc86a1fd/empire-db-spring/src/main/java/org/apache/empire/spring/example1/SampleDB.java ---------------------------------------------------------------------- diff --git a/empire-db-spring/src/main/java/org/apache/empire/spring/example1/SampleDB.java b/empire-db-spring/src/main/java/org/apache/empire/spring/example1/SampleDB.java deleted file mode 100644 index c8f3284..0000000 --- a/empire-db-spring/src/main/java/org/apache/empire/spring/example1/SampleDB.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * 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.spring.example1; - -import org.apache.empire.commons.Options; -import org.apache.empire.data.DataType; -import org.apache.empire.data.DataMode; -import org.apache.empire.db.DBColumn; -import org.apache.empire.db.DBDatabase; -import org.apache.empire.db.DBTable; -import org.apache.empire.db.DBTableColumn; - -/** - * <PRE> - * This file contains the definition of the data model in Java. - * The SampleDB data model consists of two tables and a foreign key relation. - * The tables are defined as nested classes here, but you may put the in separate files if you want. - * - * PLEASE NOTE THE NAMING CONVENTION: - * Since all tables, views and columns are declared as "final" constants they are all in upper case. - * We recommend using a prefix of T_ for tables and C_ for columns in order to keep them together - * when listed in your IDE's code completion. - * There is no need to stick to this convention but it makes life just another little bit easier. - * - * You may declare other database tables or views in the same way. - * </PRE> - */ -public class SampleDB extends DBDatabase -{ - private final static long serialVersionUID = 1L; - - /** - * This class represents the definition of the Departments table. - */ - public static class Departments extends DBTable - { - - private final static long serialVersionUID = 1L; - - public final DBTableColumn DEPARTMENT_ID; - public final DBTableColumn NAME; - public final DBTableColumn HEAD; - public final DBTableColumn BUSINESS_UNIT; - public final DBTableColumn UPDATE_TIMESTAMP; - - public Departments(DBDatabase db) - { - super("DEPARTMENTS", db); - // ID - DEPARTMENT_ID = addColumn("DEPARTMENT_ID", DataType.AUTOINC, 0, true, "DEP_ID_SEQUENCE"); - NAME = addColumn("NAME", DataType.TEXT, 80, true); - HEAD = addColumn("HEAD", DataType.TEXT, 80, false); - BUSINESS_UNIT = addColumn("BUSINESS_UNIT", DataType.TEXT, 4, true, "ITTK"); - UPDATE_TIMESTAMP= addColumn("UPDATE_TIMESTAMP", DataType.DATETIME, 0, true); - - // Primary Key - setPrimaryKey(DEPARTMENT_ID); - // Set other Indexes - addIndex("DEARTMENT_NAME_IDX", true, new DBColumn[] { NAME }); - // Set timestamp column for save updates - setTimestampColumn(UPDATE_TIMESTAMP); - } - } - - /** - * This class represents the definition of the Employees table. - */ - public static class Employees extends DBTable - { - private final static long serialVersionUID = 1L; - - public final DBTableColumn EMPLOYEE_ID; - public final DBTableColumn SALUTATION; - public final DBTableColumn FIRSTNAME; - public final DBTableColumn LASTNAME; - public final DBTableColumn DATE_OF_BIRTH; - 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; - - public Employees(DBDatabase db) - { - super("EMPLOYEES", db); - // ID - EMPLOYEE_ID = addColumn("EMPLOYEE_ID", DataType.AUTOINC, 0, true, "EMPLOYEE_ID_SEQUENCE"); - SALUTATION = addColumn("SALUTATION", DataType.TEXT, 20, false); - FIRSTNAME = addColumn("FIRST_NAME", DataType.TEXT, 40, true); - LASTNAME = addColumn("LAST_NAME", DataType.TEXT, 40, true); - DATE_OF_BIRTH = addColumn("DATE_OF_BIRTH", DataType.DATE, 0, false); - DEPARTMENT_ID = addColumn("DEPARTMENT_ID", DataType.INTEGER, 0, true); - GENDER = addColumn("GENDER", DataType.TEXT, 1, false); - PHONE_NUMBER = addColumn("PHONE_NUMBER", DataType.TEXT, 40, false); - EMAIL = addColumn("EMAIL", DataType.TEXT, 80, false); - SALARY = addColumn("SALARY", DataType.DECIMAL, 10.2, false); - RETIRED = addColumn("RETIRED", DataType.BOOL, 0, true, false); - UPDATE_TIMESTAMP= addColumn("UPDATE_TIMESTAMP", DataType.DATETIME, 0, true); - - // Primary Key - setPrimaryKey(EMPLOYEE_ID); - // Set other Indexes - addIndex("EMPLOYEE_NAME_IDX", true, new DBColumn[] { FIRSTNAME, LASTNAME, DATE_OF_BIRTH }); - // Set timestamp column for save updates - setTimestampColumn(UPDATE_TIMESTAMP); - - // Create Options for GENDER column - Options genders = new Options(); - genders.set("M", "Male"); - genders.set("F", "Female"); - GENDER.setOptions(genders); - } - } - - // Declare all Tables and Views here - public final Departments DEPARTMENTS = new Departments(this); - public final Employees EMPLOYEES = new Employees(this); - - /** - * Constructor of the SampleDB data model description - * - * Put all foreign key relations here. - */ - public SampleDB() - { - // Define Foreign-Key Relations - addRelation( EMPLOYEES.DEPARTMENT_ID.referenceOn( DEPARTMENTS.DEPARTMENT_ID )); - } - -}
http://git-wip-us.apache.org/repos/asf/empire-db/blob/cc86a1fd/empire-db-spring/src/main/java/org/apache/empire/spring/example1/SampleSpringApp.java ---------------------------------------------------------------------- diff --git a/empire-db-spring/src/main/java/org/apache/empire/spring/example1/SampleSpringApp.java b/empire-db-spring/src/main/java/org/apache/empire/spring/example1/SampleSpringApp.java deleted file mode 100644 index 3ff0230..0000000 --- a/empire-db-spring/src/main/java/org/apache/empire/spring/example1/SampleSpringApp.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.spring.example1; - -import java.util.logging.Logger; - -import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.GenericApplicationContext; -import org.springframework.core.io.ClassPathResource; - -/** - * - */ -public class SampleSpringApp { - private static final Logger log = Logger.getLogger(SampleSpringApp.class.getName()); - - //creates the application context - //this is usually in some bootstrapping code; so your application will - //just have one at runtime. - static ApplicationContext ctx = getContext(); - - //get the service that is the entry point into the application - //normally this is injected by spring into classes that need it - static EmpireApp appBean = ctx.getBean("empireApp", EmpireApp.class); - - public static void main(String[] args) throws Exception { - - System.out.println("Running Spring Example..."); - - appBean.setupDatabase(); - appBean.clearDatabase(); - - System.out.println("*** Step 6: insertDepartment() & insertEmployee() ***"); - int idDevDep = appBean.insertDepartment("Development", "ITTK"); - int idSalDep = appBean.insertDepartment("Sales", "ITTK"); - - int idPers1 = appBean.insertEmployee("Peter", "Sharp", "M", idDevDep); - int idPers2 = appBean.insertEmployee("Fred", "Bloggs", "M", idDevDep); - int idPers3 = appBean.insertEmployee("Emma", "White", "F", idSalDep); - - System.out.println("*** Step 7: updateEmployee() ***"); - appBean.updateEmployee(idPers1, "+49-7531-457160"); - appBean.updateEmployee(idPers2, "+49-5555-505050"); - appBean.updateEmployee(idPers3, "+49-040-125486"); - - System.out.println("*** Step 8 Option 1: queryRecords() / Tab-Output ***"); - appBean.doQuery(EmpireApp.QueryType.Reader); - - System.out.println("*** Step 8 Option 2: queryRecords() / Bean-List-Output ***"); - appBean.doQuery(EmpireApp.QueryType.BeanList); - - System.out.println("*** Step 8 Option 3: queryRecords() / XML-Output ***"); - appBean.doQuery(EmpireApp.QueryType.XmlDocument); - - - } - - - - static GenericApplicationContext getContext() { - log.info("Creating Spring Application Context ..."); - GenericApplicationContext ctx = new GenericApplicationContext(); - XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(ctx); - reader.loadBeanDefinitions(new ClassPathResource("/example1/applicationContext.xml")); - - ctx.refresh(); - return ctx; - } -} http://git-wip-us.apache.org/repos/asf/empire-db/blob/cc86a1fd/empire-db-spring/src/main/java/org/apache/empire/spring/example2/Department.java ---------------------------------------------------------------------- diff --git a/empire-db-spring/src/main/java/org/apache/empire/spring/example2/Department.java b/empire-db-spring/src/main/java/org/apache/empire/spring/example2/Department.java deleted file mode 100644 index 08d8943..0000000 --- a/empire-db-spring/src/main/java/org/apache/empire/spring/example2/Department.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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.spring.example2; - -public class Department { - - private Integer departmentId; - private String name; - private String businessUnit; - private String head; - - public Integer getDepartmentId() { - return departmentId; - } - - public void setDepartmentId(Integer departmentId) { - this.departmentId = departmentId; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getBusinessUnit() { - return businessUnit; - } - - public void setBusinessUnit(String businessUnit) { - this.businessUnit = businessUnit; - } - - public String getHead() { - return head; - } - - public void setHead(String head) { - this.head = head; - } - - @Override - public String toString() { - StringBuffer buf = new StringBuffer(); - buf.append(departmentId); - buf.append("\t"); - buf.append(name); - buf.append(" "); - buf.append(businessUnit); - buf.append("\t"); - buf.append(head); - return buf.toString(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - - if (obj.getClass().equals(this.getClass())) { - Department other = (Department) obj; - if (other.departmentId == null || this.departmentId == null) { - return false; - } - return this.departmentId.equals(other.departmentId); - } - - return super.equals(obj); - } - - @Override - public int hashCode() { - return 1; - } - - -} http://git-wip-us.apache.org/repos/asf/empire-db/blob/cc86a1fd/empire-db-spring/src/main/java/org/apache/empire/spring/example2/Employee.java ---------------------------------------------------------------------- diff --git a/empire-db-spring/src/main/java/org/apache/empire/spring/example2/Employee.java b/empire-db-spring/src/main/java/org/apache/empire/spring/example2/Employee.java deleted file mode 100644 index 5a07ce9..0000000 --- a/empire-db-spring/src/main/java/org/apache/empire/spring/example2/Employee.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * 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.spring.example2; - -public class Employee { - - public static enum Gender { - - M("Male"), F("Female"); - - private String label; - - private Gender(String label) { - this.label = label; - } - - @Override - public String toString() { - return this.label; - } - } - - private Integer employeeId; - private String firstName; - private String lastName; - private Gender gender; - private String phoneNumber; - - private Department department; - - public Integer getEmployeeId() { - return employeeId; - } - - public void setEmployeeId(Integer employeeId) { - this.employeeId = employeeId; - } - - 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 Gender getGender() { - return gender; - } - - public void setGender(Gender gender) { - this.gender = gender; - } - - public String getPhoneNumber() { - return phoneNumber; - } - - public void setPhoneNumber(String phoneNumber) { - this.phoneNumber = phoneNumber; - } - - public Department getDepartment() { - return department; - } - - public void setDepartment(Department department) { - this.department = department; - } - - @Override - public String toString() { - StringBuffer buf = new StringBuffer(); - buf.append(employeeId); - buf.append("\t"); - buf.append(firstName); - buf.append(" "); - buf.append(lastName); - buf.append("\t"); - buf.append(gender); - - if (department != null){ - buf.append("\t"); - buf.append(department.getName()); - buf.append("\t"); - buf.append(department.getBusinessUnit()); - } - return buf.toString(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - - if (obj.getClass().equals(this.getClass())) { - Employee other = (Employee) obj; - if (other.employeeId == null || this.employeeId == null) { - return false; - } - return this.employeeId.equals(other.employeeId); - } - - return super.equals(obj); - } - - @Override - public int hashCode() { - return 1; - } - -} http://git-wip-us.apache.org/repos/asf/empire-db/blob/cc86a1fd/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeDao.java ---------------------------------------------------------------------- diff --git a/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeDao.java b/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeDao.java deleted file mode 100644 index b2dd39f..0000000 --- a/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeDao.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.spring.example2; - -import java.util.List; - -public interface EmployeeDao { - - public List<Employee> getEmployees(); - - public List<Department> getDepartments(); - - public Integer createEmployee(Employee employee); - - public void updateEmployee(Employee employee); - - public Employee openEmployee(Integer id); - - public Employee findEmployee(String firstName, String lastName); - - public Department openDepartment(Integer id); - - public Department findDepartment(String name); - - public Integer createDepartment(Department department); - - public void updateDepartment(Department department); - - public void renameDepartment(Integer id, String name); - -} http://git-wip-us.apache.org/repos/asf/empire-db/blob/cc86a1fd/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeDaoImpl.java ---------------------------------------------------------------------- diff --git a/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeDaoImpl.java b/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeDaoImpl.java deleted file mode 100644 index 37eb07b..0000000 --- a/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeDaoImpl.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * 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.spring.example2; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.empire.db.DBCommand; -import org.apache.empire.db.DBJoinType; -import org.apache.empire.db.DBRecord; -import org.apache.empire.db.DBRecordData; -import org.apache.empire.spring.DBRecordMapper; -import org.apache.empire.spring.DBRecordWriter; -import org.apache.empire.spring.EmpireDaoSupport; -import org.apache.empire.spring.example1.SampleDB; -import org.apache.empire.spring.example1.SampleDB.Departments; -import org.apache.empire.spring.example1.SampleDB.Employees; -import org.springframework.transaction.annotation.Transactional; - -public class EmployeeDaoImpl extends EmpireDaoSupport implements EmployeeDao { - - private Employees EMPLOYEES; - private Departments DEPARTMENTS; - - @Override - protected void initEmpireDao() { - SampleDB db = getDatabase(); - this.EMPLOYEES = db.EMPLOYEES; - this.DEPARTMENTS = db.DEPARTMENTS; - } - - private DBCommand createEmployeeSelectCommand() { - DBCommand cmd = getDatabase().createCommand(); - cmd.select(EMPLOYEES.getColumns()); - cmd.select(DEPARTMENTS.getColumns()); - - cmd.join(EMPLOYEES.DEPARTMENT_ID, DEPARTMENTS.DEPARTMENT_ID, DBJoinType.INNER); - return cmd; - } - - private DBCommand createDepartmentSelectCommand() { - DBCommand cmd = getDatabase().createCommand(); - cmd.select(DEPARTMENTS.getColumns()); - return cmd; - } - - @Transactional(readOnly = true) - public Employee openEmployee(Integer id) { - DBCommand cmd = createEmployeeSelectCommand(); - cmd.where(EMPLOYEES.EMPLOYEE_ID.is(id)); - return getEmpireTemplate().queryForObject(cmd, new EmployeeMapper()); - } - - @Transactional(readOnly = true) - public Employee findEmployee(String firstName, String lastName) { - DBCommand cmd = createEmployeeSelectCommand(); - cmd.where(EMPLOYEES.FIRSTNAME.is(firstName)); - cmd.where(EMPLOYEES.LASTNAME.is(lastName)); - return getEmpireTemplate().queryForObject(cmd, new EmployeeMapper()); - } - - @Transactional(readOnly = true) - public Department openDepartment(Integer id) { - DBCommand cmd = createDepartmentSelectCommand(); - cmd.where(DEPARTMENTS.DEPARTMENT_ID.is(id)); - return getEmpireTemplate().queryForBean(cmd, Department.class); - } - - @Transactional(readOnly = true) - public Department findDepartment(String name) { - DBCommand cmd = createDepartmentSelectCommand(); - cmd.where(DEPARTMENTS.NAME.is(name)); - return getEmpireTemplate().queryForBean(cmd, Department.class); - } - - @Transactional(readOnly = true) - public List<Department> getDepartments() { - DBCommand cmd = createDepartmentSelectCommand(); - return getEmpireTemplate().queryForBeanList(cmd, Department.class); - } - - - @Transactional - public void renameDepartment(Integer id, String name) { - DBCommand cmd = getDatabase().createCommand(); - cmd.where(DEPARTMENTS.DEPARTMENT_ID.is(id)); - cmd.set(DEPARTMENTS.NAME.to(name)); - getEmpireTemplate().executeUpdate(cmd); - } - - @Transactional(readOnly = true) - public List<Employee> getEmployees() { - DBCommand cmd = createEmployeeSelectCommand(); - return getEmpireTemplate().query(cmd, new EmployeeMapper()); - } - - @Transactional - public Integer createEmployee(Employee employee) { - DBRecord record = new DBRecord(); - record.create(EMPLOYEES); - new EmployeeWriter().write(record, employee); - getEmpireTemplate().updateRecord(record); - return record.getInt(EMPLOYEES.EMPLOYEE_ID); - } - - @Transactional - public void updateEmployee(Employee employee) { - DBRecord record = getEmpireTemplate().openRecord(EMPLOYEES, employee.getEmployeeId()); - new EmployeeWriter().write(record, employee); - getEmpireTemplate().updateRecord(record); - } - - @Transactional - public Integer createDepartment(Department department) { - DBRecord record = new DBRecord(); - record.create(DEPARTMENTS); - new DepartmentWriter().write(record, department); - getEmpireTemplate().updateRecord(record); - return record.getInt(DEPARTMENTS.DEPARTMENT_ID); - } - - @Transactional - public void updateDepartment(Department department) { - DBRecord record = getEmpireTemplate().openRecord(DEPARTMENTS, department.getDepartmentId()); - new DepartmentWriter().write(record, department); - getEmpireTemplate().updateRecord(record); - } - - private class EmployeeMapper implements DBRecordMapper<Employee> { - - DepartmentMapper departmentMapper = new DepartmentMapper(); - - @Override - public Employee mapRecord(DBRecordData record, int rowNum) { - Employee result = new Employee(); - // Auto-copy all properties - //record.setBeanProperties(result); - - result.setEmployeeId(record.getInt(EMPLOYEES.EMPLOYEE_ID)); - result.setFirstName(record.getString(EMPLOYEES.FIRSTNAME)); - result.setLastName(record.getString(EMPLOYEES.LASTNAME)); - result.setGender(Employee.Gender.valueOf(record.getString(EMPLOYEES.GENDER))); - result.setPhoneNumber(record.getString(EMPLOYEES.PHONE_NUMBER)); - - result.setDepartment(departmentMapper.mapRecord(record, rowNum)); - return result; - } - - } - - private class EmployeeWriter implements DBRecordWriter<Employee> { - - @Override - public void write(DBRecord record, Employee entity) { - // Auto-copy all properties - //record.setRecordValues(entity); - - record.setValue(EMPLOYEES.EMPLOYEE_ID, entity.getEmployeeId()); - record.setValue(EMPLOYEES.FIRSTNAME, entity.getFirstName()); - record.setValue(EMPLOYEES.LASTNAME, entity.getLastName()); - record.setValue(EMPLOYEES.GENDER, entity.getGender().name()); - record.setValue(EMPLOYEES.PHONE_NUMBER, entity.getPhoneNumber()); - - record.setValue(EMPLOYEES.DEPARTMENT_ID, entity.getDepartment().getDepartmentId()); - } - - } - - private class DepartmentMapper implements DBRecordMapper<Department> { - - // reader cache, in case of joined resultset the same object is returned - - Map<Integer, Department> cache = new HashMap<Integer, Department>(); - - @Override - public Department mapRecord(DBRecordData record, int rowNum) { - - Integer id = record.getInt(DEPARTMENTS.DEPARTMENT_ID); - - Department department = cache.get(id); - if (department == null) { - department = new Department(); - // Auto-copy all properties - //record.setBeanProperties(department); - - department.setDepartmentId(id); - department.setName(record.getString(DEPARTMENTS.NAME)); - department.setHead(record.getString(DEPARTMENTS.HEAD)); - department.setBusinessUnit(record.getString(DEPARTMENTS.BUSINESS_UNIT)); - - cache.put(id, department); - } - return department; - } - - } - - private class DepartmentWriter implements DBRecordWriter<Department> { - - @Override - public void write(DBRecord record, Department entity) { - // Auto-copy all properties - //record.setRecordValues(entity); - - record.setValue(DEPARTMENTS.DEPARTMENT_ID, entity.getDepartmentId()); - record.setValue(DEPARTMENTS.NAME, entity.getName()); - record.setValue(DEPARTMENTS.HEAD, entity.getHead()); - record.setValue(DEPARTMENTS.BUSINESS_UNIT, entity.getBusinessUnit()); - - } - - } - -} http://git-wip-us.apache.org/repos/asf/empire-db/blob/cc86a1fd/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeSpringApp.java ---------------------------------------------------------------------- diff --git a/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeSpringApp.java b/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeSpringApp.java deleted file mode 100644 index 3b641a7..0000000 --- a/empire-db-spring/src/main/java/org/apache/empire/spring/example2/EmployeeSpringApp.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * 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.spring.example2; - -import java.sql.Connection; -import java.sql.SQLException; -import java.util.Iterator; -import java.util.List; -import java.util.logging.Logger; - -import org.apache.empire.db.DBColumnExpr; -import org.apache.empire.db.DBCommand; -import org.apache.empire.db.DBDatabase; -import org.apache.empire.db.DBDatabaseDriver; -import org.apache.empire.db.DBSQLScript; -import org.apache.empire.db.DBTable; -import org.apache.empire.spring.EmpireTemplate; -import org.apache.empire.spring.example1.SampleDB; -import org.apache.empire.spring.example2.Employee.Gender; -import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.GenericApplicationContext; -import org.springframework.core.io.ClassPathResource; -import org.springframework.dao.DataAccessException; -import org.springframework.jdbc.core.ConnectionCallback; - -/** - * - */ -public class EmployeeSpringApp { - private static final Logger log = Logger.getLogger(EmployeeSpringApp.class.getName()); - - //creates the application context - //this is usually in some bootstrapping code; so your application will - //just have one at runtime. - static ApplicationContext ctx = getContext(); - - //get the service that is the entry point into the application - //normally this is injected by spring into classes that need it - static EmployeeDao employeeDao = ctx.getBean("employeeDao", EmployeeDao.class); - static SampleDB sampleDb = ctx.getBean("sampleDb", SampleDB.class); - - public static void main(String[] args) throws Exception { - - System.out.println("Running Spring Example..."); - - setupDatabase(); - clearDatabase(); - - Department depDevelopment, depSales; - - System.out.println("*** Create Departments ***"); - - { - depDevelopment = new Department(); - depDevelopment.setName("Development"); - depDevelopment.setBusinessUnit("ITTK"); - Integer id = employeeDao.createDepartment(depDevelopment); - depDevelopment = employeeDao.openDepartment(id); - } - { - depSales = new Department(); - depSales.setName("Sales"); - depSales.setBusinessUnit("ITTK"); - Integer id = employeeDao.createDepartment(depSales); - depSales = employeeDao.openDepartment(id); - } - - System.out.println("*** Create Employees ***"); - - Employee peter = new Employee(); - peter.setFirstName("Peter"); - peter.setLastName("Sharp"); - peter.setGender(Gender.M); - peter.setDepartment(depDevelopment); - - Integer peterId = employeeDao.createEmployee(peter); - peter = employeeDao.openEmployee(peterId); - - - - Employee fred = new Employee(); - fred.setFirstName("Fred"); - fred.setLastName("Bloggs"); - fred.setGender(Gender.M); - fred.setDepartment(depDevelopment); - - Integer fredId = employeeDao.createEmployee(fred); - fred = employeeDao.openEmployee(fredId); - - - Employee emma = new Employee(); - emma.setFirstName("Emma"); - emma.setLastName("White"); - emma.setGender(Gender.F); - emma.setDepartment(depSales); - - Integer emmaId = employeeDao.createEmployee(emma); - emma = employeeDao.openEmployee(emmaId); - - - System.out.println("*** updateEmployees ***"); - - peter.setPhoneNumber("+49-7531-457160"); - employeeDao.updateEmployee(peter); - - fred.setPhoneNumber("+49-5555-505050"); - employeeDao.updateEmployee(fred); - - emma.setPhoneNumber("+49-040-125486"); - employeeDao.updateEmployee(emma); - - System.out.println("*** List employees ***"); - - List<Employee> employees = employeeDao.getEmployees(); - for (Iterator<Employee> iterator = employees.iterator(); iterator.hasNext();) { - Employee employee = iterator.next(); - System.out.println(employee); - } - - System.out.println("*** List departments ***"); - - List<Department> departments = employeeDao.getDepartments(); - for (Iterator<Department> iterator = departments.iterator(); iterator.hasNext();) { - Department department = iterator.next(); - System.out.println(department); - } - - } - - private static void clearDatabase() { - // Delete all Employees (no constraints) - - EmpireTemplate empireTemplate = ctx.getBean("empireTemplate", EmpireTemplate.class); - empireTemplate.executeDelete(sampleDb.EMPLOYEES, sampleDb.createCommand()); - empireTemplate.executeDelete(sampleDb.DEPARTMENTS, sampleDb.createCommand()); - - } - - - - - public static void setupDatabase() { - if (!databaseExists()) { - createDatabase(); - } - } - - - public static boolean databaseExists() { - try { - DBDatabase db = sampleDb; - if (db.getTables() == null || db.getTables().isEmpty()) { - throw new AssertionError( - "There are no tables in this database!"); - } - DBCommand cmd = db.createCommand(); - if (cmd == null) { - throw new AssertionError("The DBCommand object is null."); - } - DBTable t = db.getTables().get(0); - DBColumnExpr COUNT = t.count(); - - cmd.select(COUNT); - - EmpireTemplate empireTemplate = ctx.getBean("empireTemplate", EmpireTemplate.class); - return (empireTemplate.queryForInteger(cmd, COUNT, -1) >= 0); - } catch (Exception e) { - return false; - } - } - - - private static void createDatabase() { - - // create DLL for Database Definition - final DBSQLScript script = new DBSQLScript(); - final DBDatabaseDriver driver = sampleDb.getDriver(); - sampleDb.getCreateDDLScript(driver, script); - - // Show DLL Statement - System.out.println(script.toString()); - // Execute Script - EmpireTemplate empireTemplate = ctx.getBean("empireTemplate", EmpireTemplate.class); - empireTemplate.execute(new ConnectionCallback<Object>() { - - @Override - public Object doInConnection(Connection con) throws SQLException, - DataAccessException { - script.executeAll(driver, con, false); - return null; - } - }); - - } - - - - static GenericApplicationContext getContext() { - log.info("Creating Spring Application Context ..."); - GenericApplicationContext ctx = new GenericApplicationContext(); - XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(ctx); - reader.loadBeanDefinitions(new ClassPathResource("/example2/applicationContext-employee.xml")); - - ctx.refresh(); - return ctx; - } - - - -} http://git-wip-us.apache.org/repos/asf/empire-db/blob/cc86a1fd/empire-db-spring/src/main/resources/example1/applicationContext.xml ---------------------------------------------------------------------- diff --git a/empire-db-spring/src/main/resources/example1/applicationContext.xml b/empire-db-spring/src/main/resources/example1/applicationContext.xml deleted file mode 100644 index 7987d5a..0000000 --- a/empire-db-spring/src/main/resources/example1/applicationContext.xml +++ /dev/null @@ -1,92 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - 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. ---> -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:tx="http://www.springframework.org/schema/tx" - xmlns:aop="http://www.springframework.org/schema/aop" - xsi:schemaLocation=" - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> - - - <!-- one option is to use a class from spring that will read the properties file - and replaces the ${...} placeholders with the appropriate values --> - <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> - <property name="locations"> - <value>classpath:/settings.properties</value> - </property> - </bean> - - <!-- Data Source / DB Settings --> - <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> - <property name="driverClassName" value="${jdbc.driverClass}"/> - <property name="url" value="${jdbc.url}"/> - <property name="username" value="${jdbc.username}"/> - <property name="password" value="${jdbc.password}"/> - </bean> - - <!-- Transaction manager for a single JDBC DataSource (alternative to JTA) --> - <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> - <constructor-arg ref="dataSource"/> - </bean> - <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> - <property name="transactionManager" ref="transactionManager"/> - </bean> - - <!-- @Transactional --> - <tx:annotation-driven transaction-manager="transactionManager"/> - - <!-- Empire-DB classes --> - - <!-- application dao/services --> - <bean id="empireApp" class="org.apache.empire.spring.example1.EmpireAppImpl" parent="empireDao"/> - - - <bean id="empireDb" class="org.apache.empire.spring.DBDatabaseFactoryBean"> - <property name="driverClass" value="${empire.driver}"/> - <property name="schema" value="${empire.schemaname}"/> - <property name="databaseClass" value="org.apache.empire.spring.example1.SampleDB"/> - </bean> - - <bean id="empireDao" abstract="true"> - <property name="database" ref="empireDb"/> - <property name="empireTemplate" ref="empireTemplate"/> - </bean> - - - <bean id="empireTemplate" class="org.apache.empire.spring.EmpireTemplate"> - <property name="dataSource" ref="dataSource"/> - </bean> - - - - - - - - - - - - - - -</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/empire-db/blob/cc86a1fd/empire-db-spring/src/main/resources/example2/applicationContext-employee.xml ---------------------------------------------------------------------- diff --git a/empire-db-spring/src/main/resources/example2/applicationContext-employee.xml b/empire-db-spring/src/main/resources/example2/applicationContext-employee.xml deleted file mode 100644 index 08ce097..0000000 --- a/empire-db-spring/src/main/resources/example2/applicationContext-employee.xml +++ /dev/null @@ -1,85 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - 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. ---> -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:tx="http://www.springframework.org/schema/tx" - xmlns:aop="http://www.springframework.org/schema/aop" - xsi:schemaLocation=" - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> - - - <!-- one option is to use a class from spring that will read the properties file - and replaces the ${...} placeholders with the appropriate values --> - <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> - <property name="locations"> - <value>classpath:/settings.properties</value> - </property> - </bean> - - <!-- Data Source / DB Settings --> - <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> - <property name="driverClassName" value="${jdbc.driverClass}"/> - <property name="url" value="${jdbc.url}"/> - <property name="username" value="${jdbc.username}"/> - <property name="password" value="${jdbc.password}"/> - </bean> - - <!-- Transaction manager for a single JDBC DataSource (alternative to JTA) --> - <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> - <constructor-arg ref="dataSource"/> - </bean> - <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> - <property name="transactionManager" ref="transactionManager"/> - </bean> - - <!-- @Transactional --> - <tx:annotation-driven transaction-manager="transactionManager"/> - - <!-- Empire-DB classes --> - - <!-- application dao/services --> - <bean id="employeeDao" class="org.apache.empire.spring.example2.EmployeeDaoImpl" parent="empireDao"/> - - - <bean id="sampleDb" class="org.apache.empire.spring.DBDatabaseFactoryBean"> - <property name="driverClass" value="${empire.driver}"/> - <property name="schema" value="${empire.schemaname}"/> - <property name="databaseClass" value="org.apache.empire.spring.example1.SampleDB"/> - </bean> - - <bean id="empireDao" abstract="true"> - <property name="database" ref="sampleDb"/> - <property name="empireTemplate" ref="empireTemplate"/> - </bean> - - - <bean id="empireTemplate" class="org.apache.empire.spring.EmpireTemplate"> - <property name="dataSource" ref="dataSource"/> - </bean> - - - - - - - -</beans> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/empire-db/blob/cc86a1fd/empire-db-spring/src/main/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/empire-db-spring/src/main/resources/log4j.properties b/empire-db-spring/src/main/resources/log4j.properties deleted file mode 100644 index 031ae65..0000000 --- a/empire-db-spring/src/main/resources/log4j.properties +++ /dev/null @@ -1,21 +0,0 @@ -# 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. - -log4j.rootCategory=FATAL, console -log4j.appender.console=org.apache.log4j.ConsoleAppender -log4j.appender.console.layout=org.apache.log4j.PatternLayout -log4j.appender.console.layout.conversionPattern = %d{ISO8601} %-5p [%c] - %m%n http://git-wip-us.apache.org/repos/asf/empire-db/blob/cc86a1fd/empire-db-spring/src/main/resources/settings.properties ---------------------------------------------------------------------- diff --git a/empire-db-spring/src/main/resources/settings.properties b/empire-db-spring/src/main/resources/settings.properties deleted file mode 100644 index e5ba794..0000000 --- a/empire-db-spring/src/main/resources/settings.properties +++ /dev/null @@ -1,44 +0,0 @@ -# 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. - -## HSQLDB settings (tested) -jdbc.url=jdbc:hsqldb:file:target/hsqldb/sample;shutdown=true -jdbc.driverClass=org.hsqldb.jdbcDriver -jdbc.username=sa -jdbc.password= - -empire.driver= org.apache.empire.db.hsql.DBDatabaseDriverHSql -empire.schemaname= - - -## Derby settings (tested) -#jdbc.url=jdbc:derby:target/dbsample1;create=true -#jdbc.driverClass=org.apache.derby.jdbc.EmbeddedDriver -#jdbc.username= -#jdbc.password= -# -#empire.driver=org.apache.empire.db.derby.DBDatabaseDriverDerby -#empire.schemaname=DBSAMPLE1 - -## MySQL settings (tested) -#jdbc.url=jdbc:mysql://localhost -#jdbc.driverClass=com.mysql.jdbc.Driver -#jdbc.username=test -#jdbc.password=test -# -#empire.driver=org.apache.empire.db.mysql.DBDatabaseDriverMySQL -#empire.schemaname=dbsample1
