Author: ymartel Date: 2014-08-06 16:17:16 +0200 (Wed, 06 Aug 2014) New Revision: 425 Url: http://forge.chorem.org/projects/chorem/repository/revisions/425 Log: Review Expense Account save to better manage update Use timestamps in Expense Account beans to be sure to have date in easy format on front Modified: trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/financial/ExpenseAccountAction.java trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/bean/financial/ExpenseAccountBean.java trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/bean/financial/ExpenseAccountEntryBean.java trunk/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/expenseAccountView.jsp trunk/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/expenseAccounts.jsp trunk/chorem-webmotion/src/main/webapp/js/financial/expenseAccount.js trunk/chorem-webmotion/src/test/java/org/chorem/webmotion/converters/JsonConverterTest.java Modified: trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/financial/ExpenseAccountAction.java =================================================================== --- trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/financial/ExpenseAccountAction.java 2014-08-06 06:28:39 UTC (rev 424) +++ trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/actions/financial/ExpenseAccountAction.java 2014-08-06 14:17:16 UTC (rev 425) @@ -36,7 +36,6 @@ import org.apache.commons.lang3.StringUtils; import org.chorem.ChoremClient; import org.chorem.entities.Category; -import org.chorem.entities.CategoryImpl; import org.chorem.entities.Employee; import org.chorem.entities.ExpenseAccount; import org.chorem.entities.ExpenseAccountEntry; @@ -136,51 +135,74 @@ public void saveExpenseAccount(ChoremClient client, String expenseAccountId, ExpenseAccountBean expenseAccountBean) { ExpenseAccountImpl expenseAccountWikitty = client.restore(ExpenseAccountImpl.class, expenseAccountId); - String employeeId = expenseAccountBean.getEmployeeId(); - Employee employee = client.restore(Employee.class, employeeId); + if (expenseAccountWikitty == null) { expenseAccountWikitty = new ExpenseAccountImpl(); - expenseAccountWikitty.setEmployee(employee); } - expenseAccountWikitty.setBeginDate(expenseAccountBean.getStartDate()); - expenseAccountWikitty.setEndDate(expenseAccountBean.getEndDate()); + + // manage employee + String employeeId = expenseAccountBean.getEmployeeId(); + Employee employee = client.restore(Employee.class, employeeId); + expenseAccountWikitty.setEmployee(employee); + + expenseAccountWikitty.setBeginDate(new Date(expenseAccountBean.getStartDate())); + expenseAccountWikitty.setEndDate(new Date(expenseAccountBean.getEndDate())); + + Set<String> missingAccountEntryIds = expenseAccountWikitty.getExpenseAccountEntry(); + expenseAccountWikitty.clearExpenseAccountEntry(); + + //manage each Expense Account Entry : try to find them or create new one Set<ExpenseAccountEntryBean> expenseAccountEntryBeans = expenseAccountBean.getExpenseAccountEntries(); if (expenseAccountEntryBeans != null) { List<ExpenseAccountEntry> expenseAccountEntries = new ArrayList<>(expenseAccountEntryBeans.size()); + for (ExpenseAccountEntryBean expenseAccountEntryBean : expenseAccountEntryBeans) { - ExpenseAccountEntry expenseAccountEntry = new ExpenseAccountEntryImpl(); + String expenseAccountEntryBeanId = expenseAccountEntryBean.getId(); + ExpenseAccountEntry expenseAccountEntry = client.restore(ExpenseAccountEntry.class, expenseAccountEntryBeanId); + if (expenseAccountEntry == null) { + expenseAccountEntry = new ExpenseAccountEntryImpl(); + } else { + // remove the Id from the list of missing entries + missingAccountEntryIds.remove(expenseAccountEntryBeanId); + } + expenseAccountEntry.setReference(expenseAccountEntryBean.getJustificationNumber()); expenseAccountEntry.setEmittedDate(new Date(expenseAccountEntryBean.getEmittedDate().getTime())); expenseAccountEntry.setDescription(expenseAccountEntryBean.getDescription()); expenseAccountEntry.setAmount(expenseAccountEntryBean.getAmount()); expenseAccountEntry.setVAT(expenseAccountEntryBean.getVAT()); + String projectId = expenseAccountEntryBean.getProjectId(); if (StringUtils.isNotBlank(projectId)) { Wikitty project = client.restore(projectId); expenseAccountEntry.setTarget(project); } + String categoryId = expenseAccountEntryBean.getCategoryId(); if (StringUtils.isNotBlank(categoryId)) { Category category = client.restore(Category.class, categoryId); expenseAccountEntry.setCategory(category); } + expenseAccountEntries.add(expenseAccountEntry); - expenseAccountWikitty.addExpenseAccountEntry(expenseAccountEntry); - //TODO ymartel 2014/07/16 : think about saving the entries ! } + // Store the entries & put them in Expense Account client.store(expenseAccountEntries); + expenseAccountWikitty.addAllExpenseAccountEntryEntity(expenseAccountEntries); } + // Store the Expense Account client.store(expenseAccountWikitty); + + // Delete old entries + client.delete(missingAccountEntryIds); } public Render findExpenseAccount(ChoremClient client, String expenseAccountId) { ExpenseAccount expenseAccount = client.restore(ExpenseAccount.class, expenseAccountId); - Set<ExpenseAccountEntryBean> expenseAccountEntries; - WikittyQueryResult<CategoryImpl> categoriesResult = client.findAllByExample(new CategoryImpl(), 0, 10); - List<CategoryImpl> categories = categoriesResult.getAll(); + Set<String> expenseAccountEntryIds = expenseAccount.getExpenseAccountEntry(); + Set<ExpenseAccountEntry> expenseAccountEntries = client.restore(ExpenseAccountEntry.class, expenseAccountEntryIds); if (expenseAccount == null) { - expenseAccount = new ExpenseAccountImpl(); } @@ -217,6 +239,7 @@ @Override public ExpenseAccountEntryBean apply(ExpenseAccountEntry expenseAccountEntry) { ExpenseAccountEntryBean expenseAccountEntryBean = new ExpenseAccountEntryBean(); + expenseAccountEntryBean.setId(expenseAccountEntry.getWikittyId()); Category category = expenseAccountEntry.getCategory(false); if (category != null) { Modified: trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/bean/financial/ExpenseAccountBean.java =================================================================== --- trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/bean/financial/ExpenseAccountBean.java 2014-08-06 06:28:39 UTC (rev 424) +++ trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/bean/financial/ExpenseAccountBean.java 2014-08-06 14:17:16 UTC (rev 425) @@ -38,8 +38,8 @@ protected String id; protected String employeeName; protected String employeeId; - protected Date startDate; - protected Date endDate; + protected long startDate; + protected long endDate; protected Set<ExpenseAccountEntryBean> expenseAccountEntries; public String getId() { @@ -66,20 +66,28 @@ this.employeeId = employeeId; } - public Date getStartDate() { + public long getStartDate() { return startDate; } + public void setStartDate(long startDate) { + this.startDate = startDate; + } + public void setStartDate(Date startDate) { - this.startDate = new Date(startDate.getTime()); + this.startDate = startDate.getTime(); } - public Date getEndDate() { + public long getEndDate() { return endDate; } + public void setEndDate(long endDate) { + this.endDate = endDate; + } + public void setEndDate(Date endDate) { - this.endDate = new Date(endDate.getTime()); + this.endDate = endDate.getTime(); } public Set<ExpenseAccountEntryBean> getExpenseAccountEntries() { Modified: trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/bean/financial/ExpenseAccountEntryBean.java =================================================================== --- trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/bean/financial/ExpenseAccountEntryBean.java 2014-08-06 06:28:39 UTC (rev 424) +++ trunk/chorem-webmotion/src/main/java/org/chorem/webmotion/bean/financial/ExpenseAccountEntryBean.java 2014-08-06 14:17:16 UTC (rev 425) @@ -31,6 +31,7 @@ */ public class ExpenseAccountEntryBean implements Serializable { + protected String id; protected String categoryName; protected String categoryId; protected Date emittedDate; @@ -43,6 +44,14 @@ protected String projectId; protected Double total; + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + public String getCategoryName() { return categoryName; } Modified: trunk/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/expenseAccountView.jsp =================================================================== --- trunk/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/expenseAccountView.jsp 2014-08-06 06:28:39 UTC (rev 424) +++ trunk/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/expenseAccountView.jsp 2014-08-06 14:17:16 UTC (rev 425) @@ -45,7 +45,7 @@ <input type="text" ng-model="expenseAccount.employeeName" id="text-expenseAccount-employee" auto-complete data-source="Employee" data-model="expenseAccount" data-base-field="employee" /> <input type="hidden" ng-model="expenseAccount.employeeId" id="hidden-expenseAccount-employee"/> </dd> - <dd ng-if="editPeriod == false">Period from {{expenseAccount.startDate |date}} to {{expenseAccount.endDate |date}}</dd> + <dd ng-if="editPeriod == false">Period from {{expenseAccount.startDate |date : "dd/MM/yyyy"}} to {{expenseAccount.endDate |date : "dd/MM/yyyy"}}</dd> <dd ng-if="editPeriod == true"> <label for="expenseAccountStartDate">Start Date :</label> <input id="expenseAccountBeginDate" class="form-control" type="text" @@ -81,14 +81,14 @@ </tr> <tr ng-repeat="expenseEntry in expenseAccount.expenseAccountEntries"> <td>{{expenseEntry.justificationNumber}}</td> - <td>{{expenseEntry.emittedDate| date:'shortDate'}}</td> + <td>{{expenseEntry.emittedDate| date : "dd/MM/yyyy"}}</td> <td><a href="<c:url value="/wikitty/view/{{expenseEntry.projectId}}"/>">{{expenseEntry.projectName}}</a></td> <td>{{expenseEntry.description}}</td> <td>{{expenseEntry.categoryName}}</td> <td>{{expenseEntry.amount|number:2}}</td> <td>{{expenseEntry.VAT|number:2}}</td> <td>{{expenseEntry.total|number:2}}</td> - <td><span ng-if = "expenseEntry.paymentDate"> {{expenseEntry.paymentDate|date:'shortDate'}}</span></td> + <td><span ng-if = "expenseEntry.paymentDate"> {{expenseEntry.paymentDate|date : "dd/MM/yyyy"}}</span></td> </tr> <tr ng-if="showNewLine == true" ng-include src="'expenseAccountEntryEdit.html'"></tr> </table> Modified: trunk/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/expenseAccounts.jsp =================================================================== --- trunk/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/expenseAccounts.jsp 2014-08-06 06:28:39 UTC (rev 424) +++ trunk/chorem-webmotion/src/main/webapp/WEB-INF/jsp/financial/expenseAccounts.jsp 2014-08-06 14:17:16 UTC (rev 425) @@ -48,10 +48,10 @@ <a href="<c:url value="/wikitty/view/{{expenseAccount.employeeId}}"/>">{{expenseAccount.employeeName}}</a> </td> <td data-title="'Start Date'"> - {{expenseAccount.startDate |date}} + {{expenseAccount.startDate |date : "dd/MM/yyyy"}} </td> <td data-title="'End Date'"> - {{expenseAccount.endDate |date}} + {{expenseAccount.endDate |date : "dd/MM/yyyy"}} </td> <td> <a class="btn btn-success" href="<c:url value="/financial/expenseAccounts/{{expenseAccount.id}}"/>">See it</a> Modified: trunk/chorem-webmotion/src/main/webapp/js/financial/expenseAccount.js =================================================================== --- trunk/chorem-webmotion/src/main/webapp/js/financial/expenseAccount.js 2014-08-06 06:28:39 UTC (rev 424) +++ trunk/chorem-webmotion/src/main/webapp/js/financial/expenseAccount.js 2014-08-06 14:17:16 UTC (rev 425) @@ -6,6 +6,9 @@ $scope.expenseAccount = expenseAccountInit.expenseAccount; $scope.editPeriod = expenseAccountInit && expenseAccountInit.id ? false : true; + console.log("expenseAccount is"); + console.log(expenseAccountInit); + $scope.baseURL = expenseAccountInit.baseURL; $scope.newLine = function() { Modified: trunk/chorem-webmotion/src/test/java/org/chorem/webmotion/converters/JsonConverterTest.java =================================================================== --- trunk/chorem-webmotion/src/test/java/org/chorem/webmotion/converters/JsonConverterTest.java 2014-08-06 06:28:39 UTC (rev 424) +++ trunk/chorem-webmotion/src/test/java/org/chorem/webmotion/converters/JsonConverterTest.java 2014-08-06 14:17:16 UTC (rev 425) @@ -37,8 +37,8 @@ Assert.assertNotNull(expenseAccount.getId()); Assert.assertEquals(employeeId, expenseAccount.getEmployeeId()); Assert.assertEquals(employeeName, expenseAccount.getEmployeeName()); - Assert.assertEquals(startDate.getTime(), expenseAccount.getStartDate().getTime()); - Assert.assertEquals(endDate.getTime(), expenseAccount.getEndDate().getTime()); + Assert.assertEquals(startDate.getTime(), expenseAccount.getStartDate()); + Assert.assertEquals(endDate.getTime(), expenseAccount.getEndDate()); Set<ExpenseAccountEntryBean> expenseAccountEntries = expenseAccount.getExpenseAccountEntries(); Assert.assertNotNull(expenseAccountEntries); Assert.assertEquals(1, expenseAccountEntries.size());