Skip to content

Commit

Permalink
Refactor QueryBuilder - pass method references, not strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
tunguski committed Feb 15, 2015
1 parent bdd1d2a commit 15697de
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public AccountingPrint create(AccountingPrint entity) {

if (entity.getIdCashRegisterReport() != null) {
CashRegisterReport cashRegisterReport =
database.findOne(query(CashRegisterReport.class, eq("id", entity.getIdCashRegisterReport())));
database.findOne(query(CashRegisterReport.class, eq(CashRegisterReport::getId, entity.getIdCashRegisterReport())));
idCashRegister = cashRegisterReport.getCashRegister().getId();

cashRegisterReport.setEndingBalance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pl.matsuo.accounting.model.cashregister.initializer.CashRegisterReportInitializer;
import pl.matsuo.accounting.model.print.AccountingPrint;
import pl.matsuo.accounting.model.print.CashDocument;
import pl.matsuo.core.model.query.AbstractQuery;
import pl.matsuo.core.service.facade.FacadeBuilder;
import pl.matsuo.core.service.report.AbstractReportService;
import pl.matsuo.core.service.report.DataModelBuilder;
Expand All @@ -16,6 +17,7 @@
import java.util.Map;

import static java.math.BigDecimal.*;
import static pl.matsuo.core.model.query.AbstractQuery.*;
import static pl.matsuo.core.model.query.QueryBuilder.*;
import static pl.matsuo.core.util.DateUtil.*;
import static pl.matsuo.core.util.FreemarkerUtils.*;
Expand Down Expand Up @@ -45,7 +47,7 @@ protected void injectModel(DataModelBuilder dataModel, ICashRegisterReportParams
new CashRegisterReportInitializer());

List<CashRegisterReport> cashRegisterReports = database.find(query(CashRegisterReport.class,
eq("cast(createdTime as date)", date(cashRegisterReport.getCreatedTime(), 0, 0))).orderBy("createdTime"));
cond("cast(createdTime as date) = '" + dateFormat.format(date(cashRegisterReport.getCreatedTime(), 0, 0)) + "'")).orderBy("createdTime"));

