View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.payment.beans;
18  
19  import java.io.Serializable;
20  import java.math.BigDecimal;
21  import java.util.Date;
22  import java.util.logging.Logger;
23  
24  import javax.enterprise.context.SessionScoped;
25  import javax.enterprise.event.Event;
26  import javax.inject.Inject;
27  import javax.inject.Named;
28  
29  import org.jboss.as.quickstarts.payment.events.PaymentEvent;
30  import org.jboss.as.quickstarts.payment.events.PaymentTypeEnum;
31  import org.jboss.as.quickstarts.payment.qualifiers.Credit;
32  import org.jboss.as.quickstarts.payment.qualifiers.Debit;
33  
34  @Named
35  @SessionScoped
36  public class PaymentBean implements Serializable {
37  
38      private static final long serialVersionUID = 1L;
39  
40      @Inject
41      private Logger log;
42  
43      // Events producers
44      @Inject
45      @Credit
46      Event<PaymentEvent> creditEventProducer;
47  
48      @Inject
49      @Debit
50      Event<PaymentEvent> debitEventProducer;
51  
52      private BigDecimal amount = new BigDecimal(10.0);
53  
54      private String paymentOption = PaymentTypeEnum.DEBIT.toString();
55  
56      // Pay Action
57      public String pay() {
58  
59          PaymentEvent currentEvtPayload = new PaymentEvent();
60          currentEvtPayload.setType(PaymentTypeEnum.fromString(paymentOption));
61          currentEvtPayload.setAmount(amount);
62          currentEvtPayload.setDatetime(new Date());
63  
64          switch (currentEvtPayload.getType()) {
65              case DEBIT:
66  
67                  debitEventProducer.fire(currentEvtPayload);
68  
69                  break;
70              case CREDIT:
71                  creditEventProducer.fire(currentEvtPayload);
72  
73                  break;
74  
75              default:
76                  log.severe("invalid payment option");
77                  break;
78          }
79  
80          // paymentAction
81  
82          return "index";
83      }
84  
85      // Reset Action
86      public void reset() {
87          amount = null;
88          paymentOption = "";
89  
90      }
91  
92      public Event<PaymentEvent> getCreditEventLauncher() {
93          return creditEventProducer;
94      }
95  
96      public void setCreditEventLauncher(Event<PaymentEvent> creditEventLauncher) {
97          this.creditEventProducer = creditEventLauncher;
98      }
99  
100     public Event<PaymentEvent> getDebitEventLauncher() {
101         return debitEventProducer;
102     }
103 
104     public void setDebitEventLauncher(Event<PaymentEvent> debitEventLauncher) {
105         this.debitEventProducer = debitEventLauncher;
106     }
107 
108     public String getPaymentOption() {
109         return paymentOption;
110     }
111 
112     public void setPaymentOption(String paymentOption) {
113         this.paymentOption = paymentOption;
114     }
115 
116     public BigDecimal getAmount() {
117         return amount;
118     }
119 
120     public void setAmount(BigDecimal amount) {
121         this.amount = amount;
122     }
123 
124 }