r1034 - in xwiki/trunk/src/main/java/com/xpn/xwiki: api objects

Ludovic Dubost ludovic at users.forge.objectweb.org
Sat Apr 8 23:00:48 CEST 2006


Author: ludovic
Date: 2006-04-08 23:00:47 +0200 (Sat, 08 Apr 2006)
New Revision: 1034

Modified:
   xwiki/trunk/src/main/java/com/xpn/xwiki/api/Document.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/api/Object.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/objects/BaseObject.java
Log:
Added write API to XWiki api
Document is now cloned in the api
Fixed createNewObject to return object number
Added newObject which returns the object directly

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/api/Document.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/api/Document.java	2006-04-08 19:50:15 UTC (rev 1033)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/api/Document.java	2006-04-08 21:00:47 UTC (rev 1034)
@@ -53,12 +53,14 @@
 
 
 public class Document extends Api {
+    private XWikiDocument olddoc;
     private XWikiDocument doc;
     private Object currentObj;
 
     public Document(XWikiDocument doc, XWikiContext context) {
         super(context);
-        this.doc = doc;
+        this.olddoc = doc;
+        this.doc = (XWikiDocument) doc.clone();
     }
 
     public XWikiDocument getDocument() {
@@ -271,11 +273,15 @@
         return result;
     }
 
-    public void createNewObject(String classname) throws XWikiException {
-        if (checkProgrammingRights())
-            doc.createNewObject(classname, context);
+    public int createNewObject(String classname) throws XWikiException {
+       return doc.createNewObject(classname, context);
     }
 
+    public Object newObject(String classname) throws XWikiException {
+       int nb = createNewObject(classname);
+       return getObject(classname, nb);
+    }
+
     public boolean isFromCache() {
         return doc.isFromCache();
     }
@@ -352,8 +358,17 @@
     }
 
     public Object getObject(String classname) {
+        return getObject(classname, false);
+    }
+
+    public Object getObject(String classname, boolean create) {
         try {
             BaseObject obj = doc.getObject(classname);
+
+            if ((obj==null)&&create) {
+              return newObject(classname);
+            }
+
             if (obj==null)
                 return null;
             else
@@ -826,4 +841,41 @@
         return doc.isCreator(username);
     }
 
+    public void set(String fieldname, java.lang.Object value) {
+        Object obj;
+        if (currentObj!=null)
+         obj = currentObj;
+        else
+         obj = getFirstObject(fieldname);
+        if (obj==null)
+         return;
+
+        obj.set(fieldname, value);
+    }
+
+    public void setTitle(String title) {
+        doc.setTitle(title);
+    }
+
+    public void setParent(String parent) {
+        doc.setParent(parent);
+    }
+
+    public void setContent(String content) {
+        doc.setContent(content);
+    }
+
+    public void setDefaultTemplate(String dtemplate) {
+        doc.setDefaultTemplate(dtemplate);
+    }
+
+    public void save() throws XWikiException {
+        if (hasAccessLevel("edit"))
+         context.getWiki().saveDocument(doc, olddoc, context);
+        else {
+            java.lang.Object[] args = { doc.getFullName() };
+         throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED,
+                                  "Access denied in edit mode on document {0}", null, args);
+        }
+    }
 }

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/api/Object.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/api/Object.java	2006-04-08 19:50:15 UTC (rev 1033)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/api/Object.java	2006-04-08 21:00:47 UTC (rev 1034)
@@ -1,24 +1,24 @@
-/*
- * Copyright 2006, XpertNet SARL, and individual contributors as indicated
- * by the contributors.txt.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- *
- * @author amelentev
- */
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author amelentev
+ */
 
 
 package com.xpn.xwiki.api;
@@ -28,6 +28,8 @@
 import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.objects.BaseObject;
 
+import java.lang.*;
+
 public class Object extends Collection {
 
     public Object(BaseObject obj, XWikiContext context) {
@@ -66,8 +68,12 @@
     }
 
     public boolean equals(java.lang.Object arg0) {
-    	if (!(arg0 instanceof Object)) return false;
-    	Object o = (Object) arg0;
-    	return o.context.equals(context) && element.equals(o.element);    	
+        if (!(arg0 instanceof Object)) return false;
+        Object o = (Object) arg0;
+        return o.context.equals(context) && element.equals(o.element);
     }
+
+    public void set(String fieldname, java.lang.Object value) {
+        getBaseObject().set(fieldname, value, context);
+    }
 }

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/objects/BaseObject.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/objects/BaseObject.java	2006-04-08 19:50:15 UTC (rev 1033)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/objects/BaseObject.java	2006-04-08 21:00:47 UTC (rev 1034)
@@ -1,26 +1,26 @@
-/*
- * Copyright 2006, XpertNet SARL, and individual contributors as indicated
- * by the contributors.txt.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- *
- * @author ludovic
- * @author erwan
- * @author sdumitriu
- */
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author ludovic
+ * @author erwan
+ * @author sdumitriu
+ */
 
 package com.xpn.xwiki.objects;
 
@@ -181,4 +181,21 @@
     public com.xpn.xwiki.api.Object newObjectApi(BaseObject obj, XWikiContext context) {
         return new com.xpn.xwiki.api.Object(obj, context);
     }
+
+    public void set(String fieldname, java.lang.Object value, XWikiContext context) {
+        BaseClass bclass = getxWikiClass(context);
+        PropertyClass pclass = (PropertyClass) bclass.get(fieldname);
+        BaseProperty prop = (BaseProperty) safeget(fieldname);
+        if ((value instanceof String)&&(pclass!=null))
+         prop = pclass.fromString((String) value);
+        else {
+            if ((prop==null)&&(pclass!=null))
+             prop = pclass.newProperty();
+            if (prop!=null)
+             prop.setValue(value);
+        }
+
+        if (prop!=null)
+         safeput(fieldname, prop);
+    }
 }





More information about the Xwiki-notifications mailing list