1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.wsrp;
21  
22  import com.liferay.portal.service.PortletLocalServiceUtil;
23  import com.liferay.portal.wsrp.util.WSRPUtil;
24  
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.wsrp4j.exception.WSRPException;
32  import org.apache.wsrp4j.producer.provider.ConsumerConfiguredPortlet;
33  import org.apache.wsrp4j.producer.provider.Portlet;
34  import org.apache.wsrp4j.producer.provider.PortletPool;
35  import org.apache.wsrp4j.producer.provider.ProducerOfferedPortlet;
36  import org.apache.wsrp4j.producer.provider.driver.ConsumerConfiguredPortletImpl;
37  import org.apache.wsrp4j.producer.provider.driver.ProducerOfferedPortletImpl;
38  
39  /**
40   * <a href="PortletPoolImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Michael Young
43   *
44   */
45  public class PortletPoolImpl implements PortletPool {
46  
47      public Iterator getAllConsumerConfiguredPortlets() {
48          return _consumerConfiguredPortlets.values().iterator();
49      }
50  
51      public Iterator getAllProducerOfferedPortlets() {
52          long companyId = WSRPUtil.getCompanyId();
53          Iterator wsrpPortletsIt = null;
54          List wsrpPortlets = new ArrayList();
55  
56          try {
57              List liferayPortlets = PortletLocalServiceUtil.getPortlets(companyId);
58  
59              for (int i = 0; i < liferayPortlets.size(); i++) {
60                  com.liferay.portal.model.Portlet liferayPortlet = (com.liferay.portal.model.Portlet) liferayPortlets
61                          .get(i);
62  
63                  Portlet producerPortlet = _createProducerPortlet(liferayPortlet);
64  
65                  wsrpPortlets.add(producerPortlet);
66              }
67          }
68          catch (Exception e) {
69              e.printStackTrace();
70          }
71  
72          return wsrpPortlets.iterator();
73      }
74  
75      public Portlet clone(String portletHandle) throws WSRPException {
76          Portlet portlet = (Portlet) _consumerConfiguredPortlets
77                  .get(portletHandle);
78  
79          // we don't really support cloning right now
80          // this is done be compliant with the wsrp4j implementation
81          if (portlet != null) {
82              return null;
83          }
84          else {
85              portlet = _createConsumerPortlet(portletHandle);
86              _consumerConfiguredPortlets.put(portletHandle, portlet);
87          }
88  
89          return portlet;
90      }
91  
92      public boolean destroy(String portletHandle) throws WSRPException {
93          // we do not support cloning yet
94          return false;
95      }
96  
97      public Iterator destroySeveral(Iterator portletHandles) {
98          // we do not support cloning yet
99          return null;
100     }
101 
102     public Portlet get(String portletHandle) throws WSRPException {
103         Portlet wsrpPortlet = null;
104 
105         try {
106             wsrpPortlet = (Portlet) _consumerConfiguredPortlets
107                     .get(portletHandle);
108 
109             if (wsrpPortlet == null) {
110                 long companyId = WSRPUtil.getCompanyId();
111 
112                 com.liferay.portal.model.Portlet liferayPortlet = PortletLocalServiceUtil
113                         .getPortletById(companyId, portletHandle);
114 
115                 if (liferayPortlet != null) {
116                     wsrpPortlet = _createProducerPortlet(liferayPortlet);
117                 }
118             }
119         }
120         catch (Exception e) {
121         }
122 
123         return wsrpPortlet;
124     }
125 
126     private Portlet _createProducerPortlet(String portletHandle) {
127         ProducerOfferedPortlet producerPortlet = new ProducerOfferedPortletImpl();
128 
129         producerPortlet.setPortletHandle(portletHandle);
130         producerPortlet.setRegistrationRequired(false);
131 
132         return producerPortlet;
133     }
134 
135     private Portlet _createConsumerPortlet(String portletHandle) {
136         ConsumerConfiguredPortlet consumerPortlet = new ConsumerConfiguredPortletImpl();
137 
138         consumerPortlet.setPortletHandle(portletHandle);
139         consumerPortlet.setParentHandle(portletHandle);
140 
141         return consumerPortlet;
142     }
143 
144     private Portlet _createProducerPortlet(
145             com.liferay.portal.model.Portlet liferayPortlet) {
146         return _createProducerPortlet(liferayPortlet.getPortletId());
147     }
148 
149     private Map _consumerConfiguredPortlets = new HashMap();
150 
151 }