]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/helper/product/FinancialsProductWebViewHelperBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / helper / product / FinancialsProductWebViewHelperBean.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jfinancials.beans.helper.product;
18
19 import javax.faces.view.ViewScoped;
20 import javax.inject.Inject;
21 import javax.inject.Named;
22 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
23 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
24 import org.mxchange.jfinancials.beans.localization.FinancialsLocalizationSessionController;
25 import org.mxchange.jproduct.model.category.Category;
26 import org.mxchange.jproduct.model.product.Product;
27
28 /**
29  * A helper for product beans
30  * <p>
31  * @author Roland Häder<roland@mxchange.org>
32  */
33 @Named ("productBeanHelper")
34 @ViewScoped
35 public class FinancialsProductWebViewHelperBean extends BaseFinancialsBean implements FinancialsProductWebViewHelperController {
36
37         /**
38          * Serial number
39          */
40         private static final long serialVersionUID = 17_258_793_567_145_701L;
41
42         /**
43          * Localization controller
44          */
45         @Inject
46         private FinancialsLocalizationSessionController localizationController;
47
48         /**
49          * Default constructor
50          */
51         public FinancialsProductWebViewHelperBean () {
52                 // Call super constructor
53                 super();
54         }
55
56         /**
57          * Returns the product name and price. If null is provided, an empty string
58          * is returned.
59          * <p>
60          * @param product Product instance
61          * <p>
62          * @return Product name
63          */
64         public String renderGenericProduct (final Product product) {
65                 // Default is empty string, so let's get started
66                 final StringBuilder sb = new StringBuilder(10);
67
68                 // Is a product set?
69                 if (product instanceof Product) {
70                         // Is product number given?
71                         if (product.getProductBarCodeNumber() != null) {
72                                 // Prepend it
73                                 sb.append(this.getMessageFromBundle("BARCODE")).append(" "); //NOI18N
74                                 sb.append(product.getProductBarCodeNumber());
75                                 sb.append(", "); //NOI18N
76                         } else if (product.getProductNumber() != null) {
77                                 // Prepend it
78                                 sb.append(this.getMessageFromBundle("NUMBER")).append(" "); //NOI18N
79                                 sb.append(product.getProductNumber());
80                                 sb.append(", "); //NOI18N
81                         }
82
83                         // Add name and price
84                         sb.append(this.getMessageFromBundle(product.getProductI18nKey()));
85
86                         // Is there any age group?
87                         if (product.getProductAgeGroup() != null) {
88                                 // Show it
89                                 sb.append(", ").append(this.getMessageFromBundle(product.getProductAgeGroup().getMessageKey())); //NOI18N
90                         }
91
92                         // Is there any size?
93                         if ((product.getProductSize() != null) && (!product.getProductSize().isEmpty())) {
94                                 // Show size
95                                 sb.append(", ").append(this.getMessageFromBundle("SIZE")).append(product.getProductSize()); //NOI18N
96                         }
97
98                         // Is manufacturer assigned?
99                         if (product.getProductManufacturer() instanceof BasicData) {
100                                 // Yes, then take at least short name
101                                 sb.append(", ").append(product.getProductManufacturer().getCompanyShortName());
102                         }
103
104                         // Add price
105                         sb.append(" ("); //NOI18N
106                         sb.append(this.localizationController.formatFloatNumber(product.getProductGrossPrice().floatValue()));
107                         sb.append(" "); //NOI18N
108                         sb.append(product.getProductCurrencyCode());
109
110                         // Is tax rate given?
111                         if (product.getProductTaxRate() != null) {
112                                 // Add tax rate, formatted after current locale
113                                 sb.append(", ").append(this.localizationController.formatFloatNumber(product.getProductTaxRate().floatValue())).append("%"); //NOI18N
114                         }
115
116                         // Close bracket
117                         sb.append(")"); //NOI18N
118                 }
119
120                 // Return it
121                 return sb.toString();
122         }
123
124         /**
125          * Returns the category's i18n string translated. If null is provided, an
126          * empty string is returned.
127          * <p>
128          * @param category Product category instance
129          * <p>
130          * @return Category's i18n string translation
131          */
132         public String renderProductCategory (final Category category) {
133                 // Default is empty string, so let's get started
134                 final StringBuilder sb = new StringBuilder(10);
135
136                 // Is a category set?
137                 if (category instanceof Category) {
138                         // Add title
139                         sb.append(this.getMessageFromBundle(category.getCategoryI18nKey()));
140
141                         // Is a parent category set?
142                         if (category.getParentCategory() instanceof Category) {
143                                 // Then add it in braces, too
144                                 sb.append(" ("); //NOI18N
145                                 sb.append(this.getMessageFromBundle(category.getParentCategory().getCategoryI18nKey()));
146                                 sb.append(")"); //NOI18N
147                         }
148                 }
149
150                 // Return it
151                 return sb.toString();
152         }
153
154 }