Skip to content

Commit 742cd45

Browse files
pjeanjeantmortagne
authored andcommitted
XWIKI-21699: Add new API to help evaluate xobjects
XWIKI-21473: Improve search suggest evaluation
1 parent 2ac89a6 commit 742cd45

File tree

16 files changed

+726
-10
lines changed

16 files changed

+726
-10
lines changed

xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/Object.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
*/
2020
package com.xpn.xwiki.api;
2121

22+
import java.util.Map;
23+
24+
import org.xwiki.evaluation.ObjectEvaluator;
25+
import org.xwiki.evaluation.ObjectEvaluatorException;
2226
import org.xwiki.model.reference.ObjectPropertyReference;
27+
import org.xwiki.stability.Unstable;
2328

2429
import com.xpn.xwiki.XWikiContext;
2530
import com.xpn.xwiki.XWikiException;
@@ -159,4 +164,18 @@ public ObjectPropertyReference getPropertyReference(String propertyName)
159164
{
160165
return new ObjectPropertyReference(propertyName, getReference());
161166
}
167+
168+
/**
169+
* Evaluates the properties of an object using a matching implementation of {@link ObjectEvaluator}.
170+
*
171+
* @return a Map storing the evaluated properties
172+
* @since 14.10.21
173+
* @since 15.5.5
174+
* @since 15.10.2
175+
*/
176+
@Unstable
177+
public Map<String, String> evaluate() throws ObjectEvaluatorException
178+
{
179+
return getBaseObject().evaluate();
180+
}
162181
}

xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/objects/BaseObject.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@
2222
import java.io.Serializable;
2323
import java.util.ArrayList;
2424
import java.util.List;
25+
import java.util.Map;
2526
import java.util.UUID;
2627

2728
import org.apache.commons.lang3.builder.HashCodeBuilder;
2829
import org.dom4j.Element;
30+
import org.xwiki.evaluation.ObjectEvaluator;
31+
import org.xwiki.evaluation.ObjectEvaluatorException;
2932
import org.xwiki.model.EntityType;
3033
import org.xwiki.model.reference.DocumentReference;
3134
import org.xwiki.model.reference.DocumentReferenceResolver;
3235
import org.xwiki.model.reference.EntityReference;
3336
import org.xwiki.model.reference.SpaceReference;
37+
import org.xwiki.stability.Unstable;
3438

