Iterator<Field> fieldsIt=fields.iterator();
String widgetCode="";
while (fieldsIt.hasNext()) {
Field field=(Field) fieldsIt.next();
String contField=field.getFieldContentlet();
String contFieldValue=null;
Object contFieldValueObject=null;
FieldAPI fdAPI=APILocator.getFieldAPI();
String velPath=(!EDIT_MODE) ? "live/" : "working/";
if(fdAPI.isElementConstant(field)){
if(field.getVelocityVarName().equals("widgetPreexecute")){
continue;
}
if(field.getVelocityVarName().equals("widgetCode")) {
widgetCode="#set($_dummy=$!dotcms_content_" + content.getIdentifier() + ".put(\"" + field.getVelocityVarName() + "\", $velutil.mergeTemplate(\"" + velPath + content.getInode() + "_" + field.getInode() + "." + Config.getStringProperty("VELOCITY_FIELD_EXTENSION") + "\")))";
continue;
}else{
String fv=field.getValues()!=null ? field.getValues() : "";
if(fv.contains("$") || fv.contains("#")){
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("\", $velutil.mergeTemplate(\"").append(velPath).append(content.getInode()).append("_").append(field.getInode()).append(".").append(Config.getStringProperty("VELOCITY_FIELD_EXTENSION")).append("\")))");
}else{
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("\", \"").append(UtilMethods.espaceForVelocity(field.getValues()).trim()).append("\"))");
}
continue;
}
}
if (UtilMethods.isSet(contField)) {
try {
contFieldValueObject=conAPI.getFieldValue(content, field);
contFieldValue=contFieldValueObject == null ? "" : contFieldValueObject.toString();
} catch (Exception e) {
Logger.error(ContentletMapServices.class, "writeContentletToFile: " + e.getMessage());
}
if (!field.getFieldType().equals(Field.FieldType.DATE_TIME.toString()) && !field.getFieldType().equals(Field.FieldType.DATE.toString())
&& !field.getFieldType().equals(Field.FieldType.TIME.toString())) {
if (fdAPI.isNumeric(field)) {
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("\", ").append(contFieldValue).append("))");
} else {
if(contFieldValue.contains("$") || contFieldValue.contains("#")){
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("\", $velutil.mergeTemplate(\"").append(velPath).append(content.getInode()).append("_").append(field.getInode()).append(".").append(Config.getStringProperty("VELOCITY_FIELD_EXTENSION")).append("\")))");
}else{
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("\", \"").append(UtilMethods.espaceForVelocity(contFieldValue).trim()).append("\"))");
}
}
}
}
if (field.getFieldType().equals(Field.FieldType.TEXT.toString()) || field.getFieldType().equals(Field.FieldType.TEXT_AREA.toString())
|| field.getFieldType().equals(Field.FieldType.WYSIWYG.toString())) {
} else if (field.getFieldType().equals(Field.FieldType.IMAGE.toString())) {
String identifierValue=content.getStringProperty(field.getVelocityVarName());
if( InodeUtils.isSet(identifierValue) ) {
if (EDIT_MODE){
sb.append("#set($").append(field.getVelocityVarName()).append("=$filetool.getFile('").append(identifierValue).append("',false))");
}else{
sb.append("#set($").append(field.getVelocityVarName()).append("=$filetool.getFile('").append(identifierValue).append("',true))");
}
}else{
sb.append("#set($").append(field.getVelocityVarName()).append("=$filetool.getNewFile())");
}
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ImageInode\", $").append(field.getVelocityVarName()).append(".getInode() ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ImageIdentifier\", $").append(field.getVelocityVarName()).append(".getIdentifier() ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ImageWidth\", $").append(field.getVelocityVarName()).append(".getWidth() ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ImageHeight\", $").append(field.getVelocityVarName()).append(".getHeight() ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ImageExtension\", $UtilMethods.espaceForVelocity($").append(field.getVelocityVarName()).append(".getExtension()) ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ImageURI\", $filetool.getURI($").append(field.getVelocityVarName()).append(") ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ImageTitle\", $UtilMethods.espaceForVelocity($").append(field.getVelocityVarName()).append(".getTitle()) ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ImageFriendlyName\", $UtilMethods.espaceForVelocity($").append(field.getVelocityVarName()).append(".getFriendlyName()) ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ImagePath\", $UtilMethods.espaceForVelocity($").append(field.getVelocityVarName()).append(".getPath()) ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ImageName\", $UtilMethods.espaceForVelocity($").append(field.getVelocityVarName()).append(".getFileName()) ))");
} else if (field.getFieldType().equals(Field.FieldType.FILE.toString())) {
String identifierValue=content.getStringProperty(field.getVelocityVarName());
if( InodeUtils.isSet(identifierValue) ) {
if (EDIT_MODE){
sb.append("#set( $").append(field.getVelocityVarName()).append("Object=$filetool.getFile('").append(identifierValue).append("',false))");
}else{
sb.append("#set( $").append(field.getVelocityVarName()).append("Object=$filetool.getFile('").append(identifierValue).append("',true))");
}
}else{
sb.append("#set( $").append(field.getVelocityVarName()).append("Object=$filetool.getNewFile())");
}
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("FileInode\", $").append(field.getVelocityVarName()).append("Object.getInode() ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("FileIdentifier\", $").append(field.getVelocityVarName()).append("Object.getIdentifier() ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("FileFriendlyName\", $UtilMethods.espaceForVelocity($").append(field.getVelocityVarName()).append("Object.getFriendlyName()) ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("FileExtension\", $UtilMethods.espaceForVelocity($").append(field.getVelocityVarName()).append("Object.getExtension()) ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("FileURI\", $filetool.getURI($").append(field.getVelocityVarName()).append("Object) ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("FileTitle\", $").append(field.getVelocityVarName()).append("Object.getTitle() ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("FilePath\", $UtilMethods.espaceForVelocity($").append(field.getVelocityVarName()).append("Object.getPath()) ))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("FileName\", $UtilMethods.espaceForVelocity($").append(field.getVelocityVarName()).append("Object.getFileName()) ))");
} //http://jira.dotmarketing.net/browse/DOTCMS-2178
else if (field.getFieldType().equals(Field.FieldType.BINARY.toString())) {
java.io.File binFile;
String fileName="";
String filesize="";
try {
binFile=content.getBinary(field.getVelocityVarName());
if(binFile != null) {
fileName=binFile.getName();
filesize=FileUtil.getsize(binFile);
}
} catch (IOException e) {
Logger.error(ContentletServices.class, "Unable to retrive binary file for content id " + content.getIdentifier() + " field " + field.getVelocityVarName(), e);
continue;
}
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("BinaryFileTitle\", \"").append(UtilMethods.espaceForVelocity(fileName)).append("\"))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("BinaryFileSize\", \"").append(UtilMethods.espaceForVelocity(filesize)).append("\"))");
String binaryFileURI=fileName.length()>0? UtilMethods.espaceForVelocity("/contentAsset/raw-data/"+content.getIdentifier()+"/"+ field.getVelocityVarName() + "/" + content.getInode()):"";
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("BinaryFileURI\", \"").append(binaryFileURI).append("\"))");
} else if (field.getFieldType().equals(Field.FieldType.SELECT.toString())) {
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("SelectLabelsValues\", \""
).append( field.getValues().replaceAll("\\r\\n", " ").replaceAll("\\n", " ")).append("\"))");
} else if (field.getFieldType().equals(Field.FieldType.RADIO.toString())) {
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("RadioLabelsValues\", \""
).append(field.getValues().replaceAll("\\r\\n", " ").replaceAll("\\n", " ")).append("\"))");
} else if (field.getFieldType().equals(Field.FieldType.CHECKBOX.toString())) {
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("CheckboxLabelsValues\", \""
).append(field.getValues().replaceAll("\\r\\n", " ").replaceAll("\\n", " ")).append("\"))");
} else if (field.getFieldType().equals(Field.FieldType.DATE.toString())) {
String shortFormat="";
String dbFormat="";
if (contFieldValueObject != null && contFieldValueObject instanceof Date) {
shortFormat=UtilMethods.dateToHTMLDate((Date) contFieldValueObject, "MM/dd/yyyy");
dbFormat=UtilMethods.dateToHTMLDate((Date) contFieldValueObject, "yyyy-MM-dd");
}
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("\", $date.toDate(\"yyyy-MM-dd\", \"").append(dbFormat).append("\")))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ShortFormat\", \"").append(shortFormat).append("\"))");
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("DBFormat\", \"").append(dbFormat).append("\"))");
} else if (field.getFieldType().equals(Field.FieldType.TIME.toString())) {
String shortFormat="";
if (contFieldValueObject != null && contFieldValueObject instanceof Date) {
shortFormat=UtilMethods.dateToHTMLDate((Date) contFieldValueObject, "H:mm:ss");
}
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("\", $date.toDate(\"H:mm:ss\", \"").append(shortFormat).append("\")))");
sb.append("#set($_dummy=$!dotcms_content_").append( content.getIdentifier()).append(".put(\"").append(field.getVelocityVarName()).append("ShortFormat\", \"").append(shortFormat).append("\"))");
} else if (field.getFieldType().equals(Field.FieldType.DATE_TIME.toString())) {
String shortFormat="";
String longFormat="";
String dbFormat="";
if (contFieldValueObject != null && contFieldValueObject instanceof Date) {
shortFormat=UtilMethods.dateToHTMLDate((Date) contFieldValueObject, "MM/dd/yyyy");
longFormat=UtilMethods.dateToHTMLDate((Date) contFieldValueObject, "MM/dd/yyyy H:mm:ss");
dbFormat=UtilMethods.dateToHTMLDate((Date) contFieldValueObject, "yyyy-MM-dd H:mm:ss");
}
sb.append("#set($_dummy=$!dotcms_content_").append(content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "\", $date.toDate(\"yyyy-MM-dd H:mm:ss\", \"" ).append( dbFormat ).append( "\")))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "ShortFormat\", \"" ).append( shortFormat ).append( "\"))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "DBFormat\", \"" ).append( dbFormat ).append( "\"))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "LongFormat\", \"" ).append( longFormat ).append( "\"))");
} else if (field.getFieldType().equals(Field.FieldType.BUTTON.toString())) {
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "ButtonValue\", \"" ).append( (field.getFieldName() == null ? "" : field.getFieldName())
).append( "\"))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "ButtonCode\", \"" ).append( (field.getValues() == null ? "" : field.getValues()) ).append( "\"))");
} else if (field.getFieldType().equals(Field.FieldType.CATEGORY.toString())) {
// Get the Category Field
Category category=categoryAPI.find(field.getValues(), systemUser, false);
// Get all the Contentlets Categories
List<Category> selectedCategories=categoryAPI.getParents(content, systemUser, false);
// Initialize variables
String catInodes="";
Set<Category> categoryList=new HashSet<Category>();
List<Category> categoryTree=categoryAPI.getAllChildren(category, systemUser, false);
if (selectedCategories.size() > 0 && categoryTree != null) {
for (int k=0; k < categoryTree.size(); k++) {
Category cat=(Category) categoryTree.get(k);
for (Category categ : selectedCategories) {
if (categ.getInode().equalsIgnoreCase(cat.getInode())) {
categoryList.add(cat);
}
}
}
}
if (categoryList.size() > 0) {
Iterator<Category> it=categoryList.iterator();
StringBuilder catbuilder=new StringBuilder();
while (it.hasNext()) {
Category cat=(Category) it.next();
catbuilder.append("\"").append(cat.getInode()).append("\"") ;
if (it.hasNext()) {
catbuilder.append(",");
}
}
catInodes=catbuilder.toString();
sb.append("#set($catobjects=$categories.filterCategoriesByUserPermissions([" ).append( catInodes ).append( "]))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "CategoryObjects\", $catobjects))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "Categories\", $categories.fetchCategoriesInodes($catobjects)))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "\", $categories.fetchCategoriesInodes($catobjects)))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "CategoriesNames\", $categories.fetchCategoriesNames($catobjects)))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "CategoriesKeys\", $categories.fetchCategoriesKeys($catobjects)))");
}
else {
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "CategoryObjects\", $contents.getEmptyList()))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "Categories\", $contents.getEmptyList()))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "\", $contents.getEmptyList()))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "CategoriesNames\", $contents.getEmptyList()))");
sb.append("#set($_dummy=$!dotcms_content_" ).append( content.getIdentifier() ).append( ".put(\"" ).append( field.getVelocityVarName() ).append( "CategoriesKeys\", $contents.getEmptyList()))");
}
}
}