01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.lenya.inbox;
19:
20: import java.util.Map;
21:
22: import org.apache.avalon.framework.configuration.Configuration;
23: import org.apache.avalon.framework.configuration.ConfigurationException;
24: import org.apache.avalon.framework.service.ServiceException;
25: import org.apache.avalon.framework.service.ServiceManager;
26: import org.apache.avalon.framework.service.Serviceable;
27: import org.apache.cocoon.components.modules.input.AbstractInputModule;
28: import org.apache.cocoon.environment.ObjectModelHelper;
29: import org.apache.cocoon.environment.Request;
30: import org.apache.lenya.ac.User;
31: import org.apache.lenya.cms.repository.RepositoryUtil;
32: import org.apache.lenya.cms.repository.Session;
33:
34: /**
35: * <p>
36: * Inbox module.
37: * </p>
38: * <p>
39: * Attributes:
40: * </p>
41: * <ul>
42: * <li><strong>newMessageCount</strong> - the number of unread messages as
43: * string</li>
44: * </ul>
45: */
46: public class InboxModule extends AbstractInputModule implements
47: Serviceable {
48:
49: protected static final String NEW_MESSAGE_COUNT = "newMessageCount";
50: protected ServiceManager manager;
51:
52: public Object getAttribute(String name, Configuration modeConf,
53: Map objectModel) throws ConfigurationException {
54:
55: Object value = null;
56: if (name.equals(NEW_MESSAGE_COUNT)) {
57: InboxManager inboxManager = null;
58: try {
59: inboxManager = (InboxManager) this .manager
60: .lookup(InboxManager.ROLE);
61: Request request = ObjectModelHelper
62: .getRequest(objectModel);
63: Session session = RepositoryUtil.getSession(manager,
64: request);
65: User user = session.getIdentity().getUser();
66: if (user == null) {
67: return "0";
68: } else {
69: Inbox inbox = inboxManager.getInbox(user);
70: int count = 0;
71: InboxMessage[] messages = inbox.getMessages();
72: for (int i = 0; i < messages.length; i++) {
73: if (!messages[i].isMarkedAsRead()) {
74: count++;
75: }
76: }
77: value = Integer.toString(count);
78: }
79:
80: } catch (Exception e) {
81: throw new ConfigurationException("Attribute [" + name
82: + "]: ", e);
83: } finally {
84: if (inboxManager != null) {
85: this .manager.release(inboxManager);
86: }
87: }
88: } else {
89: throw new ConfigurationException("Attribute: [" + name
90: + "] not supported.");
91: }
92: return value;
93: }
94:
95: public void service(ServiceManager manager) throws ServiceException {
96: this.manager = manager;
97: }
98:
99: }
|