3539
import com.xpn.xwiki.XWikiContext;
3640
import com.xpn.xwiki.XWikiException;
@@ -435,4 +439,20 @@ protected void mergeField(PropertyInterface currentElement, ElementInterface pre
435439

436440
super.mergeField(currentElement, previousElement, newElement, configuration, context, mergeResult);
437441
}
442+
443+
/**
444+
* Evaluates the properties of an object using a matching implementation of {@link ObjectEvaluator}.
445+
*
446+
* @return a Map storing the evaluated properties
447+
* @throws ObjectEvaluatorException if the evaluation fails
448+
* @since 14.10.21
449+
* @since 15.5.5
450+
* @since 15.10.2
451+
*/
452+
@Unstable
453+
public Map<String, String> evaluate() throws ObjectEvaluatorException
454+
{
455+
ObjectEvaluator objectEvaluator = Utils.getComponent(ObjectEvaluator.class);
456+
return objectEvaluator.evaluate(this);
457+
}
438458
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 org.xwiki.evaluation;
21+
22+
import java.util.Map;
23+
24+
import org.xwiki.component.annotation.Role;
25+
import org.xwiki.evaluation.internal.DefaultObjectEvaluator;
26+
import org.xwiki.stability.Unstable;
27+
28+
import com.xpn.xwiki.objects.BaseObject;
29+
30+
/**
31+
* Evaluates the properties of an object and returns a Map that stores the evaluation results, in which keys are the
32+
* field names of the properties, and values their evaluated content.
33+
* Implement an instance with a hint corresponding to the class name of the object you want to evaluate and use
34+
* {@link DefaultObjectEvaluator} that will proxy the calls to the right implementation.
35+
* This ensures that the properties being evaluated are only the ones referenced explicitly by the provided
36+
* implementation, in order to avoid accidental evaluation of properties that should only be used in specific contexts.
37+
*
38+
* @version $Id$
39+
* @since 14.10.21
40+
* @since 15.5.5
41+
* @since 15.10.2
42+
*/
43+
@Unstable
44+
@Role
45+
public interface ObjectEvaluator
46+
{
47+
/**
48+
* Evaluates the properties of an object.
49+
*
50+
* @param object the object to evaluate
51+
* @return a Map storing the evaluated properties
52+
* @throws ObjectEvaluatorException if the evaluation fails
53+
*/
54+
Map<String, String> evaluate(BaseObject object) throws ObjectEvaluatorException;
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 org.xwiki.evaluation;
21+
22+
import org.xwiki.stability.Unstable;
23+
24+
/**
25+
* Exception raised during evaluation of XObjects properties.
26+
*
27+
* @version $Id$
28+
* @since 14.10.21
29+
* @since 15.5.5
30+
* @since 15.10.2
31+
*/
32+
@Unstable
33+
public class ObjectEvaluatorException extends Exception
34+
{
35+
/**
36+
* Creates an instance of ObjectEvaluatorException with a message and a cause.
37+
*
38+
* @param message the message detailing the issue
39+
* @param cause the cause of the exception
40+
*/
41+
public ObjectEvaluatorException(String message, Throwable cause)
42+
{
43+
super(message, cause);
44+
}
45+
46+
/**
47+
* Creates an instance of ObjectEvaluatorException with a message.
48+
*
49+
* @param message the message detailing the issue
50+
*/
51+
public ObjectEvaluatorException(String message)
52+
{
53+
this(message, null);
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 org.xwiki.evaluation;
21+
22+
import java.util.Map;
23+
24+
import org.xwiki.component.annotation.Role;
25+
import org.xwiki.stability.Unstable;
26+
27+
import com.xpn.xwiki.objects.BaseObject;
28+
29+
/**
30+
* Evaluates the properties of an object and returns a Map that stores the evaluation results, in which keys are the
31+
* field names of the properties, and values their evaluated content.
32+
* Instances of this interface should be used as helpers by implementations of {@link ObjectEvaluator}.
33+
*
34+
* @version $Id$
35+
* @since 14.10.21
36+
* @since 15.5.5
37+
* @since 15.10.2
38+
*/
39+
@Unstable
40+
@Role
41+
public interface ObjectPropertyEvaluator
42+
{
43+
/**
44+
* Evaluates the properties of an object.
45+
*
46+
* @param object the object to evaluate
47+
* @param properties the names of the properties to evaluate
48+
* @return a Map storing the evaluated properties
49+
* @throws ObjectEvaluatorException if the evaluation fails
50+
*/
51+
Map<String, String> evaluateProperties(BaseObject object, String... properties) throws ObjectEvaluatorException;
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 org.xwiki.evaluation.internal;
21+
22+
import java.util.Collections;
23+
import java.util.Map;
24+
25+
import javax.inject.Inject;
26+
import javax.inject.Named;
27+
import javax.inject.Provider;
28+
import javax.inject.Singleton;
29+
30+
import org.xwiki.component.annotation.Component;
31+
import org.xwiki.component.manager.ComponentLookupException;
32+
import org.xwiki.component.manager.ComponentManager;
33+
import org.xwiki.evaluation.ObjectEvaluator;
34+
import org.xwiki.evaluation.ObjectEvaluatorException;
35+
import org.xwiki.model.reference.EntityReferenceSerializer;
36+
37+
import com.xpn.xwiki.objects.BaseObject;
38+
39+
/**
40+
* Evaluator that proxies the actual evaluation to the right ObjectEvaluator implementation, based on the XClass of
41+
* the object.
42+
*
43+
* @version $Id$
44+
* @since 14.10.21
45+
* @since 15.5.5
46+
* @since 15.10.2
47+
*/
48+
@Component
49+
@Singleton
50+
public class DefaultObjectEvaluator implements ObjectEvaluator
51+
{
52+
@Inject
53+
@Named("context")
54+
private Provider<ComponentManager> contextComponentManagerProvider;
55+
56+
@Inject
57+
@Named("local")
58+
private EntityReferenceSerializer<String> entityReferenceSerializer;
59+
60+
@Override
61+
public Map<String, String> evaluate(BaseObject object) throws ObjectEvaluatorException
62+
{
63+
if (object == null) {
64+
return Collections.emptyMap();
65+
}
66+
67+
String xClassName = this.entityReferenceSerializer.serialize(object.getXClassReference());
68+
ComponentManager componentManager = this.contextComponentManagerProvider.get();
69+
if (!componentManager.hasComponent(ObjectEvaluator.class, xClassName)) {
70+
throw new ObjectEvaluatorException(String.format("Could not find an instance of 'ObjectEvaluator' for "
71+
+ "XObject of class '%s'.", xClassName));
72+
}
73+
74+
try {
75+
ObjectEvaluator objectEvaluator = componentManager.getInstance(ObjectEvaluator.class, xClassName);
76+
return objectEvaluator.evaluate(object);
77+
} catch (ComponentLookupException e) {
78+
throw new ObjectEvaluatorException(String.format("Could not instantiate 'ObjectEvaluator' for XObject of "
79+
+ "class '%s'.", xClassName), e);
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)