1 package com.dotmarketing.services; 2 3 import java.util.Date ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 import org.apache.commons.beanutils.PropertyUtils; 8 9 import com.dotmarketing.beans.Identifier; 10 import com.dotmarketing.cache.FieldsCache; 11 import com.dotmarketing.factories.IdentifierFactory; 12 import com.dotmarketing.factories.InodeFactory; 13 import com.dotmarketing.portlets.categories.model.Category; 14 import com.dotmarketing.portlets.contentlet.model.Contentlet; 15 import com.dotmarketing.portlets.files.model.File; 16 import com.dotmarketing.portlets.links.model.Link; 17 import com.dotmarketing.portlets.structure.model.Field; 18 import com.dotmarketing.util.Config; 19 import com.dotmarketing.util.Logger; 20 import com.dotmarketing.util.UtilMethods; 21 import com.dotmarketing.util.WebKeys; 22 23 26 public class ContentletMapServices { 27 28 public static void publishContentletMapToFile(Contentlet contentlet) { 29 30 writeContentletMapToFile(contentlet, false); 31 } 32 33 public static void writeContentletMapToFile(Contentlet content, boolean EDIT_MODE) { 34 if (content.getInode() == 0 || content.getIdentifier() == 0) { 35 return; 36 } 37 StringBuffer sb = new StringBuffer (); 39 40 sb.append("#set( $content = ${contents.getEmptyMap()})\n"); 44 sb.append("$!content.put(\"permission\", $EDIT_CONTENT_PERMISSION" + content.getIdentifier() + " )\n"); 45 sb.append("$!content.put(\"inode\", \"" + content.getInode() + "\" )\n"); 46 sb.append("$!content.put(\"identifier\", \"" + content.getIdentifier() + "\" )\n"); 47 sb.append("$!content.put(\"contentTitle\", \"" + UtilMethods.espaceForVelocity(content.getTitle()) + "\" )\n"); 48 49 List fields = FieldsCache.getFieldsByStructureInode(content.getStructureInode()); 51 Iterator fieldsIt = fields.iterator(); 52 53 while (fieldsIt.hasNext()) { 54 Field field = (Field) fieldsIt.next(); 55 56 sb.append("\n\n##Set Field " + field.getFieldName() + " properties\n"); 57 58 String contField = field.getFieldContentlet(); 59 String contFieldValue = null; 60 Object contFieldValueObject = null; 61 if (UtilMethods.isSet(contField)) { 62 try { 63 contFieldValueObject = PropertyUtils.getProperty(content, contField); 64 contFieldValue = contFieldValueObject == null ? "" : contFieldValueObject.toString(); 65 } catch (Exception e) { 66 Logger.error(ContentletMapServices.class, "writeContentletToFile: " + e.getMessage()); 67 } 68 if (!field.getFieldType().equals(WebKeys.TypeField.DATETIME) && 69 !field.getFieldType().equals(WebKeys.TypeField.DATE) && 70 !field.getFieldType().equals(WebKeys.TypeField.TIME)) { 71 sb.append("$!content.put(\"" + field.getVelocityVarName() + "\", \"" + UtilMethods.espaceForVelocity(contFieldValue).trim() + "\")\n"); 72 } 73 74 } 75 76 if (field.getFieldType().equals(WebKeys.TypeField.TEXT) || field.getFieldType().equals(WebKeys.TypeField.TEXTAREA) 77 || field.getFieldType().equals(WebKeys.TypeField.WYSIWYG)) { 78 83 } else if (field.getFieldType().equals(WebKeys.TypeField.IMAGE)) { 84 Identifier id = (Identifier) InodeFactory.getChildOfClassByRelationType(content, Identifier.class, field.getFieldRelationType()); 85 File image = (File) IdentifierFactory.getLiveChildOfClass(id, File.class); 86 sb.append("$!content.put(\"" + field.getVelocityVarName() + "ImageInode\", \"" + image.getInode() + "\" )\n"); 87 sb.append("$!content.put(\"" + field.getVelocityVarName() + "ImageWidth\", \"" + image.getWidth() + "\" )\n"); 88 sb.append("$!content.put(\"" + field.getVelocityVarName() + "ImageHeight\", \"" + image.getHeight() + "\" )\n"); 89 sb.append("$!content.put(\"" + field.getVelocityVarName() + "ImageExtension\", \"" 90 + UtilMethods.espaceForVelocity(image.getExtension()) + "\" )\n"); 91 sb.append("$!content.put(\"" + field.getVelocityVarName() + "ImageURI\", \"" + UtilMethods.espaceForVelocity(image.getURI()) 92 + "\" )\n"); 93 sb.append("$!content.put(\"" + field.getVelocityVarName() + "ImageTitle\", \"" + UtilMethods.espaceForVelocity(image.getTitle()) 94 + "\" )\n"); 95 96 } else if (field.getFieldType().equals(WebKeys.TypeField.FILE)) { 97 Identifier id = (Identifier) InodeFactory.getChildOfClassByRelationType(content, Identifier.class, field.getFieldRelationType()); 98 File file = (File) IdentifierFactory.getLiveChildOfClass(id, File.class); 99 sb.append("$!content.put(\"" + field.getVelocityVarName() + "FileInode\", \"" + file.getInode() + "\" )\n"); 100 sb.append("$!content.put(\"" + field.getVelocityVarName() + "FileExtension\", \"" 101 + UtilMethods.espaceForVelocity(file.getExtension()) + "\" )\n"); 102 sb 103 .append("$!content.put(\"" + field.getVelocityVarName() + "FileURI\", \"" + UtilMethods.espaceForVelocity(file.getURI()) 104 + "\" )\n"); 105 sb.append("$!content.put(\"" + field.getVelocityVarName() + "FileTitle\", \"" + UtilMethods.espaceForVelocity(file.getTitle()) 106 + "\" )\n"); 107 108 } else if (field.getFieldType().equals(WebKeys.TypeField.LINK)) { 109 Identifier id = (Identifier) InodeFactory.getChildOfClassByRelationType(content, Identifier.class, field.getFieldRelationType()); 110 Link link = (Link) IdentifierFactory.getLiveChildOfClass(id, Link.class); 111 sb.append("$!content.put(\"" + field.getVelocityVarName() + "LinkInode\", \"" + link.getInode() + "\" )\n"); 112 sb 113 .append("$!content.put(\"" + field.getVelocityVarName() + "LinkURL\", \"" + UtilMethods.espaceForVelocity(link.getUrl()) 114 + "\" )\n"); 115 sb.append("$!content.put(\"" + field.getVelocityVarName() + "LinkProtocol\", \"" + UtilMethods.espaceForVelocity(link.getProtocal()) 116 + "\" )\n"); 117 sb.append("$!content.put(\"" + field.getVelocityVarName() + "LinkTitle\", \"" + UtilMethods.espaceForVelocity(link.getTitle()) 118 + "\" )\n"); 119 sb.append("$!content.put(\"" + field.getVelocityVarName() + "LinkTarget\", \"" + link.getTarget() + "\" )\n"); 120 121 } else if (field.getFieldType().equals(WebKeys.TypeField.SELECT)) { 122 sb.append("$!content.put(\"" + field.getVelocityVarName() + "SelectLabelsValues\", \"" 123 + field.getValues().replaceAll("\\r\\n", " ").replaceAll("\\n", " ") + "\")\n"); 124 125 } else if (field.getFieldType().equals(WebKeys.TypeField.RADIO)) { 126 sb.append("$!content.put(\"" + field.getVelocityVarName() + "RadioLabelsValues\", \"" 127 + field.getValues().replaceAll("\\r\\n", " ").replaceAll("\\n", " ") + "\")\n"); 128 129 } else if (field.getFieldType().equals(WebKeys.TypeField.CHECKBOX)) { 130 sb.append("$!content.put(\"" + field.getVelocityVarName() + "CheckboxLabelsValues\", \"" 131 + field.getValues().replaceAll("\\r\\n", " ").replaceAll("\\n", " ") + "\")\n"); 132 133 } else if (field.getFieldType().equals(WebKeys.TypeField.DATE)) { 134 String shortFormat = ""; 135 String dbFormat = ""; 136 if (contFieldValueObject != null && contFieldValueObject instanceof Date ) { 137 shortFormat = UtilMethods.dateToHTMLDate((Date ) contFieldValueObject, "MM/dd/yyyy"); 138 dbFormat = UtilMethods.dateToHTMLDate((Date ) contFieldValueObject, "yyyy-MM-dd"); 139 } 140 sb.append("$!content.put(\"" + field.getVelocityVarName() + "\", $date.toDate(\"yyyy-MM-dd\", \"" + dbFormat + "\"))\n"); 141 sb.append("$!content.put(\"" + field.getVelocityVarName() + "ShortFormat\", \"" + shortFormat + "\")\n"); 142 sb.append("$!content.put(\"" + field.getVelocityVarName() + "DBFormat\", \"" + dbFormat + "\")\n"); 143 } else if (field.getFieldType().equals(WebKeys.TypeField.TIME)) { 144 String shortFormat = ""; 145 if (contFieldValueObject != null && contFieldValueObject instanceof Date ) { 146 shortFormat = UtilMethods.dateToHTMLDate((Date ) contFieldValueObject, "H:mm:ss"); 147 } 148 sb.append("$!content.put(\"" + field.getVelocityVarName() + "\", $date.toDate(\"H:mm:ss\", \"" + shortFormat + "\"))\n"); 149 sb.append("$!content.put(\"" + field.getVelocityVarName() + "\", \"" + shortFormat + "\")\n"); 150 151 } else if (field.getFieldType().equals(WebKeys.TypeField.DATETIME)) { 152 String shortFormat = ""; 153 String longFormat = ""; 154 String dbFormat = ""; 155 if (contFieldValueObject != null && contFieldValueObject instanceof Date ) { 156 shortFormat = UtilMethods.dateToHTMLDate((Date ) contFieldValueObject, "MM/dd/yyyy"); 157 longFormat = UtilMethods.dateToHTMLDate((Date ) contFieldValueObject, "MM/dd/yyyy H:mm:ss"); 158 dbFormat = UtilMethods.dateToHTMLDate((Date ) contFieldValueObject, "yyyy-MM-dd H:mm:ss"); 159 } 160 161 sb.append("$!content.put(\"" + field.getVelocityVarName() + "\", $date.toDate(\"yyyy-MM-dd H:mm:ss\", \"" + dbFormat + "\"))\n"); 162 sb.append("$!content.put(\"" + field.getVelocityVarName() + "ShortFormat\", \"" + shortFormat + "\")\n"); 163 sb.append("$!content.put(\"" + field.getVelocityVarName() + "DBFormat\", \"" + dbFormat + "\")\n"); 164 sb.append("$!content.put(\"" + field.getVelocityVarName() + "LongFormat\", \"" + longFormat + "\")\n"); 165 166 } else if (field.getFieldType().equals(WebKeys.TypeField.BUTTON)) { 167 sb.append("$!content.put(\"" + field.getVelocityVarName() + "ButtonValue\", \"" 168 + (field.getFieldName() == null ? "" : field.getFieldName()) + "\")\n"); 169 sb.append("$!content.put(\"" + field.getVelocityVarName() + "ButtonCode\", \"" + (field.getValues() == null ? "" : field.getValues()) 170 + "\")\n"); 171 172 } 173 174 } 175 176 String categories = ""; 177 String categoryNames = ""; 178 List categoryList = InodeFactory.getParentsOfClass(content, Category.class); 180 if (categoryList != null) { 181 for (int i = 0; i < categoryList.size(); i++) { 182 Category category = (Category) categoryList.get(i); 183 categories += category.getInode(); 184 categoryNames += "\"" + category.getCategoryName() + "\""; 185 if (i != (categoryList.size() - 1)) { 186 categories += ","; 187 categoryNames += ","; 188 } 189 } 190 } 191 192 sb.append("$!content.put(\"ContentletCategories\", [" + categories + "] )\n"); 195 196 199 try { 200 int bytedata; 201 202 String folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; 203 String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); 204 if (velocityRootPath.startsWith("/WEB-INF")) { 205 velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath); 206 } 207 velocityRootPath += java.io.File.separator; 208 209 java.io.BufferedOutputStream tmpOut = new java.io.BufferedOutputStream (new java.io.FileOutputStream (new java.io.File ( 210 velocityRootPath + folderPath + content.getIdentifier() + "_" + content.getLanguageId() + "." 211 + Config.getStringProperty("VELOCITY_CONTENT_MAP_EXTENSION")))); 212 213 java.io.StringReader strReader = new java.io.StringReader (sb.toString()); 214 215 while ((bytedata = strReader.read()) != -1) { 217 tmpOut.write(bytedata); 218 } 219 220 tmpOut.close(); 221 strReader.close(); 222 223 } catch (Exception e) { 224 Logger.error(ContentletMapServices.class, e.toString(), e); 225 } 226 227 } 228 229 public static void unpublishContentletMapFile(Contentlet asset) { 230 231 Identifier identifier = IdentifierFactory.getParentIdentifier(asset); 232 removeContentletMapFile(asset, identifier, false); 233 } 234 235 public static void removeContentletMapFile(Contentlet asset, boolean EDIT_MODE) { 236 237 Identifier identifier = IdentifierFactory.getParentIdentifier(asset); 238 removeContentletMapFile(asset, identifier, EDIT_MODE); 239 } 240 241 public static void removeContentletMapFile(Contentlet asset, Identifier identifier, boolean EDIT_MODE) { 242 String folderPath = (!EDIT_MODE) ? "live/" : "working/"; 243 java.io.File f = new java.io.File (Config.CONTEXT.getRealPath("/WEB-INF/velocity/" + folderPath + identifier.getInode() + "." 244 + Config.getStringProperty("VELOCITY_CONTENT_MAP_EXTENSION"))); 245 f.delete(); 246 } 247 248 } | Popular Tags |