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

File ObjectsSynchronizeAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

4
27
3
1
105
60
7
0.26
9
3
2.33

Classes

Class Line # Actions
ObjectsSynchronizeAction 39 27 0% 7 34
0.00%
 

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.List;
23   
24    import javax.servlet.http.HttpServletResponse;
25   
26    import com.xpn.xwiki.XWiki;
27    import com.xpn.xwiki.XWikiContext;
28    import com.xpn.xwiki.XWikiException;
29    import com.xpn.xwiki.doc.XWikiDocument;
30    import com.xpn.xwiki.objects.BaseObject;
31    import com.xpn.xwiki.objects.BaseProperty;
32   
33    /**
34    * Synchronizes the objects in a document with their current classes, by removing any deprecated properties.
35    *
36    * @version $Id: 07a33115e4f71b007778f1672385d8d1109130a5 $
37    * @since 2.4M2
38    */
 
39    public class ObjectsSynchronizeAction extends XWikiAction
40    {
 
41  0 toggle @Override
42    public boolean action(XWikiContext context) throws XWikiException
43    {
44  0 XWiki xwiki = context.getWiki();
45  0 XWikiResponse response = context.getResponse();
46  0 XWikiDocument doc = context.getDoc();
47  0 XWikiRequest request = context.getRequest();
48   
49  0 String className = request.getParameter("classname");
50  0 String objectNumber = request.getParameter("object");
51   
52  0 if (className != null && objectNumber != null) {
53  0 try {
54  0 BaseObject object = doc.getObject(className, Integer.valueOf(objectNumber));
55  0 synchronizeObject(object, context);
56    } catch (Exception ex) {
57    // Wrong parameters, non-existing object
58  0 return true;
59    }
60    } else {
61  0 for (List<BaseObject> classObjects : doc.getXObjects().values()) {
62  0 for (BaseObject object : classObjects) {
63  0 synchronizeObject(object, context);
64    }
65    }
66    }
67   
68    // Set the new author
69  0 doc.setAuthorReference(context.getUserReference());
70   
71  0 xwiki.saveDocument(doc, localizePlainOrKey("core.model.xobject.synchronizeObjects.versionSummary"), true,
72    context);
73   
74  0 if (Utils.isAjaxRequest(context)) {
75  0 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
76  0 response.setContentLength(0);
77    } else {
78    // forward to edit
79  0 String redirect = Utils.getRedirect("edit", "editor=object", context);
80  0 sendRedirect(response, redirect);
81    }
82  0 return false;
83    }
84   
 
85  0 toggle @Override
86    public String render(XWikiContext context) throws XWikiException
87    {
88  0 context.getResponse().setStatus(HttpServletResponse.SC_NOT_FOUND);
89  0 context.put("message", "core.model.xobject.synchronizeObjects.error.missingObject");
90  0 return "exception";
91    }
92   
93    /**
94    * Remove deprecated fields (properties deleted from the XClass) from an object.
95    *
96    * @param object the object to synchronize
97    * @param context the current request context
98    */
 
99  0 toggle private void synchronizeObject(BaseObject object, XWikiContext context)
100    {
101  0 for (BaseProperty property : object.getXClass(context).getDeprecatedObjectProperties(object)) {
102  0 object.removeField(property.getName());
103    }
104    }
105    }