dataModel.put("cashRegisterReport", cashRegisterReport);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class MediqCashRegisterTestData extends AbstractMediqTestData {

@Override
public void internalExecute() {
OrganizationUnit mediq = database.findOne(query(OrganizationUnit.class, eq("code", MediqTestData.MEDIQ)));
OrganizationUnit onet = database.findOne(query(OrganizationUnit.class, eq("code", PayersTestData.ONET)));
OrganizationUnit mediq = database.findOne(query(OrganizationUnit.class, eq(OrganizationUnit::getCode, MediqTestData.MEDIQ)));
OrganizationUnit onet = database.findOne(query(OrganizationUnit.class, eq(OrganizationUnit::getCode, PayersTestData.ONET)));

CashRegister cashRegister = new CashRegister();
cashRegister.setCode("Kasa 1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void internalExecute() {


protected void savePrints(AccountingPrint ... prints) {
User user = database.findAsAdmin(query(User.class, eq("username", "admin"))).get(0);
User user = database.findAsAdmin(query(User.class, eq(User::getUsername, "admin"))).get(0);

for (AccountingPrint print : prints) {
print.setIdUserCreated(user.getId());
Expand All @@ -78,8 +78,8 @@ public String getExecuteServiceName() {
private void rewriteParties(CashDocument cashDocument, String sellerCode, String buyerCode) {
Object o1 = cashDocument.getBuyer();
Object o2 = cashDocument.getSeller();
rewriteParty(cashDocument.getBuyer(), database.findOne(query(OrganizationUnit.class, eq("code", buyerCode))));
rewriteParty(cashDocument.getSeller(), database.findOne(query(OrganizationUnit.class, eq("code", sellerCode))));
rewriteParty(cashDocument.getBuyer(), database.findOne(query(OrganizationUnit.class, eq(OrganizationUnit::getCode, buyerCode))));
rewriteParty(cashDocument.getSeller(), database.findOne(query(OrganizationUnit.class, eq(OrganizationUnit::getCode, sellerCode))));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public abstract class AbstractAccountingPrintServiceTest extends AbstractDbTest

@Before
public void setUp() {
mediq = database.findOne(query(OrganizationUnit.class, eq("code", MediqTestData.MEDIQ)));
mediq = database.findOne(query(OrganizationUnit.class, eq(OrganizationUnit::getCode, MediqTestData.MEDIQ)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ protected ICashDocumentService findCashDocumentService(String printType) {
@Override
protected AbstractQuery<AccountingPrint> listQuery(ICashDocumentParams params, Condition... additionalConditions) {
return super.listQuery(params,
maybe(params.getStartDate(), ge("issuanceDate", params.getStartDate())),
maybe(params.getEndDate(), le("issuanceDate", params.getEndDate())))
maybe(params.getStartDate(), ge(AccountingPrint::getIssuanceDate, params.getStartDate())),
maybe(params.getEndDate(), le(AccountingPrint::getIssuanceDate, params.getEndDate())))
.orderBy("issuanceDate DESC");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public HttpEntity<CashRegisterReport> create(@RequestBody @Valid CashRegisterRep

@RequestMapping(value = "/reportForCashRegister/{id}", method = GET, consumes = {APPLICATION_OCTET_STREAM_VALUE})
public CashRegisterReport reportForCashRegister(@PathVariable("id") Integer idCashRegister) {
CashRegister cashRegister = database.findOne(query(CashRegister.class, eq("id", idCashRegister)));
CashRegister cashRegister = database.findOne(query(CashRegister.class, eq(CashRegister::getId, idCashRegister)));

CashRegisterReport lastReport = database.findOne(query(CashRegisterReport.class,
eq("cashRegister.id", idCashRegister)).orderBy("createdTime DESC").limit(1));
eq(sub(CashRegisterReport::getCashRegister, CashRegister::getId), idCashRegister)).orderBy("createdTime DESC").limit(1));

List<AccountingPrint> prints = database.find(query(AccountingPrint.class,
eq("idCashRegister", idCashRegister), isNull("idCashRegisterReport"))
eq(AccountingPrint::getIdCashRegister, idCashRegister), isNull(AccountingPrint::getIdCashRegisterReport))
.initializer(new PrintInitializer()));

CashRegisterReport report = new CashRegisterReport();
Expand All @@ -121,8 +121,9 @@ public CashRegisterReport reportForCashRegister(@PathVariable("id") Integer idCa
*/
@RequestMapping(value = "/cashRegisterPrintsSummary/{id}", method = GET, consumes = {APPLICATION_OCTET_STREAM_VALUE})
public BigDecimal cashRegisterPrintsSummary(@PathVariable("id") Integer idCashRegister) {
return sumCashRegisterAmount(database.find(query(AccountingPrint.class, eq("idCashRegister", idCashRegister),
isNull("idCashRegisterReport")).initializer(new PrintInitializer())));
return sumCashRegisterAmount(database.find(query(AccountingPrint.class,
eq(AccountingPrint::getIdCashRegister, idCashRegister),
isNull(AccountingPrint::getIdCashRegisterReport)).initializer(new PrintInitializer())));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ protected AccountingPrint createPrint(Class<? extends InvoiceCommon> clazz) {

InvoiceCommon invoice = createFacade(print);
OrganizationUnit organizationUnit =
database.findOne(query(OrganizationUnit.class, eq("code", MediqTestData.MEDIQ)));
Person person = database.findOne(query(Person.class, eq("pesel", "42041428579")));
database.findOne(query(OrganizationUnit.class, eq(OrganizationUnit::getCode, MediqTestData.MEDIQ)));
Person person = database.findOne(query(Person.class, eq(Person::getPesel, "42041428579")));
invoice.getBuyer().setId(person.getId());
invoice.getSeller().setId(organizationUnit.getId());
print.setIdBucket(organizationUnit.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ private AccountingPrint createPrint() {

Invoice invoice = facadeBuilder.createFacade(print);

OrganizationUnit organizationUnit = database.findOne(query(OrganizationUnit.class, eq("code", MediqTestData.MEDIQ)));
Person person = database.findOne(query(Person.class, eq("pesel", "42041428579")));
OrganizationUnit organizationUnit = database.findOne(query(OrganizationUnit.class,
eq(OrganizationUnit::getCode, MediqTestData.MEDIQ)));
Person person = database.findOne(query(Person.class, eq(Person::getPesel, "42041428579")));
invoice.getBuyer().setId(person.getId());
invoice.getSeller().setId(organizationUnit.getId());
print.setIdBucket(organizationUnit.getId());
Expand Down

0 comments on commit 15697de

Please sign in to comment.