Vincent Massol wrote:
I like this. Shouldn't we also remove the
automatic creation of Tag
objects in all documents by default?
Yes, I'd really like that. The current implementation for tags is wrong
at many levels. We should replace all the custom code with calls to the
tag plugin.
I have never understood why we do this for Tags and
not for Rights,
and all other objects. For me the objects should be created only when
required and not by default.
+1
On Jul 4, 2009, at 5:22 PM, sdumitriu (SVN) wrote:
> Author: sdumitriu
> Date: 2009-07-04 17:22:41 +0200 (Sat, 04 Jul 2009)
> New Revision: 21818
Another problem is that creating objects does not create their
properties also, so objects are property-less until they are saved from
the web interface, and there is no simple API to set and eventually
create such a property. The code here is much too complicated and tied
to the internals of the current data model, but there's no simpler way
of doing it right now.
> Modified:
> platform/xwiki-plugins/trunk/tag/src/main/java/com/xpn/xwiki/
> plugin/tag/TagPlugin.java
> Log:
> XATAG-20: Adding tags fails when there is no Tag object attached to
> the document
> Fixed.
>
> Modified: platform/xwiki-plugins/trunk/tag/src/main/java/com/xpn/
> xwiki/plugin/tag/TagPlugin.java
> ===================================================================
> --- platform/xwiki-plugins/trunk/tag/src/main/java/com/xpn/xwiki/
> plugin/tag/TagPlugin.java 2009-07-04 15:22:34 UTC (rev 21817)
> +++ platform/xwiki-plugins/trunk/tag/src/main/java/com/xpn/xwiki/
> plugin/tag/TagPlugin.java 2009-07-04 15:22:41 UTC (rev 21818)
> @@ -29,12 +29,18 @@
>
> import org.apache.commons.collections.CollectionUtils;
> import org.apache.commons.lang.StringUtils;
> +import org.apache.commons.logging.Log;
> +import org.apache.commons.logging.LogFactory;
>
> import com.xpn.xwiki.XWikiContext;
> import com.xpn.xwiki.XWikiException;
> import com.xpn.xwiki.api.Api;
> import com.xpn.xwiki.doc.XWikiDocument;
> +import com.xpn.xwiki.objects.BaseObject;
> import com.xpn.xwiki.objects.BaseProperty;
> +import com.xpn.xwiki.objects.DBStringListProperty;
> +import com.xpn.xwiki.objects.classes.BaseClass;
> +import com.xpn.xwiki.objects.classes.PropertyClass;
> import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
> import com.xpn.xwiki.plugin.XWikiPluginInterface;
>
> @@ -45,6 +51,9 @@
> */
> public class TagPlugin extends XWikiDefaultPlugin implements
> XWikiPluginInterface
> {
> + /** Logging helper object. */
> + public static final Log LOG = LogFactory.getLog(TagPlugin.class);
> +
> /**
> * The identifier for this plugin; used for accessing the plugin
> from velocity, and as the action returning the
> * extension content.
> @@ -116,10 +125,40 @@
> private void setDocumentTags(XWikiDocument document,
> List<String> tags, XWikiContext context)
> {
> BaseProperty prop = (BaseProperty)
> document.getObject(TAG_CLASS, true, context).safeget(TAG_PROPERTY);
> + // Properties aren't added to an object unless a value is
> specified either from the Web or from an XML.
> + if (prop == null) {
> + prop = createTagProperty(document.getObject(TAG_CLASS,
> true, context), context);
> + }
> prop.setValue(tags);
> }
>
> /**
> + * Create and add the main tag property to the provided tag
> object. The new property corresponds to the definition
> + * in the tag class, but in case of an error, the default type
> is a relational-stored list.
> + *
> + * @param tagObject the target tag object
> + * @param context the current request context
> + * @return the created property
> + * @see #TAG_PROPERTY
> + */
> + private BaseProperty createTagProperty(BaseObject tagObject,
> XWikiContext context)
> + {
> + BaseProperty tagProperty;
> + try {
> + BaseClass tagClass =
> context.getWiki().getClass(TAG_CLASS, context);
> + PropertyClass tagPropertyDefinition = (PropertyClass)
> tagClass.getField(TAG_PROPERTY);
> + tagProperty = tagPropertyDefinition.newProperty();
> + } catch (XWikiException ex) {
> + LOG.warn("Failed to properly create tag property for
> the tag object, creating a default one");
> + tagProperty = new DBStringListProperty();
> + }
> + tagProperty.setName(TAG_PROPERTY);
> + tagProperty.setObject(tagObject);
> + tagObject.safeput(TAG_PROPERTY, tagProperty);
> + return tagProperty;
> + }
> +
> + /**
> * Get all tags within the wiki.
> *
> * @param context XWiki context.
--
Sergiu Dumitriu
http://purl.org/net/sergiu/