]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/profile/FinancialsUserProfileWebRequestBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / profile / FinancialsUserProfileWebRequestBean.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.profile;
18
19 import java.text.MessageFormat;
20 import javax.enterprise.context.RequestScoped;
21 import javax.faces.FacesException;
22 import javax.inject.Inject;
23 import javax.inject.Named;
24 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
25 import org.mxchange.jfinancials.beans.user.list.FinancialsUserListWebViewController;
26 import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionController;
27 import org.mxchange.jusercore.exceptions.UserNotFoundException;
28 import org.mxchange.jusercore.model.user.User;
29 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
30
31 /**
32  * A web request bean for user profiles
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @Named (value = "profileController")
37 @RequestScoped
38 public class FinancialsUserProfileWebRequestBean extends BaseFinancialsBean implements FinancialsUserProfileWebRequestController {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 187_687_145_286_710L;
44
45         /**
46          * User list controller
47          */
48         @Inject
49         private FinancialsUserListWebViewController userListController;
50
51         /**
52          * Login controller
53          */
54         @Inject
55         private FinancialsUserLoginWebSessionController userLoginController;
56
57         /**
58          * Default constructor
59          */
60         public FinancialsUserProfileWebRequestBean () {
61                 // Call super constructor
62                 super();
63         }
64
65         @Override
66         public boolean isProfileLinkVisibleById (final Long userId) {
67                 // Init user instance
68                 User user = null;
69
70                 try {
71                         // Try to get it
72                         user = this.userListController.lookupUserById(userId);
73                 } catch (final UserNotFoundException ex) {
74                         // Throw again
75                         throw new FacesException(ex);
76                 }
77
78                 // Is it null?
79                 if (null == user) {
80                         // Not found, not visible.
81                         return false;
82                 }
83
84                 // Ask other method
85                 return this.isProfileLinkVisibleByUser(user);
86         }
87
88         @Override
89         public boolean isProfileLinkVisibleByUser (final User user) {
90                 // Check on user
91                 if (null == user) {
92                         /*
93                          * Not set, means wrong invocation of this method as the user
94                          * instance needs to be set first.
95                          */
96                         throw new NullPointerException("user is null"); //NOI18N
97                 } else if (user.getUserId() == null) {
98                         /*
99                          * If the id number is not set it means that the user instance has
100                          * not been persisted and the JPA has not been flushed. Or a
101                          * "virgin" instance (e.g. from registration) has been used.
102                          */
103                         throw new NullPointerException("user.userId is null"); //NOI18N
104                 } else if (user.getUserId() < 1) {
105                         /*
106                          * The id number is set invalid for an unknown reason.
107                          */
108                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
109                 } else if (user.getUserProfileMode() == null) {
110                         /*
111                          * Possibly an out-dated user profile is being used. This should not
112                          * happen.
113                          */
114                         throw new NullPointerException("user.userProfileMode is null"); //NOI18N
115                 }
116
117                 // Get profile mode from user instance (safe now)
118                 final ProfileMode profileMode = user.getUserProfileMode();
119
120                 // Check all conditions (except for admin)
121                 return ((profileMode.equals(ProfileMode.PUBLIC)) ||
122                                 (this.userLoginController.isUserLoggedIn()) && (profileMode.equals(ProfileMode.MEMBERS)));
123         }
124
125 }