View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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  package org.apache.jetspeed.search.handlers.pam;
18  
19  import java.util.Collection;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  
23  import org.apache.commons.collections.MultiHashMap;
24  import org.apache.jetspeed.om.common.LocalizedField;
25  import org.apache.jetspeed.om.common.portlet.PortletApplication;
26  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
27  import org.apache.jetspeed.search.AbstractObjectHandler;
28  import org.apache.jetspeed.search.BaseParsedObject;
29  import org.apache.jetspeed.search.ParsedObject;
30  import org.apache.jetspeed.util.JetspeedLocale;
31  import org.apache.pluto.om.common.Description;
32  import org.apache.pluto.om.common.DisplayName;
33  import org.apache.pluto.om.common.Language;
34  
35  /***
36   * @author <a href="mailto: jford@apache.org">Jeremy Ford</a>
37   */
38  public class PortletDefinitionHandler extends AbstractObjectHandler
39  {
40      private static final String KEY_PREFIX = "PortletDefinition::";
41      private static final String ID = "ID";
42      private static final String PORTLET_APPLICATION = "portlet_application";
43      
44      {
45          fields.add(ID);
46          fields.add(PORTLET_APPLICATION);
47      }
48  
49      /* (non-Javadoc)
50       * @see org.apache.jetspeed.search.ObjectHandler#parseObject(java.lang.Object)
51       */
52      public ParsedObject parseObject(Object o)
53      {
54          BaseParsedObject result = null;
55          if(o instanceof PortletDefinitionComposite)
56          {
57              result = new BaseParsedObject();
58              PortletDefinitionComposite pd = (PortletDefinitionComposite)o;
59              
60              //need to get Locale here
61              String displayNameText = pd.getDisplayNameText(JetspeedLocale.getDefaultLocale());
62              result.setTitle(displayNameText);
63              
64              String description = pd.getDescriptionText(JetspeedLocale.getDefaultLocale());
65              result.setDescription(description);
66              
67              result.setClassName(pd.getClass().getName());
68              result.setKey(KEY_PREFIX + pd.getUniqueName());
69              result.setType(ParsedObject.OBJECT_TYPE_PORTLET);
70              
71              //TODO: this is common to PAs as well, possible refactor
72              MultiHashMap fieldMap = new MultiHashMap();
73              fieldMap.put(ID, pd.getName());
74              
75              PortletApplication pa = (PortletApplication)pd.getPortletApplicationDefinition();
76              fieldMap.put(PORTLET_APPLICATION, pa.getName()); 
77              
78              Collection mdFields = pd.getMetadata().getFields();
79              for (Iterator fieldIter = mdFields.iterator(); fieldIter.hasNext();)
80              {
81                  LocalizedField field = (LocalizedField) fieldIter.next();                
82                  fieldMap.put(field.getName(), field.getValue());
83              }
84              
85              //Handle descriptions
86              Iterator descIter = pd.getDescriptionSet().iterator();
87              while (descIter.hasNext())
88              {
89                  Description desc = (Description) descIter.next();
90                  fieldMap.put(ParsedObject.FIELDNAME_DESCRIPTION, desc.getDescription());
91              }
92              
93              //Handle keywords and titles
94              Iterator displayNameIter = pd.getDisplayNameSet().iterator();
95              while (displayNameIter.hasNext())
96              {
97                  DisplayName displayName = (DisplayName) displayNameIter.next();
98                  fieldMap.put(ParsedObject.FIELDNAME_TITLE, displayName.getDisplayName());
99              }
100             
101             HashSet keywordSet = new HashSet();
102             
103             Iterator langIter = pd.getLanguageSet().iterator();
104             while (langIter.hasNext())
105             {
106                 Language lang = (Language) langIter.next();
107                 fieldMap.put(ParsedObject.FIELDNAME_TITLE, lang.getTitle());
108                 fieldMap.put(ParsedObject.FIELDNAME_TITLE, lang.getShortTitle());
109                 
110                 Iterator keywordIter = lang.getKeywords();
111                 if (keywordIter != null)
112                 {
113                     while (keywordIter.hasNext())
114                     {
115                         String keyword = (String) keywordIter.next();
116                         keywordSet.add(keyword);
117                     }
118                 }
119             }
120             
121             String[] temp = new String[keywordSet.size()];
122             result.setKeywords((String[])keywordSet.toArray(temp));
123             result.setFields(fieldMap);
124         }
125         return result;
126     }
127 }