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

File ComputedFieldClass.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

0
20
9
1
144
75
10
0.5
2.22
9
1.11

Classes

Class Line # Actions
ComputedFieldClass 42 20 0% 10 25
0.1379310313.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.objects.classes;
21   
22    import javax.script.ScriptContext;
23   
24    import org.xwiki.script.ScriptContextManager;
25   
26    import com.xpn.xwiki.XWikiContext;
27    import com.xpn.xwiki.doc.XWikiDocument;
28    import com.xpn.xwiki.objects.BaseCollection;
29    import com.xpn.xwiki.objects.BaseObject;
30    import com.xpn.xwiki.objects.BaseProperty;
31    import com.xpn.xwiki.objects.StringProperty;
32    import com.xpn.xwiki.objects.meta.PropertyMetaClass;
33    import com.xpn.xwiki.web.Utils;
34   
35    /**
36    * Computed Field Class allows to create a field without storage that will display computed values based on other data
37    * in the object or wiki.
38    *
39    * @version $Id: bf9a9400b56cca65a4d30c69fb2634001df104ad $
40    * @since 4.2M2
41    */
 
42    public class ComputedFieldClass extends PropertyClass
43    {
44    /**
45    * Constant defining the field name.
46    **/
47    protected static final String XCLASSNAME = "computedfield";
48   
49    /**
50    * Constant defining the name of the script field.
51    **/
52    protected static final String FIELD_SCRIPT = "script";
53   
54    /**
55    * Constructor for ComputedFieldClass.
56    *
57    * @param wclass Meta Class
58    */
 
59  6 toggle public ComputedFieldClass(PropertyMetaClass wclass)
60    {
61  6 super(XCLASSNAME, "Computed Field", wclass);
62    }
63   
64    /**
65    * Constructor for ComputedFieldClass.
66    */
 
67  6 toggle public ComputedFieldClass()
68    {
69  6 this(null);
70    }
71   
72    /**
73    * Setter for the script value.
74    *
75    * @param sValue script to be used for the computed field
76    */
 
77  0 toggle public void setScript(String sValue)
78    {
79  0 setLargeStringValue(FIELD_SCRIPT, sValue);
80    }
81   
82    /**
83    * Getter for the script value.
84    *
85    * @return script to be used for the computed field
86    */
 
87  0 toggle public String getScript()
88    {
89  0 String sValue = getLargeStringValue(FIELD_SCRIPT);
90  0 return sValue;
91    }
92   
 
93  0 toggle @Override
94    public BaseProperty fromString(String value)
95    {
96  0 return null;
97    }
98   
 
99  0 toggle @Override
100    public BaseProperty newProperty()
101    {
102  0 BaseProperty property = new StringProperty();
103  0 property.setName(getName());
104  0 return property;
105    }
106   
 
107  0 toggle @Override
108    public void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object,
109    XWikiContext context)
110    {
111  0 String script = getScript();
112   
113  0 try {
114  0 ScriptContext scontext = Utils.getComponent(ScriptContextManager.class).getCurrentScriptContext();
115  0 scontext.setAttribute("name", name, ScriptContext.ENGINE_SCOPE);
116  0 scontext.setAttribute("prefix", prefix, ScriptContext.ENGINE_SCOPE);
117  0 scontext.setAttribute("object", new com.xpn.xwiki.api.Object((BaseObject) object, context),
118    ScriptContext.ENGINE_SCOPE);
119   
120  0 XWikiDocument classDocument = object.getXClass(context).getOwnerDocument();
121   
122  0 String result = renderContentInContext(script, classDocument.getSyntax().toIdString(),
123    classDocument.getAuthorReference(), context);
124   
125  0 buffer.append(result);
126    } catch (Exception e) {
127    // TODO: append a rendering style complete error instead
128  0 buffer.append(e.getMessage());
129    }
130    }
131   
 
132  0 toggle @Override
133    public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
134    XWikiContext context)
135    {
136  0 displayView(buffer, name, prefix, object, context);
137    }
138   
 
139  0 toggle @Override
140    public void displayHidden(StringBuffer buffer, String name, String prefix, BaseCollection object,
141    XWikiContext context)
142    {
143    }
144    }