package org.mxchange.jfinancials.model.receipt;
import java.text.MessageFormat;
-import java.util.GregorianCalendar;
-import java.util.List;
-import java.util.Objects;
+import java.util.Date;
+import javax.ejb.EJB;
import javax.ejb.Stateless;
-import javax.persistence.Query;
import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean;
import org.mxchange.jfinancials.exceptions.ReceiptAlreadyAddedException;
import org.mxchange.jusercore.model.user.User;
/**
- * A stateless bean for general purposes for receipts
+ * A stateless bean for administrative purposes for receipts
* <p>
* @author Roland Häder<roland@mxchange.org>
*/
-@Stateless (name = "financialReceipt", description = "A stateless session bean for handling receipts.")
-public class FinancialReceiptSessionBean extends BaseFinancialsDatabaseBean implements FinancialReceiptSessionBeanRemote {
+@Stateless (name = "adminFinancialReceipt", description = "A stateless session bean for handling receipts.")
+public class FinancialAdminReceiptSessionBean extends BaseFinancialsDatabaseBean implements FinancialAdminReceiptSessionBeanRemote {
/**
* Serial number
*/
private static final long serialVersionUID = 659_868_076_723_741L;
+ /**
+ * General receipt bean
+ */
+ @EJB
+ private FinancialReceiptSessionBean receiptBean;
+
@Override
public BillableReceipt addReceipt (final BillableReceipt receipt) throws ReceiptAlreadyAddedException {
// Trace message
} else if ((receipt.getReceiptUser() instanceof User) && receipt.getReceiptUser().getUserId() < 1) {
// Throw NPE again
throw new NullPointerException(MessageFormat.format("receipt.receiptUser.userId={0} is not valid", receipt.getReceiptUser().getUserId()));
- } else if (this.isReceiptRegistered(receipt)) {
+ } else if (this.receiptBean.isReceiptRegistered(receipt)) {
// Has already been registered
throw new ReceiptAlreadyAddedException(receipt);
}
// Add created instance
- receipt.setReceiptCreated(new GregorianCalendar());
+ receipt.setReceiptCreated(new Date());
// Persist it
this.getEntityManager().persist(receipt);
return receipt;
}
- @Override
- @SuppressWarnings ("unchecked")
- public List<BillableReceipt> allReceipts () {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allReceipts(): CALLED!", this.getClass().getSimpleName()));
-
- // Query all
- final Query query = this.getEntityManager().createNamedQuery("AllReceipts");
-
- // Get all
- final List<BillableReceipt> receipts = query.getResultList();
-
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allReceipts(): receipts.size()={1} EXIT!", this.getClass().getSimpleName(), receipts.size()));
-
- // Return it
- return receipts;
- }
-
- @Override
- @SuppressWarnings ("unchecked")
- public List<BillableReceipt> allUsersReceipts (final User user) {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsersReceipts(): user={1} - CALLED!", this.getClass().getSimpleName(), user));
-
- // Validate parameter
- if (null == user) {
- // Throw NPE
- throw new NullPointerException("user is null");
- } else if (user.getUserId() == null) {
- // Throw it again
- throw new NullPointerException("user.userId is null");
- } else if (user.getUserId() < 1) {
- // Throw IAE
- throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId()));
- }
-
- // Query all
- final Query query = this.getEntityManager().createNamedQuery("SearchAllUserReceipts");
-
- // Add parameter
- query.setParameter("receiptUser", user);
-
- // Get all
- final List<BillableReceipt> receipts = query.getResultList();
-
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsersReceipts(): receipts.size()={1} EXIT!", this.getClass().getSimpleName(), receipts.size()));
-
- // Return it
- return receipts;
- }
-
- /**
- * Checks if given receipt is already persisted by checking receipt number
- * and branch office combination.
- * <p>
- * @param receipt Receipt instance
- * <p>
- * @return Whether the receipt has already been registered
- */
- private boolean isReceiptRegistered (final BillableReceipt receipt) {
- // Get all receipts
- final List<BillableReceipt> receipts = this.allReceipts();
-
- // Is the list empty?
- if (receipts.isEmpty()) {
- // Abort here
- return false;
- }
-
- // Default is not found
- boolean isFound = false;
-
- // Now, check each entry
- for (final BillableReceipt foundReceipt : receipts) {
- // Is same entity or same receipt number and branch office found?
- if (Objects.equals(foundReceipt, receipt)) {
- // Yes, then stop searching
- isFound = true;
- break;
- } else if (Receipts.isSameReceipt(foundReceipt, receipt)){
- // Yes, then stop searching
- isFound = true;
- break;
- }
- }
-
- // Return flag
- return isFound;
- }
-
}