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.portal.wsrp;
24  
25  import com.liferay.portal.service.PortletLocalServiceUtil;
26  import com.liferay.portal.wsrp.util.WSRPUtil;
27  
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.apache.wsrp4j.exception.WSRPException;
35  import org.apache.wsrp4j.producer.provider.ConsumerConfiguredPortlet;
36  import org.apache.wsrp4j.producer.provider.Portlet;
37  import org.apache.wsrp4j.producer.provider.PortletPool;
38  import org.apache.wsrp4j.producer.provider.ProducerOfferedPortlet;
39  import org.apache.wsrp4j.producer.provider.driver.ConsumerConfiguredPortletImpl;
40  import org.apache.wsrp4j.producer.provider.driver.ProducerOfferedPortletImpl;
41  
42  /**
43   * <a href="PortletPoolImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Michael Young
46   *
47   */
48  public class PortletPoolImpl implements PortletPool {
49  
50      public Iterator getAllConsumerConfiguredPortlets() {
51          return _consumerConfiguredPortlets.values().iterator();
52      }
53  
54      public Iterator getAllProducerOfferedPortlets() {
55          long companyId = WSRPUtil.getCompanyId();
56          Iterator wsrpPortletsIt = null;
57          List wsrpPortlets = new ArrayList();
58  
59          try {
60              List liferayPortlets = PortletLocalServiceUtil.getPortlets(companyId);
61  
62              for (int i = 0; i < liferayPortlets.size(); i++) {
63                  com.liferay.portal.model.Portlet liferayPortlet = (com.liferay.portal.model.Portlet) liferayPortlets
64                          .get(i);
65  
66                  Portlet producerPortlet = _createProducerPortlet(liferayPortlet);
67  
68                  wsrpPortlets.add(producerPortlet);
69              }
70          }
71          catch (Exception e) {
72              e.printStackTrace();
73          }
74  
75          return wsrpPortlets.iterator();
76      }
77  
78      public Portlet clone(String portletHandle) throws WSRPException {
79          Portlet portlet = (Portlet) _consumerConfiguredPortlets
80                  .get(portletHandle);
81  
82          // we don't really support cloning right now
83          // this is done be compliant with the wsrp4j implementation
84          if (portlet != null) {
85              return null;
86          }
87          else {
88              portlet = _createConsumerPortlet(portletHandle);
89              _consumerConfiguredPortlets.put(portletHandle, portlet);
90          }
91  
92          return portlet;
93      }
94  
95      public boolean destroy(String portletHandle) throws WSRPException {
96          // we do not support cloning yet
97          return false;
98      }
99  
100     public Iterator destroySeveral(Iterator portletHandles) {
101         // we do not support cloning yet
102         return null;
103     }
104 
105     public Portlet get(String portletHandle) throws WSRPException {
106         Portlet wsrpPortlet = null;
107 
108         try {
109             wsrpPortlet = (Portlet) _consumerConfiguredPortlets
110                     .get(portletHandle);
111 
112             if (wsrpPortlet == null) {
113                 long companyId = WSRPUtil.getCompanyId();
114 
115                 com.liferay.portal.model.Portlet liferayPortlet = PortletLocalServiceUtil
116                         .getPortletById(companyId, portletHandle);
117 
118                 if (liferayPortlet != null) {
119                     wsrpPortlet = _createProducerPortlet(liferayPortlet);
120                 }
121             }
122         }
123         catch (Exception e) {
124         }
125 
126         return wsrpPortlet;
127     }
128 
129     private Portlet _createProducerPortlet(String portletHandle) {
130         ProducerOfferedPortlet producerPortlet = new ProducerOfferedPortletImpl();
131 
132         producerPortlet.setPortletHandle(portletHandle);
133         producerPortlet.setRegistrationRequired(false);
134 
135         return producerPortlet;
136     }
137 
138     private Portlet _createConsumerPortlet(String portletHandle) {
139         ConsumerConfiguredPortlet consumerPortlet = new ConsumerConfiguredPortletImpl();
140 
141         consumerPortlet.setPortletHandle(portletHandle);
142         consumerPortlet.setParentHandle(portletHandle);
143 
144         return consumerPortlet;
145     }
146 
147     private Portlet _createProducerPortlet(
148             com.liferay.portal.model.Portlet liferayPortlet) {
149         return _createProducerPortlet(liferayPortlet.getPortletId());
150     }
151 
152     private Map _consumerConfiguredPortlets = new HashMap();
153 
154 }