Refactoring the classes ExpenseSheet and ExpenseSheetLine

FEA: ItEr76S22ExpenseTrackingSystem
This commit is contained in:
Susana Montes Pedreira 2012-04-24 18:49:03 +01:00
parent 398242bd35
commit 24be482a78
4 changed files with 78 additions and 71 deletions

View file

@ -33,7 +33,6 @@ import org.hibernate.validator.Valid;
import org.libreplan.business.common.IntegrationEntity;
import org.libreplan.business.common.Registry;
import org.libreplan.business.common.entities.EntitySequence;
import org.libreplan.business.common.exceptions.ValidationException;
import org.libreplan.business.expensesheet.daos.IExpenseSheetDAO;
/**
@ -52,13 +51,14 @@ public class ExpenseSheet extends IntegrationEntity {
private String description;
@Valid
@NotEmpty(message = "the expense sheet must have least a expense sheet line.")
@NotNull(message = "the expense sheet must have least a expense sheet line.")
private SortedSet<ExpenseSheetLine> expenseSheetLines = new TreeSet<ExpenseSheetLine>(
new ExpenseSheetLineComparator());
private Integer lastExpenseSheetLineSequenceCode = 0;
/**
* Constructor for Hibernate. Do not use!
*/
protected ExpenseSheet() {
}
@ -69,15 +69,11 @@ public class ExpenseSheet extends IntegrationEntity {
}
public static ExpenseSheet create() {
ExpenseSheet expenseSheet = new ExpenseSheet();
expenseSheet.setNewObject(true);
return expenseSheet;
return create(new ExpenseSheet());
}
public static ExpenseSheet create(Date firstExpense, Date lastExpense, BigDecimal total) {
ExpenseSheet expenseSheet = new ExpenseSheet(firstExpense, lastExpense, total);
expenseSheet.setNewObject(true);
return expenseSheet;
return create(new ExpenseSheet(firstExpense, lastExpense, total));
}
@Override
@ -105,16 +101,14 @@ public class ExpenseSheet extends IntegrationEntity {
this.total = total;
}
@Min(message = "length less than 0", value = 0)
@Min(message = "total must be greater or equal than 0", value = 0)
@NotNull(message = "total not specified")
public BigDecimal getTotal() {
return total;
}
public void setExpenseSheetLines(SortedSet<ExpenseSheetLine> expenseSheetLines) {
this.expenseSheetLines = expenseSheetLines;
}
@NotEmpty(message = "the expense sheet must have least a expense sheet line.")
@NotNull(message = "the expense sheet must have least a expense sheet line.")
public SortedSet<ExpenseSheetLine> getExpenseSheetLines() {
return Collections.unmodifiableSortedSet(expenseSheetLines);
}
@ -141,11 +135,6 @@ public class ExpenseSheet extends IntegrationEntity {
return getFirstRepeatedCode(this.expenseSheetLines) == null;
}
@AssertTrue(message = "The expense sheet line collection cannot be empty or null.")
public boolean checkConstraintNotEmptyExpenseSheetLines() {
return ((getExpenseSheetLines() != null) && (!getExpenseSheetLines().isEmpty()));
}
public void generateExpenseSheetLineCodes(int numberOfDigits) {
for (ExpenseSheetLine line : this.getExpenseSheetLines()) {
if ((line.getCode() == null) || (line.getCode().isEmpty())
@ -183,7 +172,7 @@ public class ExpenseSheet extends IntegrationEntity {
}
}
public void updateTotal() throws ValidationException {
public void updateTotal() {
this.setTotal(new BigDecimal(0));
for (ExpenseSheetLine line : this.expenseSheetLines) {
if (line.getValue() != null) {

View file

@ -49,11 +49,14 @@ public class ExpenseSheetLine extends IntegrationEntity {
private ExpenseSheet expenseSheet;
/**
* Constructor for Hibernate. Do not use!
*/
protected ExpenseSheetLine() {
}
protected ExpenseSheetLine(BigDecimal value, String concept, Date date,
private ExpenseSheetLine(BigDecimal value, String concept, Date date,
OrderElement orderElement) {
this.orderElement = orderElement;
this.concept = concept;
@ -63,9 +66,7 @@ public class ExpenseSheetLine extends IntegrationEntity {
public static ExpenseSheetLine create(BigDecimal value, String concept, Date date,
OrderElement orderElement) {
ExpenseSheetLine expenseSheetLine = new ExpenseSheetLine(value, concept, date, orderElement);
expenseSheetLine.setNewObject(true);
return create(expenseSheetLine);
return create(new ExpenseSheetLine(value, concept, date, orderElement));
}
public void setValue(BigDecimal value) {
@ -88,8 +89,8 @@ public class ExpenseSheetLine extends IntegrationEntity {
return true;
}
@Min(message = "length less than 0", value = 0)
@NotNull(message = "total not specified")
@Min(message = "value must be greater or equal than 0", value = 0)
@NotNull(message = "value not specified")
public BigDecimal getValue() {
return value;
}

View file

@ -202,9 +202,18 @@
<column name="resource_id" type="BIGINT"/>
<column name="expense_sheet_id" type="BIGINT"/>
</createTable>
<addForeignKeyConstraint constraintName="expense_sheet_line_order_element_fkey"
baseTableName="expense_sheet_line" baseColumnNames="order_element_id"
referencedTableName="order_element" referencedColumnNames="id" />
<addForeignKeyConstraint constraintName="expense_sheet_line_resource_fkey"
baseTableName="expense_sheet_line" baseColumnNames="resource_id"
referencedTableName="resource" referencedColumnNames="id" />
<addForeignKeyConstraint constraintName="expense_sheet_line_expense_sheet_fkey"
baseTableName="expense_sheet_line" baseColumnNames="expense_sheet_id"
referencedTableName="expense_sheet" referencedColumnNames="id" />
</changeSet>
<changeSet id="add-new-column-generate-code-for-expense-sheets" author="mrego">
<changeSet id="add-new-column-generate-code-for-expense-sheets" author="smontes">
<comment>Add new column to generate the code for expense sheet in configuration table</comment>
<addColumn tableName="configuration">
<column name="generate_code_for_expense_sheets" type="BOOLEAN"/>

View file

@ -1,63 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.libreplan.business.expensesheet.entities" default-access="field">
<hibernate-mapping package="org.libreplan.business.expensesheet.entities"
default-access="field">
<!-- ExpenseSheetLine -->
<class name="ExpenseSheetLine" table="expense_sheet_line">
<cache usage="nonstrict-read-write"/>
<id name="id" type="long" access="property">
<generator class="hilo">
<param name="max_lo">100</param>
</generator>
</id>
<version name="version" access="property" type="long" />
<property name="code" access="property" not-null="true" unique="true"/>
<!-- ExpenseSheetLine -->
<class name="ExpenseSheetLine" table="expense_sheet_line">
<cache usage="nonstrict-read-write" />
<id name="id" type="long" access="property">
<generator class="hilo">
<param name="max_lo">100</param>
</generator>
</id>
<version name="version" access="property" type="long" />
<property name="code" access="property" not-null="true"
unique="true" />
<property name="value" access="field" column="value" />
<property name="concept" access="field" column="concept" />
<property name="date" access="field" column="date" />
<property name="value" access="field" column="value" />
<property name="concept" access="field" column="concept" />
<property name="date" access="field" column="date" />
<many-to-one name="orderElement" column="order_element_id" class="org.libreplan.business.orders.entities.OrderElement"/>
<many-to-one name="orderElement" column="order_element_id"
class="org.libreplan.business.orders.entities.OrderElement" />
<many-to-one name="resource" column="resource_id" class="org.libreplan.business.resources.entities.Resource"/>
<many-to-one name="resource" column="resource_id"
class="org.libreplan.business.resources.entities.Resource" />
<many-to-one name="expenseSheet" class="ExpenseSheet" column="expense_sheet_id" index="idx_expense_sheet_line"/>
<many-to-one name="expenseSheet" class="ExpenseSheet"
column="expense_sheet_id" index="idx_expense_sheet_line" />
</class>
</class>
<!-- ExpenseSheet -->
<class name="ExpenseSheet" table="expense_sheet">
<cache usage="nonstrict-read-write"/>
<id name="id" type="long" access="property">
<generator class="hilo">
<param name="max_lo">100</param>
</generator>
</id>
<version name="version" access="property" type="long" />
<!-- ExpenseSheet -->
<class name="ExpenseSheet" table="expense_sheet">
<cache usage="nonstrict-read-write" />
<id name="id" type="long" access="property">
<generator class="hilo">
<param name="max_lo">100</param>
</generator>
</id>
<version name="version" access="property" type="long" />
<property name="code" access="property" not-null="true" unique="true"/>
<property name="code" access="property" not-null="true"
unique="true" />
<property name="codeAutogenerated" not-null="true"
column="code_autogenerated" />
<property name="codeAutogenerated" not-null="true"
column="code_autogenerated" />
<property name="firstExpense" access="field" column="first_expense"/>
<property name="firstExpense" access="field" column="first_expense" />
<property name="lastExpense" access="field" column="last_expense"/>
<property name="lastExpense" access="field" column="last_expense" />
<property name="total" access="field" column="total"/>
<property name="total" access="field" column="total" />
<property name="description" access="field" column="description"/>
<property name="description" access="field" column="description" />
<property name="lastExpenseSheetLineSequenceCode" access="field"
column="last_expense_sheet_line_sequence_code" />
<property name="lastExpenseSheetLineSequenceCode" access="field"
column="last_expense_sheet_line_sequence_code" />
<!-- Indexed the other side -->
<set name="expenseSheetLines" inverse="true" cascade="all,delete-orphan" access="field"
sort="org.libreplan.business.expensesheet.entities.ExpenseSheetLineComparator">
<key column="expense_sheet_id" />
<one-to-many class="org.libreplan.business.expensesheet.entities.ExpenseSheetLine" />
</set>
<!-- Indexed the other side -->
<set name="expenseSheetLines" inverse="true" cascade="all,delete-orphan"
access="field"
sort="org.libreplan.business.expensesheet.entities.ExpenseSheetLineComparator">
<key column="expense_sheet_id" />
<one-to-many
class="org.libreplan.business.expensesheet.entities.ExpenseSheetLine" />
</set>
</class>
</class>
</hibernate-mapping>
</hibernate-mapping>