// wraps request to get session object
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
HttpServletRequest httpReq = reqImpl.getHttpServletRequest();
//Contentlet Form
ContentletForm cf = (ContentletForm) form;
String cmd = req.getParameter(Constants.CMD);
String inode = req.getParameter("inode");
String inodeStr = (InodeUtils.isSet(inode) ? inode : "");
Contentlet contentlet = new Contentlet();
if(InodeUtils.isSet(inodeStr))
contentlet = conAPI.find(inodeStr, user, false);
req.setAttribute(WebKeys.CONTENTLET_EDIT, contentlet);
Structure structure = contentlet.getStructure();
String selectedStructure = "";
if (InodeUtils.isSet(req.getParameter("selectedStructure"))) {
selectedStructure = req.getParameter("selectedStructure");
structure = (Structure) InodeFactory.getInode(selectedStructure, Structure.class);
contentlet.setStructureInode(structure.getInode());
} else if (cmd.equals("newedit")) {
structure = StructureFactory.getDefaultStructure();
contentlet.setStructureInode(structure.getInode());
}
String langId = req.getParameter("lang");
if(UtilMethods.isSet(langId)) {
try {
contentlet.setLanguageId(Integer.parseInt(langId));
} catch (NumberFormatException e) {
contentlet.setLanguageId(APILocator.getLanguageAPI().getDefaultLanguage().getId());
}
}else{
contentlet.setLanguageId(APILocator.getLanguageAPI().getDefaultLanguage().getId());
}
Map<String, Object> lastSearchMap = new HashMap<String, Object>();
lastSearchMap.put("structure", structure);
lastSearchMap.put("fieldsSearch",new HashMap<String,String>());
lastSearchMap.put("categories",new ArrayList<String>());
lastSearchMap.put("showDeleted",false);
lastSearchMap.put("filterSystemHost",false);
lastSearchMap.put("filterLocked",false);
lastSearchMap.put("page",1);
lastSearchMap.put("orderBy","modDate desc");
httpReq.getSession().setAttribute(WebKeys.CONTENTLET_LAST_SEARCH, lastSearchMap);
// Checking permissions to add new of structure selected
_checkWritePermissions(structure, user, httpReq);
List<Field> list = (List<Field>) FieldsCache.getFieldsByStructureInode(structure.getInode());
for (Field field : list) {
String defaultValue = field.getDefaultValue();
if (UtilMethods.isSet(defaultValue)) {
String typeField = field.getFieldContentlet();
if (typeField.startsWith("bool")) {
boolean defaultValueBoolean = false;
if(defaultValue.equalsIgnoreCase("true") || defaultValue.equalsIgnoreCase("1") || defaultValue.equalsIgnoreCase("yes")
|| defaultValue.equalsIgnoreCase("y") || defaultValue.equalsIgnoreCase("on"))
defaultValueBoolean = true;
contentlet.setBoolProperty(field.getVelocityVarName(), defaultValueBoolean);
} else if (typeField.startsWith("date")) {
if(defaultValue.equals("now"))
contentlet.setDateProperty(field.getVelocityVarName(), new Date());
else {
DateFormat df=null;
final String ft=field.getFieldType();
if(ft.equals(Field.FieldType.DATE.toString()))
df=new SimpleDateFormat("yyyy-MM-dd");
else if(ft.equals(Field.FieldType.DATE_TIME.toString()))
df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
else if(ft.equals(Field.FieldType.TIME.toString()))
df=new SimpleDateFormat("HH:mm:ss");
try {
contentlet.setDateProperty(field.getVelocityVarName(), df.parse(defaultValue));
}
catch(ParseException e) {
// pass it as null
}
}
} else if (typeField.startsWith("float")) {
contentlet.setFloatProperty(field.getVelocityVarName(), Float.parseFloat(defaultValue));
} else if (typeField.startsWith("integer")) {
contentlet.setLongProperty(field.getVelocityVarName(), Long.parseLong(defaultValue));
} else if (typeField.startsWith("text")) {
contentlet.setStringProperty(field.getVelocityVarName(), defaultValue);
}
if (field.getFieldType().equals(Field.FieldType.IMAGE.toString())
|| field.getFieldType().equals(Field.FieldType.FILE.toString())) {
try {
//Identifier id = (Identifier) InodeFactory.getInode((String) value, Identifier.class);
String value=contentlet.getStringProperty(field.getVelocityVarName());
Identifier id = APILocator.getIdentifierAPI().find((String) value);
if (InodeUtils.isSet(id.getInode())) {
if (field.getFieldType().equals(Field.FieldType.IMAGE.toString())) {
File inodeAux = (File) APILocator.getVersionableAPI().findWorkingVersion(id, APILocator.getUserAPI().getSystemUser(), false);
value = inodeAux.getInode();
} else if (field.getFieldType().equals(Field.FieldType.FILE.toString())) {
File inodeAux = (File) APILocator.getVersionableAPI().findWorkingVersion(id, APILocator.getUserAPI().getSystemUser(), false);
value = inodeAux.getInode();
}
contentlet.setStringProperty(field.getVelocityVarName(), value);
}
} catch (Exception ex) {
Logger.debug(this, ex.toString());
}
}
}
}
//Setting review intervals form properties
if (structure.getReviewInterval() != null) {
String interval = structure.getReviewInterval();
Pattern p = Pattern.compile("(\\d+)([dmy])");
Matcher m = p.matcher(interval);
boolean b = m.matches();
if (b) {
cf.setReviewContent(true);
String g1 = m.group(1);
String g2 = m.group(2);
cf.setReviewIntervalNum(g1);
cf.setReviewIntervalSelect(g2);
}
}
}