boolean isAutoSave, boolean isCheckin) throws LanguageException,
PortalException, SystemException, DotDataException,
DotSecurityException, java.text.ParseException {
HibernateUtil.startTransaction();
ContentletWebAPI contentletWebAPI = WebAPILocator.getContentletWebAPI();
int tempCount = 0;// To store multiple values opposite to a name. Ex: selected permissions & categories
String newInode = "";
String referer = "";
String language = "";
String strutsAction = "";
String recurrenceDaysOfWeek="";
Map<String,Object> contentletFormData = new HashMap<String,Object>();
Map<String,Object> callbackData = new HashMap<String,Object>();
List<String> saveContentErrors = new ArrayList<String>();
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = com.liferay.portal.util.PortalUtil.getUser((HttpServletRequest)req);
List<Field> fields = com.dotmarketing.cache.FieldsCache.getFieldsByStructureInode(eventAPI.getEventStructure().getInode());
String titleField = "";
String urlTitleField = "";
String urlTitleFieldValue = "";
String titleFieldValue = "";
for(Field field : fields){
if(field.getVelocityVarName().equals("urlTitle")){
urlTitleField = field.getFieldContentlet();
}
if(field.getVelocityVarName().equals("title")){
titleField = field.getFieldContentlet();
}
if(UtilMethods.isSet(titleField) && UtilMethods.isSet(urlTitleField)){
break;
}
}
// get the struts_action from the form data
for (Iterator<String> iterator = formData.iterator(); iterator.hasNext();) {
String element = iterator.next();
if(element!=null) {
String elementName = element.substring(0, element.indexOf(WebKeys.CONTENTLET_FORM_NAME_VALUE_SEPARATOR));
if (elementName.startsWith("_EXT") && elementName.endsWith("cmd")) {
strutsAction = elementName.substring(0, elementName.indexOf("cmd"));
break;
}
}
}
// Storing form data into map.
for (Iterator<String> iterator = formData.iterator(); iterator.hasNext();) {
String element = iterator.next();
if (!com.dotmarketing.util.UtilMethods.isSet(element))
continue;
String elementName = element.substring(0, element.indexOf(WebKeys.CONTENTLET_FORM_NAME_VALUE_SEPARATOR));
Object elementValue = element.substring(element.indexOf(WebKeys.CONTENTLET_FORM_NAME_VALUE_SEPARATOR) + WebKeys.CONTENTLET_FORM_NAME_VALUE_SEPARATOR.length());
if (element.startsWith(strutsAction))
elementName = elementName.substring(elementName
.indexOf(strutsAction)
+ strutsAction.length());
// Placed increments as Map holds unique keys.
if(elementName.equals("read")
||elementName.equals("write")
||elementName.equals("publish")){
tempCount++;
elementName = "selected_permission_"+tempCount+elementName;
}
if(elementName.equals(titleField)){
titleFieldValue = (String)elementValue;
}
if(elementName.equals(urlTitleField)){
urlTitleFieldValue = (String)elementValue;
}
if(elementName.equals("categories")){
tempCount++;
elementName = elementName+tempCount+"_";
}
//http://jira.dotmarketing.net/browse/DOTCMS-3232
if(elementName.equalsIgnoreCase("hostId")){
callbackData.put("hostOrFolder",true);
}
if(elementName.startsWith("binary")){
String binaryFileValue = (String) elementValue;
if(UtilMethods.isSet(binaryFileValue) && !binaryFileValue.equals("---removed---")){
binaryFileValue = ContentletUtil.sanitizeFileName(binaryFileValue);
File binaryFile = new File(APILocator.getFileAPI().getRealAssetPathTmpBinary()
+ File.separator + user.getUserId() + File.separator + elementName
+ File.separator + binaryFileValue);
if(binaryFile.exists())
binaryFile.delete();
elementValue = binaryFile;
}else{
elementValue = null;
}
}
if(!UtilMethods.isSet(elementName))
continue;
if(elementValue==null)
elementValue="";
if(elementName.equals("referer"))
referer = (String)elementValue;
if(elementName.equals("languageId"))
language = (String)elementValue;
if ( elementName.equals("recurrenceDaysOfWeek")) {
recurrenceDaysOfWeek= recurrenceDaysOfWeek + elementValue+ ",";
}
contentletFormData.put(elementName, elementValue);
}
contentletFormData.put("recurrenceDaysOfWeek", recurrenceDaysOfWeek);
if(!UtilMethods.isSet(urlTitleFieldValue) && UtilMethods.isSet(titleFieldValue)){
urlTitleFieldValue = titleFieldValue.toLowerCase();
urlTitleFieldValue = urlTitleFieldValue.replace("/^\\s+|\\s+$/g","");
urlTitleFieldValue = urlTitleFieldValue.replace("/[^a-zA-Z 0-9]+/g"," ");
urlTitleFieldValue = urlTitleFieldValue.replace("/\\s/g", "-");
while(urlTitleFieldValue.indexOf("--") > -1){
urlTitleFieldValue = urlTitleFieldValue.replace("--","-");
}
contentletFormData.put(urlTitleField, urlTitleFieldValue);
}
String d1 =(String)contentletFormData.get("date1");
String d2 =(String)contentletFormData.get("date2");
String d3 =(String)contentletFormData.get("recurrenceEnds");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
Date eventStartDate = null;
Date eventEndDate = null;
Date recurrenceEndDate= null;
try {
eventStartDate = df.parse(d1);
eventEndDate = df.parse(d2);
if(UtilMethods.isSet(d3)){
recurrenceEndDate = df2.parse(d3);
}
} catch (java.text.ParseException e1) {
}
Boolean cont=true;
if (eventEndDate.before(eventStartDate)){
String errorString = LanguageUtil.get(user,"message.event.endate.before.stardate");
saveContentErrors.add(errorString);
}
if(!contentletFormData.get("recurrenceOccurs").toString().equals("never")) {
if(contentletFormData.get("noEndDate")==null || !Boolean.parseBoolean(contentletFormData.get("noEndDate").toString())){
if (recurrenceEndDate!=null && recurrenceEndDate.before(eventStartDate) ){
String errorString = LanguageUtil.get(user,"message.event.recurrence.endate.before.stardate");
saveContentErrors.add(errorString);
}
}
}
Calendar start = Calendar.getInstance();
start.setTime(eventStartDate);
Calendar end = Calendar.getInstance();
end.setTime(eventEndDate);
if(!contentletFormData.get("recurrenceOccurs").toString().equals("never")) {
if(end.after(start) && (end.get(Calendar.DAY_OF_MONTH) > start.get(Calendar.DAY_OF_MONTH) ||
end.get(Calendar.MONTH) > start.get(Calendar.MONTH) ||
end.get(Calendar.YEAR) > start.get(Calendar.YEAR))){
contentletFormData.put("recurrenceOccurs", "never");
}
}
if (Boolean.parseBoolean(contentletFormData.get("recurrenceChanged").toString())) {
if(!UtilMethods.isSet(contentletFormData.get("recurrenceInterval"))){
String errorString = LanguageUtil.get(user,"message.event.recurrence.invalid.interval");
saveContentErrors.add(errorString);
}else{
try{
Long.valueOf((String)contentletFormData.get("recurrenceInterval"));
}catch(NumberFormatException nfe){
String errorString = LanguageUtil.get(user,"message.event.recurrence.invalid.interval");
saveContentErrors.add(errorString);
}
}
if(contentletFormData.get("recurrenceOccurs").toString().equals("monthly")){
if(Boolean.parseBoolean(contentletFormData.get("isSpecificDate").toString()) &&
!UtilMethods.isSet((String)contentletFormData.get("recurrenceDayOfMonth"))){
String errorString = LanguageUtil.get(user,"message.event.recurrence.invalid.dayofmonth");
saveContentErrors.add(errorString);
}
if(Boolean.parseBoolean(contentletFormData.get("isSpecificDate").toString()) &&
UtilMethods.isSet((String)contentletFormData.get("recurrenceDayOfMonth"))){
try{
Long.valueOf((String)contentletFormData.get("recurrenceDayOfMonth"));
}catch (Exception e) {
String errorString = LanguageUtil.get(user,"message.event.recurrence.invalid.dayofmonth");
saveContentErrors.add(errorString);
}
}else{
contentletFormData.put("recurrenceDayOfMonth", "0");
}
}
if(contentletFormData.get("recurrenceOccurs").toString().equals("annually")){
if(Boolean.parseBoolean(contentletFormData.get("isSpecificDate").toString()) &&
!UtilMethods.isSet((String)contentletFormData.get("specificDayOfMonthRecY")) &&
!UtilMethods.isSet((String)contentletFormData.get("specificMonthOfYearRecY"))){
String errorString = LanguageUtil.get(user,"message.event.recurrence.invalid.date");
saveContentErrors.add(errorString);
}
if(Boolean.parseBoolean(contentletFormData.get("isSpecificDate").toString()) &&
UtilMethods.isSet((String)contentletFormData.get("specificDayOfMonthRecY"))
&& UtilMethods.isSet((String)contentletFormData.get("specificMonthOfYearRecY"))){
try{
Long.valueOf((String)contentletFormData.get("specificDayOfMonthRecY"));
contentletFormData.put("recurrenceDayOfMonth", (String)contentletFormData.get("specificDayOfMonthRecY"));
}catch (Exception e) {
String errorString = LanguageUtil.get(user,"message.event.recurrence.invalid.dayofmonth");
saveContentErrors.add(errorString);
}
try{
Long.valueOf((String)contentletFormData.get("specificMonthOfYearRecY"));
contentletFormData.put("recurrenceMonthOfYear", (String)contentletFormData.get("specificMonthOfYearRecY"));
}catch (Exception e) {
String errorString = LanguageUtil.get(user,"message.event.recurrence.invalid.monthofyear");
saveContentErrors.add(errorString);
}
}else{
contentletFormData.put("recurrenceDayOfMonth", "0");
}
}
}
if(!contentletFormData.get("recurrenceOccurs").toString().equals("never")) {
if(contentletFormData.get("noEndDate")==null || (contentletFormData.get("noEndDate")!=null &&
!Boolean.parseBoolean(contentletFormData.get("noEndDate").toString()))){
if(!UtilMethods.isSet((String)contentletFormData.get("recurrenceEnds"))){
String errorString = LanguageUtil.get(user,"message.event.recurrence.invalid.enddate");
saveContentErrors.add(errorString);
}else{
try{
eventRecurrenceEndDateF.parse((String)contentletFormData.get("recurrenceEnds"));
}catch(Exception e){
String errorString = LanguageUtil.get(user,"message.event.recurrence.invalid.enddate");
saveContentErrors.add(errorString);
}
}
}
}
//http://jira.dotmarketing.net/browse/DOTCMS-6327
if(!contentletFormData.get("recurrenceOccurs").toString().equals("never")) {
if(contentletFormData.get("noEndDate")==null ||
!Boolean.parseBoolean(contentletFormData.get("noEndDate").toString())){
Integer interval = UtilMethods.isSet((String) contentletFormData.get("recurrenceInterval"))?
Integer.valueOf((String) contentletFormData.get("recurrenceInterval")):null;
Integer recurrenceWeekOfMonth =UtilMethods.isSet((String) contentletFormData.get("recurrenceWeekOfMonth"))?
Integer.valueOf((String) contentletFormData.get("recurrenceWeekOfMonth")):null;
Integer recurrenceDayOfWeek =UtilMethods.isSet((String) contentletFormData.get("recurrenceDayOfWeek"))?
Integer.valueOf((String) contentletFormData.get("recurrenceDayOfWeek")):null;
Integer recurrenceMonthOfYear =UtilMethods.isSet((String) contentletFormData.get("recurrenceMonthOfYear"))?
Integer.valueOf((String) contentletFormData.get("recurrenceMonthOfYear")):null;
Integer recurrenceDayOfMonth =UtilMethods.isSet((String) contentletFormData.get("recurrenceDayOfMonth"))?
Integer.valueOf((String) contentletFormData.get("recurrenceDayOfMonth")):null;
Occurrency occurency = Occurrency.findOcurrency((String)contentletFormData.get("recurrenceOccurs"));
if(occurency!=null){
Calendar firstOccurence = RecurrenceUtil.calculateFirstOccurence(eventStartDate,
interval,
occurency,
recurrenceDaysOfWeek,
recurrenceWeekOfMonth,
recurrenceDayOfWeek,
recurrenceMonthOfYear,
recurrenceDayOfMonth);
if (recurrenceEndDate.before(firstOccurence.getTime())){
String errorString = LanguageUtil.get(user,"message.event.recurrence.before.occurence");
saveContentErrors.add(errorString);
}
}
}
}
try {
if(cont &&(saveContentErrors==null || saveContentErrors.isEmpty())){
newInode = contentletWebAPI.saveContent(contentletFormData,isAutoSave,isCheckin,user);
}
}
catch (DotContentletValidationException ve) {
if(ve.hasRequiredErrors()){