]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bec10d722e9f03635a286406fb9804242cc6b37a
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  *//**
24  *  This document is a part of the source code and related artifacts
25  *  for CollectionSpace, an open source collections management system
26  *  for museums and related institutions:
27
28  *  http://www.collectionspace.org
29  *  http://wiki.collectionspace.org
30
31  *  Copyright 2009 University of California at Berkeley
32
33  *  Licensed under the Educational Community License (ECL), Version 2.0.
34  *  You may not use this file except in compliance with this License.
35
36  *  You may obtain a copy of the ECL 2.0 License at
37
38  *  https://source.collectionspace.org/collection-space/LICENSE.txt
39
40  *  Unless required by applicable law or agreed to in writing, software
41  *  distributed under the License is distributed on an "AS IS" BASIS,
42  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43  *  See the License for the specific language governing permissions and
44  *  limitations under the License.
45  */
46 /*
47  * To change this template, choose Tools | Templates
48  * and open the template in the editor.
49  */
50 package org.collectionspace.services.account.storage;
51
52 import java.util.List;
53 import java.util.regex.Matcher;
54 import java.util.regex.Pattern;
55 import org.collectionspace.services.account.AccountTenant;
56 import org.collectionspace.services.account.AccountsCommon;
57 import org.collectionspace.services.account.Tenant;
58 import org.collectionspace.services.common.ServiceMessages;
59 import org.collectionspace.services.common.context.ServiceContext;
60 import org.collectionspace.services.common.document.DocumentHandler.Action;
61 import org.collectionspace.services.common.document.DocumentNotFoundException;
62 import org.collectionspace.services.common.document.InvalidDocumentException;
63 import org.collectionspace.services.common.document.ValidatorHandler;
64 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67
68 /**
69  *
70  * @author 
71  */
72 public class AccountValidatorHandler implements ValidatorHandler {
73
74     final Logger logger = LoggerFactory.getLogger(AccountValidatorHandler.class);
75
76     @Override
77     public void validate(Action action, ServiceContext ctx)
78             throws InvalidDocumentException {
79         if (logger.isDebugEnabled()) {
80             logger.debug("validate() action=" + action.name());
81         }
82         try {
83             AccountsCommon account = (AccountsCommon) ctx.getInput();
84             StringBuilder msgBldr = new StringBuilder(ServiceMessages.VALIDATION_FAILURE);
85             boolean invalid = false;
86
87             List<AccountTenant> tl = account.getTenants();
88             if (tl != null && tl.size() > 0) {
89                 if (isInvalidTenant(tl, msgBldr)) {
90                     invalid = true;
91                 }
92             }
93
94             if (action.equals(Action.CREATE)) {
95
96                 //create specific validation here
97                 if (account.getScreenName() == null || account.getScreenName().isEmpty()) {
98                     invalid = true;
99                     msgBldr.append("\nscreenName : missing");
100                 }
101                 if (account.getUserId() == null || account.getUserId().isEmpty()) {
102                     invalid = true;
103                     msgBldr.append("\nuserId : missing");
104                 }
105                 if (account.getPassword() == null || account.getPassword().length == 0) {
106                     invalid = true;
107                     msgBldr.append("\npassword : missing");
108                 }                
109                 if (account.getEmail() == null || account.getEmail().isEmpty()) {
110                     invalid = true;
111                     msgBldr.append("\nemail : missing");
112                 } else {
113                     if (isInvalidEmail(account.getEmail(), msgBldr)) {
114                         invalid = true;
115                     }
116                 }
117             } else if (action.equals(Action.UPDATE)) {
118                 //update specific validation here
119                 if (account.getScreenName() != null && account.getScreenName().isEmpty()) {
120                     invalid = true;
121                     msgBldr.append("\nscreenName : cannot be changed!");
122                 }
123                 if (account.getPassword() != null
124                         && (account.getUserId() == null || account.getUserId().isEmpty())) {
125                     invalid = true;
126                     msgBldr.append("\npassword : userId is needed");
127                 }
128                 if (account.getEmail() != null) {
129                     if (isInvalidEmail(account.getEmail(), msgBldr)) {
130                         invalid = true;
131                     }
132                 }
133             }
134             if (invalid) {
135                 String msg = msgBldr.toString();
136                 logger.error(msg);
137                 throw new InvalidDocumentException(msg);
138             }
139         } catch (InvalidDocumentException ide) {
140             throw ide;
141         } catch (Exception e) {
142             throw new InvalidDocumentException(e);
143         }
144     }
145
146     private boolean isInvalidEmail(String email, StringBuilder msgBldr) {
147         boolean invalid = false;
148         Pattern p = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[_A-Za-z0-9-]+)");
149         Matcher m = p.matcher(email);
150         if (!m.find()) {
151             invalid = true;
152             msgBldr.append("\nemail : invalid " + email);
153         }
154         return invalid;
155     }
156
157     private boolean isInvalidTenant(List<AccountTenant> atList, StringBuilder msgBldr)
158                 throws DocumentNotFoundException {
159         boolean invalid = false;
160         for (AccountTenant at : atList) {
161             String tid = at.getTenantId();
162             if (tid == null || tid.isEmpty()) {
163                 invalid = true;
164                 msgBldr.append("\n tenant : tenantId is missing");
165                 break;
166             }
167             Tenant tenantFound = (Tenant) JpaStorageUtils.getEntity(tid, Tenant.class);
168             if (tenantFound == null) {
169                 invalid = true;
170                 msgBldr.append("\n tenant : tenantId=" + tid
171                         + " not found");
172                 break;
173             }
174         }
175         return invalid;
176     }
177 }