1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.mail.util.recipient;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  
30  import java.util.ArrayList;
31  import java.util.LinkedHashMap;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.SortedSet;
35  import java.util.TreeSet;
36  
37  import javax.mail.internet.InternetAddress;
38  
39  import org.apache.commons.collections.map.MultiValueMap;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /**
44   * <a href="DirectoryRecipientFinder.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Alexander Chow
47   *
48   */
49  public class DirectoryRecipientFinder implements RecipientFinder {
50  
51      public String getName() {
52          return "directory";
53      }
54  
55      public MultiValueMap getOptions(long userId) {
56          try {
57              User user = UserLocalServiceUtil.getUserById(userId);
58  
59              if (user.hasOrganization()) {
60                  return _options;
61              }
62          }
63          catch (Exception e) {
64              _log.error(e, e);
65          }
66  
67          return new MultiValueMap();
68      }
69  
70      public SortedSet getRecipients(long userId, String data, Map options) {
71          SortedSet recipients = new TreeSet();
72  
73          data = data.toLowerCase();
74  
75          try {
76              String optionOrganization =
77                  (String)options.get(_OPTION_ORGANIZATION);
78  
79              User user = UserLocalServiceUtil.getUserById(userId);
80  
81              LinkedHashMap params = null;
82  
83              List results = null;
84  
85              if ((user.hasOrganization()) &&
86                  (Validator.isNull(optionOrganization) ||
87                   optionOrganization.equals("my-organization"))) {
88  
89                  params = new LinkedHashMap();
90  
91                  long[] organizationIds = user.getOrganizationIds();
92  
93                  results = new ArrayList();
94  
95                  for (int i = 0; i < organizationIds.length; i++) {
96                      params.put("usersOrgs", String.valueOf(organizationIds[i]));
97  
98                      List subresults = UserLocalServiceUtil.search(
99                          user.getCompanyId(), null, Boolean.TRUE, params, 0, 50,
100                         null);
101 
102                     results.addAll(subresults);
103                 }
104             }
105             else {
106                 results = UserLocalServiceUtil.search(
107                     user.getCompanyId(), null, Boolean.TRUE, params, 0, 50,
108                     null);
109             }
110 
111             for (int i = 0; i < results.size(); i++) {
112                 User recipient = (User)results.get(i);
113 
114                 String str =
115                     recipient.getFullName() + StringPool.SPACE +
116                         StringPool.LESS_THAN + recipient.getEmailAddress() +
117                             StringPool.GREATER_THAN;
118 
119                 if (str.toLowerCase().indexOf(data) != -1) {
120                     recipients.add(str);
121                 }
122             }
123         }
124         catch (Exception e) {
125             _log.error(e, e);
126         }
127 
128         return recipients;
129     }
130 
131     public boolean isReadOnly(long userId) {
132         return true;
133     }
134 
135     public void save(long userId, InternetAddress ia)
136         throws UnsupportedOperationException {
137 
138         throw new UnsupportedOperationException();
139     }
140 
141     private static MultiValueMap _options = new MultiValueMap();
142 
143     private static final String _OPTION_ORGANIZATION = "organization";
144 
145     static {
146         _options.put(_OPTION_ORGANIZATION, "my-organization");
147         _options.put(_OPTION_ORGANIZATION, "all-available");
148     }
149 
150     private static Log _log = LogFactory.getLog(DirectoryRecipientFinder.class);
151 
152 }