001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.mail.util.recipient;
022:
023: import com.liferay.portal.kernel.util.StringPool;
024: import com.liferay.portal.kernel.util.Validator;
025: import com.liferay.portal.model.User;
026: import com.liferay.portal.service.UserLocalServiceUtil;
027:
028: import java.util.ArrayList;
029: import java.util.LinkedHashMap;
030: import java.util.List;
031: import java.util.Map;
032: import java.util.SortedSet;
033: import java.util.TreeSet;
034:
035: import javax.mail.internet.InternetAddress;
036:
037: import org.apache.commons.collections.map.MultiValueMap;
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: /**
042: * <a href="DirectoryRecipientFinder.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Alexander Chow
045: *
046: */
047: public class DirectoryRecipientFinder implements RecipientFinder {
048:
049: public String getName() {
050: return "directory";
051: }
052:
053: public MultiValueMap getOptions(long userId) {
054: try {
055: User user = UserLocalServiceUtil.getUserById(userId);
056:
057: if (user.hasOrganization()) {
058: return _options;
059: }
060: } catch (Exception e) {
061: _log.error(e, e);
062: }
063:
064: return new MultiValueMap();
065: }
066:
067: public SortedSet getRecipients(long userId, String data, Map options) {
068: SortedSet recipients = new TreeSet();
069:
070: data = data.toLowerCase();
071:
072: try {
073: String optionOrganization = (String) options
074: .get(_OPTION_ORGANIZATION);
075:
076: User user = UserLocalServiceUtil.getUserById(userId);
077:
078: LinkedHashMap params = null;
079:
080: List results = null;
081:
082: if ((user.hasOrganization())
083: && (Validator.isNull(optionOrganization) || optionOrganization
084: .equals("my-organization"))) {
085:
086: params = new LinkedHashMap();
087:
088: long[] organizationIds = user.getOrganizationIds();
089:
090: results = new ArrayList();
091:
092: for (int i = 0; i < organizationIds.length; i++) {
093: params.put("usersOrgs", String
094: .valueOf(organizationIds[i]));
095:
096: List subresults = UserLocalServiceUtil.search(user
097: .getCompanyId(), null, Boolean.TRUE,
098: params, 0, 50, null);
099:
100: results.addAll(subresults);
101: }
102: } else {
103: results = UserLocalServiceUtil.search(user
104: .getCompanyId(), null, Boolean.TRUE, params, 0,
105: 50, null);
106: }
107:
108: for (int i = 0; i < results.size(); i++) {
109: User recipient = (User) results.get(i);
110:
111: String str = recipient.getFullName() + StringPool.SPACE
112: + StringPool.LESS_THAN
113: + recipient.getEmailAddress()
114: + StringPool.GREATER_THAN;
115:
116: if (str.toLowerCase().indexOf(data) != -1) {
117: recipients.add(str);
118: }
119: }
120: } catch (Exception e) {
121: _log.error(e, e);
122: }
123:
124: return recipients;
125: }
126:
127: public boolean isReadOnly(long userId) {
128: return true;
129: }
130:
131: public void save(long userId, InternetAddress ia)
132: throws UnsupportedOperationException {
133:
134: throw new UnsupportedOperationException();
135: }
136:
137: private static MultiValueMap _options = new MultiValueMap();
138:
139: private static final String _OPTION_ORGANIZATION = "organization";
140:
141: static {
142: _options.put(_OPTION_ORGANIZATION, "my-organization");
143: _options.put(_OPTION_ORGANIZATION, "all-available");
144: }
145:
146: private static Log _log = LogFactory
147: .getLog(DirectoryRecipientFinder.class);
148:
149: }
|