1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web

File ObjectAddAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

8
29
1
1
116
63
5
0.17
29
1
5

Classes

Class Line # Actions
ObjectAddAction 41 29 0% 5 5
0.868421186.8%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.web;
21   
22    import java.util.Collection;
23    import java.util.Map;
24    import java.util.regex.Pattern;
25   
26    import javax.servlet.http.HttpServletResponse;
27   
28    import org.xwiki.model.EntityType;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.model.reference.EntityReference;
31    import org.xwiki.model.reference.EntityReferenceResolver;
32   
33    import com.xpn.xwiki.XWiki;
34    import com.xpn.xwiki.XWikiContext;
35    import com.xpn.xwiki.XWikiException;
36    import com.xpn.xwiki.doc.XWikiDocument;
37    import com.xpn.xwiki.objects.BaseObject;
38    import com.xpn.xwiki.objects.classes.BaseClass;
39    import com.xpn.xwiki.objects.classes.PropertyClass;
40   
 
41    public class ObjectAddAction extends XWikiAction
42    {
43    private static final String[] EMPTY_PROPERTY = new String[] { "" };
44   
45    /**
46    * A pattern that matches the {@code xobjectNumber} request parameter which is used to pass the number of the added
47    * object to the redirect URL.
48    */
49    private static final Pattern XOBJECT_NUMBER_PARAMETER = Pattern.compile("(\\?|&)xobjectNumber=?(&|#|$)");
50   
51    /**
52    * Used to resolve XClass references.
53    */
54    private EntityReferenceResolver<String> relativeResolver = Utils.getComponent(EntityReferenceResolver.TYPE_STRING,
55    "relative");
56   
 
57  15 toggle @Override
58    public boolean action(XWikiContext context) throws XWikiException
59    {
60    // CSRF prevention
61  15 if (!csrfTokenCheck(context)) {
62  0 return false;
63    }
64   
65  15 XWiki xwiki = context.getWiki();
66  15 XWikiResponse response = context.getResponse();
67  15 DocumentReference userReference = context.getUserReference();
68  15 XWikiDocument doc = context.getDoc();
69  15 ObjectAddForm oform = (ObjectAddForm) context.getForm();
70   
71  15 String className = oform.getClassName();
72  15 EntityReference classReference = this.relativeResolver.resolve(className, EntityType.DOCUMENT);
73  15 BaseObject object = doc.newXObject(classReference, context);
74   
75  15 BaseClass baseclass = object.getXClass(context);
76    // The request parameter names that correspond to object fields must NOT specify the object number because the
77    // object number is not known before the object is added. The following is a good parameter name:
78    // Space.Class_property. As a consequence we use only the class name to extract the object from the request.
79  15 Map<String, String[]> objmap = oform.getObject(className);
80    // We need to have a string in the map for each field for the object to be correctly created.
81    // Otherwise, queries using the missing properties will fail to return this object.
82  15 @SuppressWarnings("unchecked")
83    Collection<PropertyClass> fields = baseclass.getFieldList();
84  15 for (PropertyClass property : fields) {
85  220 String name = property.getName();
86  220 if (objmap.get(name) == null) {
87  186 objmap.put(name, EMPTY_PROPERTY);
88    }
89    }
90   
91    // Load the object properties that are defined in the request.
92  15 baseclass.fromMap(objmap, object);
93   
94  15 doc.setAuthorReference(userReference);
95  15 if (doc.isNew()) {
96  1 doc.setCreatorReference(userReference);
97    }
98  15 xwiki.saveDocument(doc, localizePlainOrKey("core.comment.addObject"), true, context);
99   
100    // If this is an ajax request, no need to redirect.
101  15 if (Utils.isAjaxRequest(context)) {
102  0 context.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
103  0 return false;
104    }
105   
106    // forward to edit
107  15 String redirect = Utils.getRedirect("edit", "editor=object", "xcontinue", "xredirect");
108    // If the redirect URL contains the xobjectNumber parameter then inject the number of the added object as its
109    // value so that the target page knows which object was added.
110  15 redirect =
111    XOBJECT_NUMBER_PARAMETER.matcher(redirect).replaceFirst("$1xobjectNumber=" + object.getNumber() + "$2");
112  15 sendRedirect(response, redirect);
113   
114  15 return false;
115    }
116    }