1   /**
2    * Copyright (c) 2000-2007 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.Organization;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  
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              Organization organization = user.getOrganization();
60  
61              if (organization.getOrganizationId() > 0) {
62                  return _options;
63              }
64          }
65          catch (Exception e) {
66              _log.error(e, e);
67          }
68  
69          return new MultiValueMap();
70      }
71  
72      public SortedSet getRecipients(long userId, String data, Map options) {
73          SortedSet recipients = new TreeSet();
74  
75          data = data.toLowerCase();
76  
77          try {
78              String optionOrganization =
79                  (String)options.get(_OPTION_ORGANIZATION);
80  
81              User user = UserLocalServiceUtil.getUserById(userId);
82  
83              Organization organization = user.getOrganization();
84  
85              LinkedHashMap params = null;
86  
87              if ((organization.getOrganizationId() > 0) &&
88                  (Validator.isNull(optionOrganization) ||
89                   optionOrganization.equals("my-organization"))) {
90  
91                  params = new LinkedHashMap();
92  
93                  params.put(
94                      "usersOrgs", new Long(organization.getOrganizationId()));
95              }
96  
97              List results = UserLocalServiceUtil.search(
98                  user.getCompanyId(), null, Boolean.TRUE, params, 0, 50, null);
99  
100             for (int i = 0; i < results.size(); i++) {
101                 User recipient = (User)results.get(i);
102 
103                 String str =
104                     recipient.getFullName() + StringPool.SPACE +
105                         StringPool.LESS_THAN + recipient.getEmailAddress() +
106                             StringPool.GREATER_THAN;
107 
108                 if (str.toLowerCase().indexOf(data) != -1) {
109                     recipients.add(str);
110                 }
111             }
112         }
113         catch (Exception e) {
114             _log.error(e, e);
115         }
116 
117         return recipients;
118     }
119 
120     public boolean isReadOnly(long userId) {
121         return true;
122     }
123 
124     public void save(long userId, InternetAddress ia)
125         throws UnsupportedOperationException {
126 
127         throw new UnsupportedOperationException();
128     }
129 
130     private static MultiValueMap _options = new MultiValueMap();
131 
132     private static final String _OPTION_ORGANIZATION = "organization";
133 
134     static {
135         _options.put(_OPTION_ORGANIZATION, "my-organization");
136         _options.put(_OPTION_ORGANIZATION, "all-available");
137     }
138 
139     private static Log _log = LogFactory.getLog(DirectoryRecipientFinder.class);
140 
141 }