KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > services > ContainerServices


1 package com.dotmarketing.services;
2
3 import java.io.File JavaDoc;
4
5 import com.dotmarketing.beans.Identifier;
6 import com.dotmarketing.factories.IdentifierFactory;
7 import com.dotmarketing.factories.InodeFactory;
8 import com.dotmarketing.portlets.containers.model.Container;
9 import com.dotmarketing.portlets.structure.model.Structure;
10 import com.dotmarketing.util.Config;
11 import com.dotmarketing.util.Logger;
12 import com.dotmarketing.util.UtilMethods;
13
14 /**
15  * @author will
16  */

17 public class ContainerServices {
18
19     public static void publishContainerToFile(Container container) {
20
21         Identifier identifier = IdentifierFactory.getParentIdentifier(container);
22         writeContainerToFile(container, identifier, false);
23
24     }
25
26     public static void writeContainerToFile(Container container, boolean EDIT_MODE) {
27
28         Identifier identifier = IdentifierFactory.getParentIdentifier(container);
29         writeContainerToFile(container, identifier, EDIT_MODE);
30
31     }
32     
33     public static void writeContainerToFile(Container container, Identifier identifier, boolean EDIT_MODE) {
34
35         boolean isDynamic = UtilMethods.isSet(container.getLuceneQuery());
36         
37         // let's write this puppy out to our file
38
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
39         sb.append("#set ($SERVER_NAME =\"$host.getHostname()\" )\n");
40         sb.append("#set ($CONTAINER_IDENTIFIER_INODE = " +identifier.getInode() + ")\n");
41         sb.append("#set ($CONTAINER_INODE = " +container.getInode() + ")\n");
42         sb.append("#set ($CONTAINER_MAX_CONTENTLETS = " + container.getMaxContentlets()+ ")\n");
43         Structure st = (Structure) InodeFactory.getParentOfClass(container, Structure.class);
44         sb.append("#set ($CONTAINER_STRUCTURE_NAME = \"" + (UtilMethods.isSet(st.getName())?st.getName():"") + "\")\n");
45         sb.append("#set ($STATIC_CONTAINER = " + !UtilMethods.isSet(container.getLuceneQuery()) +")\n");
46         sb.append("#set ($SORT_PAGE = \"" + container.getSortContentletsBy() + "\")\n");
47         sb.append("#set ($containerInode = \"" + container.getInode() + "\")\n");
48
49         //Permissions over the container
50
sb.append("#set ($EDIT_CONTAINER_PERMISSION = $EDIT_CONTAINER_PERMISSION" + identifier.getInode() + ")\n");
51
52         //Permissions over the structure to add new contents
53
sb.append("#set ($ADD_CONTENT_PERMISSION = $ADD_CONTENT_PERMISSION" + identifier.getInode() + ")\n");
54         
55         sb.append("#set ($CONTENTLETS = $contentletList" + identifier.getInode() + ")\n");
56         sb.append("#set ($CONTAINER_NUM_CONTENTLETS = $contentletList" + identifier.getInode() + ".size())\n");
57         
58         /*
59          * isDynamic means that the content list will be pulled from lucene.
60          */

61         if (isDynamic) {
62             String JavaDoc luceneQuery = container.getLuceneQuery();
63             sb.append("#set ($CONTENTS_PER_PAGE = \"$CONTAINER_MAX_CONTENTLETS\")\n");
64             sb.append("#if ($request.getParameter(\"cont_" + identifier.getInode() + "_per_page\"))\n");
65             sb.append(" #set ($CONTENTS_PER_PAGE = $request.getParameter(\"cont_" + identifier.getInode() + "_per_page\"))\n");
66             sb.append("#end\n");
67             sb.append("#set ($CURRENT_PAGE = \"1\")\n");
68             sb.append("#if ($request.getParameter(\"cont_" + identifier.getInode() + "_page\"))\n");
69             sb.append(" #set ($CURRENT_PAGE = $request.getParameter(\"cont_" + identifier.getInode() + "_page\"))\n");
70             sb.append("#end\n");
71             sb.append("#set ($LUCENE_QUERY = \"" + luceneQuery + "\")\n");
72         }
73         
74         // if the container needs to get its contentlets
75
if (container.getMaxContentlets() > 0) {
76             sb.append("#if($EDIT_MODE)\n");
77                 // To edit the look, see WEB-INF/velocity/static/preview/container_controls.vtl
78
sb.append("<div class='container'>\n");
79             sb.append("#end\n");
80             
81             // pre loop if it exists
82
if(UtilMethods.isSet(container.getPreLoop())){
83                 sb.append(container.getPreLoop());
84             }
85             
86             //let's do the search of contentlets using lucene query
87
if (isDynamic) {
88                 Structure containerStructure = (Structure) InodeFactory.getParentOfClass(
89                         container, Structure.class);
90
91                 sb.append("#set ($contentletResultsMap" + identifier.getInode() +
92                         " = $contents.searchWithLuceneQuery(\""+ containerStructure.getInode() +"\", " +
93                                 "\"$LUCENE_QUERY\", " +
94                                 "\"$SORT_PAGE\", " +
95                                 "$CURRENT_PAGE, $CONTENTS_PER_PAGE))\n");
96                 sb.append("#set ($contentletList" + identifier.getInode() +
97                         " = $contents.getContentsPathsFromLuceneHits($contentletResultsMap" + identifier.getInode() + ".get(\"assets\")," +
98                         (EDIT_MODE?"true":"false")+"))\n");
99                 
100                 sb.append("#set ($HAS_NEXT_PAGE = $contentletResultsMap" + identifier.getInode() + ".get(\"has_next_page\"))\n");
101                 sb.append("#set ($HAS_PREVIOUS_PAGE = $contentletResultsMap" + identifier.getInode() + ".get(\"has_previous_page\"))\n");
102                 sb.append("#set ($TOTAL_CONTENTS = $contentletResultsMap" + identifier.getInode() + ".get(\"total_records_int\"))\n");
103                 sb.append("#set ($TOTAL_PAGES = $contentletResultsMap" + identifier.getInode() + ".get(\"total_pages_int\"))\n");
104                 sb.append("#set ($CONTENTLETS = $contentletList" + identifier.getInode() + ")\n");
105                 sb.append("#set ($CONTAINER_NUM_CONTENTLETS = $contentletList" + identifier.getInode() + ".size())\n");
106             }
107                         
108             sb.append("\n#foreach ($contentletId in $contentletList" + identifier.getInode() + ")\n");
109             if (EDIT_MODE) {
110                 sb.append("\n#if($webapi.canParseContent($contentletId))\n");
111             }
112             
113                 sb.append("\n#parse($contentletId)\n");
114                 
115                 sb.append("#if($EDIT_MODE)\n");
116                     sb.append("<div class=\"contentlet\">");
117                 sb.append("#end\n");
118                 sb.append(container.getCode());
119                 sb.append("#if($EDIT_MODE)\n");
120                     sb.append("#parse('static/preview_mode/content_controls.vtl')\n");
121                     sb.append("<div class=\"clear\"></div></div> ");
122                 sb.append("#end\n");
123             if (EDIT_MODE) {
124                 sb.append("\n#else\n");
125                     sb.append("#set($CONTENT_INODE =\"$webapi.getContentInode($contentletId)\")");
126                     sb.append("#set($EDIT_CONTENT_PERMISSION =\"$webapi.getContentPermissions($contentletId)\")");
127
128                     sb.append("<div class=\"contentlet\">");
129                     sb.append(" Content Parse Error. Check your Content Code. ");
130                     sb.append("#parse('static/preview_mode/content_controls.vtl')");
131                     sb.append("<div class=\"clear\"></div></div> ");
132                 sb.append("\n#end\n");
133                 
134             }
135                 
136             sb.append("\n#end\n");
137             
138             // post loop if it exists
139
if(UtilMethods.isSet(container.getPostLoop())){
140                 sb.append(container.getPostLoop());
141             }
142             //close our container preview mode div
143
sb.append("#if($EDIT_MODE)\n");
144                 sb.append("#parse('static/preview_mode/container_controls.vtl')");
145                 sb.append("</div>");
146             sb.append("#end\n");
147             
148         }
149         else {
150             
151             
152             
153             sb.append(container.getCode());
154         }
155         try {
156             int bytedata;
157             String JavaDoc folderPath = (!EDIT_MODE) ? "live" + File.separator: "working" + File.separator;
158             String JavaDoc velocityRootPath = Config.getStringProperty("VELOCITY_ROOT");
159             if (velocityRootPath.startsWith("/WEB-INF")) {
160                 velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath);
161             }
162             velocityRootPath += File.separator;
163             
164             java.io.BufferedOutputStream JavaDoc tmpOut = new java.io.BufferedOutputStream JavaDoc(new java.io.FileOutputStream JavaDoc(
165                     new java.io.File JavaDoc(velocityRootPath + folderPath + identifier.getInode() + "." + Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION"))));
166
167             java.io.StringReader JavaDoc strReader = new java.io.StringReader JavaDoc(sb.toString());
168
169             //writes all data from input in output
170
while ((bytedata = strReader.read()) != -1) {
171                 tmpOut.write(bytedata);
172             }
173             tmpOut.close();
174             strReader.close();
175         } catch (Exception JavaDoc e) {
176             Logger.error(ContentletServices.class, e.toString(), e);
177         }
178         
179     }
180     
181     public static void unpublishContainerFile(Container container) {
182
183         Identifier identifier = IdentifierFactory.getParentIdentifier(container);
184         removeContainerFile(container, identifier, false);
185     }
186     
187     public static void removeContainerFile(Container container, boolean EDIT_MODE) {
188
189         Identifier identifier = IdentifierFactory.getParentIdentifier(container);
190         removeContainerFile(container, identifier, EDIT_MODE);
191     }
192     
193     public static void removeContainerFile (Container container, Identifier identifier, boolean EDIT_MODE) {
194         String JavaDoc folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator;
195         String JavaDoc velocityRootPath = Config.getStringProperty("VELOCITY_ROOT");
196         if (velocityRootPath.startsWith("/WEB-INF")) {
197             velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath);
198         }
199         velocityRootPath += java.io.File.separator;
200         java.io.File JavaDoc f = new java.io.File JavaDoc(velocityRootPath + folderPath +
201                 identifier.getInode() + "." +
202                 Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION"));
203         f.delete();
204     }
205        
206
207 }
Popular Tags