EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
PayForLoansRole.java
1 /*******************************************************************************
2  * Copyright 2012 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package emlab.gen.role.operating;
17 
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.transaction.annotation.Transactional;
20 
21 import agentspring.role.AbstractRole;
22 import agentspring.role.Role;
23 import agentspring.role.RoleComponent;
29 
37 @RoleComponent
38 public class PayForLoansRole extends AbstractRole<EnergyProducer> implements Role<EnergyProducer> {
39 
40 
41  @Autowired
42  Reps reps;
43 
44  @Override
45  @Transactional
46  public void act(EnergyProducer producer) {
47 
48  logger.info("Process accepted bids to cash flow now");
49 
50  // for (Loan loan : loanRepository.findLoansFromAgent(producer)) {
51  for (PowerPlant plant : reps.powerPlantRepository.findPowerPlantsByOwner(producer)) {
52  Loan loan = plant.getLoan();
53  if (loan != null) {
54  logger.info("Found a loan: {}", loan);
55  if (loan.getNumberOfPaymentsDone() < loan.getTotalNumberOfPayments()) {
56 
57  double payment = loan.getAmountPerPayment();
58  reps.nonTransactionalCreateRepository.createCashFlow(producer, loan.getTo(), payment,
59  CashFlow.LOAN, getCurrentTick(), loan.getRegardingPowerPlant());
60 
61  loan.setNumberOfPaymentsDone(loan.getNumberOfPaymentsDone() + 1);
62 
63  logger.info("Paying {} (euro) for loan {}", payment, loan);
64  logger.info("Number of payments done {}, total needed: {}", loan.getNumberOfPaymentsDone(),
65  loan.getTotalNumberOfPayments());
66  }
67  }
68  Loan downpayment = plant.getDownpayment();
69  if (downpayment != null) {
70  logger.info("Found downpayment");
71  if (downpayment.getNumberOfPaymentsDone() < downpayment.getTotalNumberOfPayments()) {
72  double payment = downpayment.getAmountPerPayment();
73  reps.nonTransactionalCreateRepository.createCashFlow(producer, downpayment.getTo(), payment,
74  CashFlow.DOWNPAYMENT, getCurrentTick(),
75  downpayment.getRegardingPowerPlant());
76  downpayment.setNumberOfPaymentsDone(downpayment.getNumberOfPaymentsDone() + 1);
77  logger.info("Paying {} (euro) for downpayment {}", payment, downpayment);
78  logger.info("Number of payments done {}, total needed: {}", downpayment.getNumberOfPaymentsDone(),
79  downpayment.getTotalNumberOfPayments());
80  }
81  }
82  }
83  }
84 }
Iterable< PowerPlant > findPowerPlantsByOwner(@Param("owner") EnergyProducer owner)