MoccaCalendar - MoccaCalendarEventValidation 
Page of - last modified by Andrey Che on 2018/12/10 14:09 
MoccaCalendarEventValidation 

Andrey Che

2018/12/10 14:09
Table of Contents

import com.xpn.xwiki.validation.*;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.doc.XWikiDocument;
public class Val implements XWikiValidationInterface  {
 private String MoccaCalendarEventClass = "MoccaCalendar.MoccaCalendarEventClass";
  
 public boolean validateDocument(XWikiDocument doc, XWikiContext context) {
  try{
  
   def classDocumentReference = doc.resolveClassReference(MoccaCalendarEventClass);
 
   BaseObject obj = doc.getXObject(classDocumentReference, 0);
   if (obj == null) {
    return true;
   }
   Date startDate = obj.getDateValue("startDate");
   Date endDate = obj.getDateValue("endDate");
   String title = obj.getStringValue("title");
   boolean allDay = obj.getIntValue("allDay") == 1
 
   boolean valid = true;
   if(startDate == null) {
                                obj.setDateValue("startDate",new Date());
                                startDate=obj.getDateValue("startDate");
   }
 
   if(endDate!=null && startDate !=null) {
    if(endDate.getTime()<startDate.getTime()) {
     valid = false;
     XWikiValidationStatus.addErrorToContext(MoccaCalendarEventClass, "endDate", "End Date", "val_endDate", context);
    }
   }
  
   com.xpn.xwiki.web.XWikiRequest request = context.getRequest();
   String action = request.getParameter("ocalcaction");
   if("create".equals(action)) {
    if(!doc.isNew()) {
     valid = false;
     XWikiValidationStatus.addErrorToContext(MoccaCalendarEventClass, "title", "Title", "val_title_already_exists", context);
    }
   }
   return valid;
  }catch(Exception e) {
   System.out.println("VALIDATION EXCEPTION");
   e.printStackTrace();
   throw e;
  }
 }
 public boolean validateObject(BaseObject object, XWikiContext context) {
  return true;
 }
}