1 package org.jahia.layout; 2 3 import java.util.Enumeration ; 4 import java.util.Vector ; 5 6 import org.jahia.data.JahiaData; 7 import org.jahia.data.containers.JahiaContainer; 8 import org.jahia.data.containers.JahiaContainerDefinition; 9 import org.jahia.data.containers.JahiaContainerList; 10 import org.jahia.data.fields.FieldTypes; 11 import org.jahia.data.fields.JahiaField; 12 import org.jahia.data.fields.JahiaFieldDefinition; 13 import org.jahia.exceptions.JahiaException; 14 import org.jahia.registries.ServicesRegistry; 15 import org.jahia.services.fields.ContentField; 16 import org.jahia.services.fields.ContentSmallTextSharedLangField; 17 import org.jahia.services.version.EntryLoadRequest; 18 import org.jahia.services.version.EntrySaveRequest; 19 import org.jahia.services.containers.ContentContainer; 20 import java.util.Set ; 21 import java.util.HashSet ; 22 import org.jahia.content.ContentObject; 23 import org.jahia.services.version.StateModificationContext; 24 import org.jahia.content.ContentContainerKey; 25 26 39 40 public class PortletsJahiaContentSerializer { 41 42 private static org.apache.log4j.Logger logger = 43 org.apache.log4j.Logger.getLogger(PortletsJahiaContentSerializer.class); 44 45 private static final String METADATA_CONTAINER_SUFFIT = "MetaData"; 46 47 private static final int defaultPortletID = 0; 49 private static final int defaultPortletSourceID = 0; 50 private static final int defaultPortletW = 100; 51 private static final int defaultPortletH = 100; 52 private static final int defaultPortletColumn = 0; 53 private static final int defaultPortletRow = 0; 54 55 private JahiaData jData; 56 private String portletGroupName; 57 58 64 public PortletsJahiaContentSerializer(JahiaData jData, String portletGroupName) 65 throws JahiaException { 66 this.jData = jData; 67 this.portletGroupName = portletGroupName + METADATA_CONTAINER_SUFFIT; 68 declareJahiaTemplatePortlets(); 71 } 72 73 78 private void declareJahiaTemplatePortlets() 79 throws JahiaException { 80 PortletBean defaultPortlet = new PortletBean(defaultPortletID, 81 defaultPortletSourceID, 82 defaultPortletW, 83 defaultPortletH, 84 defaultPortletColumn, 85 defaultPortletRow); 86 declareJahiaTemplatePortlets(defaultPortlet); 87 } 88 89 94 private void declareJahiaTemplatePortlets(PortletBean defaultValues) 95 throws JahiaException { 96 97 try { 98 99 jData.containers().declareField( this.portletGroupName, "PortletID", "ID", FieldTypes.SMALLTEXT_SHARED_LANG, 100 Integer.toString(defaultValues.getPortletID()) ); 101 jData.containers().declareField( this.portletGroupName, "PortletSourceID", "Source ID", FieldTypes.SMALLTEXT_SHARED_LANG, 102 Integer.toString(defaultValues.getPortletSourceID()) ); 103 jData.containers().declareField( this.portletGroupName, "PortletW", "Width", FieldTypes.SMALLTEXT_SHARED_LANG, 104 Integer.toString(defaultValues.getPortletW()) ); 105 jData.containers().declareField( this.portletGroupName, "PortletH", "Height", FieldTypes.SMALLTEXT_SHARED_LANG, 106 Integer.toString(defaultValues.getPortletH()) ); 107 jData.containers().declareField( this.portletGroupName, "PortletColumn", "Column", FieldTypes.SMALLTEXT_SHARED_LANG, 108 Integer.toString(defaultValues.getPortletColumn()) ); 109 jData.containers().declareField( this.portletGroupName, "PortletRow", "Row", FieldTypes.SMALLTEXT_SHARED_LANG, 110 Integer.toString(defaultValues.getPortletRow()) ); 111 112 Vector theFieldList = new Vector (); 113 theFieldList.add( "PortletID"); 114 theFieldList.add( "PortletSourceID" ); 115 theFieldList.add( "PortletW" ); 116 theFieldList.add( "PortletH" ); 117 theFieldList.add( "PortletColumn" ); 118 theFieldList.add( "PortletRow" ); 119 jData.containers().declareContainer( portletGroupName, "Portlet List Meta Data", theFieldList ); 120 logger.debug("Container for portlet list meta data [" + 121 portletGroupName + "] declared"); 122 } catch (JahiaException je) { 123 126 logger.debug("Portlets already defined..."); 127 } 128 129 } 130 131 135 public Enumeration getPortlets(String currentUserName) throws JahiaException { 136 JahiaContainerList portletContainerList = jData.containers().getContainerList(portletGroupName); 139 Enumeration portletContainerEnumeration = portletContainerList.getContainers(); 140 Vector resultPortletList = new Vector (); 141 while (portletContainerEnumeration.hasMoreElements()) { 142 JahiaContainer portletContainer = (JahiaContainer) portletContainerEnumeration.nextElement(); 143 resultPortletList.add(containerToPortlet(portletContainer)); 144 } 145 return resultPortletList.elements(); 146 } 147 148 private PortletBean containerToPortlet(JahiaContainer container) throws JahiaException { 149 PortletBean resultPortlet = null; 150 resultPortlet = new PortletBean(container.getFieldValue("PortletID"), 151 container.getFieldValue("PortletSourceID"), 152 container.getFieldValue("PortletW"), 153 container.getFieldValue("PortletH"), 154 container.getFieldValue("PortletColumn"), 155 container.getFieldValue("PortletRow"), 156 container.getID() 157 ); 158 159 return resultPortlet; 160 } 161 162 private void setSharedFieldValue(JahiaContainer container, String fieldName, String fieldValue) throws JahiaException { 163 if (container.getField(fieldName) == null) { 164 JahiaContainerDefinition ctnDef = container.getDefinition(); 165 JahiaFieldDefinition fieldDef = ctnDef.findFieldInStructure(fieldName, jData.params().getPage().getPageTemplateID()); 166 if (fieldDef != null) { 167 168 ContentSmallTextSharedLangField theField = 169 ContentSmallTextSharedLangField.createSmallText( 170 jData.params().getSiteID(), 171 jData.params().getPageID(), 172 container.getID(), 173 fieldDef.getID(), 174 container.getAclID(), 175 0, 176 fieldValue, 177 jData.params()); 178 } 179 } else { 180 JahiaField theField = container.getField(fieldName); 181 ContentSmallTextSharedLangField contentField = (ContentSmallTextSharedLangField) ContentField.getField(theField.getID()); 182 183 contentField.setText(fieldValue, new EntrySaveRequest(jData.params().getUser())); 184 185 } 186 } 187 188 private void portletToContainer(PortletBean pb, JahiaContainer container) throws JahiaException { 189 setSharedFieldValue(container, "PortletID", Integer.toString(pb.getPortletID())); 190 setSharedFieldValue(container, "PortletSourceID", Integer.toString(pb.getPortletSourceID())); 191 setSharedFieldValue(container, "PortletW", Integer.toString(pb.getPortletW())); 192 setSharedFieldValue(container, "PortletH", Integer.toString(pb.getPortletH())); 193 setSharedFieldValue(container, "PortletColumn", Integer.toString(pb.getPortletColumn())); 194 setSharedFieldValue(container, "PortletRow", Integer.toString(pb.getPortletRow())); 195 } 196 197 public PortletBean getPortlet(String currentUserName, 198 int portletID) throws JahiaException { 199 JahiaContainerList portletContainerList = jData.containers().getContainerList(portletGroupName); 200 Enumeration portletContainerEnumeration = portletContainerList.getContainers(); 201 while (portletContainerEnumeration.hasMoreElements()) { 202 JahiaContainer portletContainer = (JahiaContainer) portletContainerEnumeration.nextElement(); 203 PortletBean portlet = containerToPortlet(portletContainer); 204 if (portlet.getPortletID() == portletID) 205 return portlet; 206 } 207 return null; 208 } 209 210 216 public Enumeration getPortletsFromColumn(int theColumn, String currentUserName) throws JahiaException { 217 218 Enumeration allPortlets = getPortlets(currentUserName); 219 Vector portletList = new Vector (); 220 while (allPortlets.hasMoreElements()) { 221 PortletBean portlet = (PortletBean) allPortlets.nextElement(); 222 if (portlet.getPortletColumn() == theColumn) { 223 portletList.add(portlet); 224 } 225 } 226 227 return portletList.elements(); 228 } 230 231 public boolean isPortletInStore(PortletBean portlet, 232 String currentUserName) throws JahiaException { 233 return (getPortlet(currentUserName, portlet.getPortletID()) != null); 234 } 235 236 private JahiaContainer lookupPortletContainer(PortletBean portlet, 237 String currentUserName) 238 throws JahiaException 239 { 240 logger.debug("Looking up portlet " + portlet.getPortletSourceID() + " for user " + currentUserName); 241 JahiaContainerList portletContainerList = jData.containers().getContainerList(portletGroupName); 242 Enumeration portletContainerEnumeration = portletContainerList.getContainers(); 243 while (portletContainerEnumeration.hasMoreElements()) { 244 JahiaContainer portletContainer = (JahiaContainer) portletContainerEnumeration.nextElement(); 245 int containerPortletID = Integer.parseInt(portletContainer.getField("PortletSourceID").getValue()); 246 logger.debug("Source ID = " + Integer.toString(containerPortletID)); 247 if (containerPortletID == portlet.getPortletSourceID()) { 248 return portletContainer; 249 } 250 } 251 return null; 252 } 253 254 public void storePortlet(PortletBean portlet, 255 String currentUserName) 256 throws JahiaException { 257 logger.debug("Storing portlet " + portlet.getPortletSourceID() + " meta data for user " + currentUserName); 258 JahiaContainer portletContainer = lookupPortletContainer(portlet, currentUserName); 259 if (portletContainer != null) { 260 logger.debug("Updating portlet container..."); 261 portletToContainer(portlet, portletContainer); 262 ContentContainer.invalidateContainerCache(portletContainer.getID()); 263 } else { 264 logger.debug("Creating portlet " + portlet.getPortletSourceID() + " meta data for user " +currentUserName); 266 JahiaContainerList portletList = jData.containers().getContainerList(this.portletGroupName); 267 ServicesRegistry.getInstance().getJahiaContainersService().saveContainerListInfo(portletList, jData.page().getAclID()); 268 portletList = jData.containers().getContainerList(this.portletGroupName); 269 logger.debug("ACL ID of container list=" + Integer.toString(portletList.getAclID())); 270 portletContainer = new JahiaContainer(0, jData.params().getJahiaID(), 271 jData.params().getPageID(), 272 portletList.getID(), 273 0, 274 portletList.getAclID(), 275 portletList.getctndefid(), 276 0, 277 EntryLoadRequest.STAGING_WORKFLOW_STATE); 278 279 ServicesRegistry.getInstance().getJahiaContainersService().saveContainer(portletContainer, 0, jData.params()); 280 logger.debug("Setting field values in container"); 281 portletToContainer(portlet, portletContainer); 282 } 283 } 284 285 public void removePortlet(PortletBean portlet, 286 String currentUserName) throws JahiaException { 287 JahiaContainer portletContainer = lookupPortletContainer(portlet, currentUserName); 288 if (portletContainer != null) { 289 ContentContainer contentContainer = ContentContainer. 291 getContainer (portletContainer.getID ()); 292 293 Set curLanguageCodes = new HashSet (); 296 curLanguageCodes.add (ContentObject.SHARED_LANGUAGE); 297 298 StateModificationContext stateModifContext = 299 new StateModificationContext (new 300 ContentContainerKey ( 301 portletContainer.getID ()), curLanguageCodes, true); 302 stateModifContext.pushAllLanguages (true); 303 304 contentContainer.markLanguageForDeletion ( 305 jData.params().getUser (), 306 ContentObject.SHARED_LANGUAGE, 307 stateModifContext); 308 } 309 } 310 311 } 312 | Popular Tags |