Wiki source code of MoccaCalendarEventValidation
Last modified by Andrey Che on 2018/12/10 14:09
Show last authors
1 | import com.xpn.xwiki.validation.*; |
2 | |
3 | import com.xpn.xwiki.XWikiContext; |
4 | import com.xpn.xwiki.objects.BaseObject; |
5 | import com.xpn.xwiki.doc.XWikiDocument; |
6 | |
7 | public class Val implements XWikiValidationInterface { |
8 | private String MoccaCalendarEventClass = "MoccaCalendar.MoccaCalendarEventClass"; |
9 | |
10 | |
11 | public boolean validateDocument(XWikiDocument doc, XWikiContext context) { |
12 | try{ |
13 | |
14 | def classDocumentReference = doc.resolveClassReference(MoccaCalendarEventClass); |
15 | |
16 | BaseObject obj = doc.getXObject(classDocumentReference, 0); |
17 | if (obj == null) { |
18 | return true; |
19 | } |
20 | Date startDate = obj.getDateValue("startDate"); |
21 | Date endDate = obj.getDateValue("endDate"); |
22 | String title = obj.getStringValue("title"); |
23 | // boolean allDay = obj.getIntValue("allDay") == 1 |
24 | |
25 | boolean valid = true; |
26 | if(startDate == null) { |
27 | obj.setDateValue("startDate",new Date()); |
28 | startDate=obj.getDateValue("startDate"); |
29 | } |
30 | |
31 | if(endDate!=null && startDate !=null) { |
32 | if(endDate.getTime()<startDate.getTime()) { |
33 | valid = false; |
34 | XWikiValidationStatus.addErrorToContext(MoccaCalendarEventClass, "endDate", "End Date", "val_endDate", context); |
35 | } |
36 | } |
37 | |
38 | com.xpn.xwiki.web.XWikiRequest request = context.getRequest(); |
39 | String action = request.getParameter("ocalcaction"); |
40 | if("create".equals(action)) { |
41 | if(!doc.isNew()) { |
42 | valid = false; |
43 | XWikiValidationStatus.addErrorToContext(MoccaCalendarEventClass, "title", "Title", "val_title_already_exists", context); |
44 | } |
45 | } |
46 | return valid; |
47 | }catch(Exception e) { |
48 | System.out.println("VALIDATION EXCEPTION"); |
49 | e.printStackTrace(); |
50 | throw e; |
51 | } |
52 | } |
53 | |
54 | public boolean validateObject(BaseObject object, XWikiContext context) { |
55 | return true; |
56 | } |
57 | |
58 | } |