Thank you Eduard!
That means I don't have to calculate a "new id" by myself... so slimmer
code:
List<XWikiObjectSummary> objs = rpc.getObjects("XWiki.mygroup");
boolean alreadyAMember = false;
for (XWikiObjectSummary objSum : objs) {
XWikiObject obj = rpc.getObject(objSum);
if (obj.getClassName().equals("XWiki.XWikiGroups")) {
if (obj.getProperty("member").equals("XWiki.user01")) {
alreadyAMember=true;
break; // for
}
}
}
if (!alreadyAMember) {
XWikiObject xobjgrp = new XWikiObject();
xobjgrp.setClassName("XWiki.XWikiGroups");
xobjgrp.setPageId(("XWiki.mygroup");
xobjgrp.setId(-1);
xobjgrp.setProperty("member","XWiki.user01");
rpc.storeObject(xobjgrp);
}
-----Mensaje original-----
De: devs-bounces(a)xwiki.org [mailto:devs-bounces@xwiki.org] En nombre de Eduard Moraru
Enviado el: viernes, 17 de junio de 2011 10:56
Para: devs(a)xwiki.org
Asunto: Re: [xwiki-devs] creating groups via XML-RPC
Hi,
Not sure about this, but XWiki`s XMLRPC server should automatically
assign an object ID if none exists. However, I believe the problem was
that you did not specify any object ID and the default was 0. Just look
at the server side code
.
1. So your first object was not found and thus created with ID 0
(generated by XWiki).
2. Your second object (with default id of 0 caused by the fact that you
did not specify an ID) was already found (which was actually the first
object) and, using the same id (0), was overriden with the properties
from the second object.
3. In the end, you have the first object with id 0 overriden with the
properties of the last object you tried to store.
If you specify and id of -1, no object should be found on the server and
XWiki should generate a new id on the server side. The object returned
by the storeObject method on the client side should contain the new ID.
So you`d have to do...
XWikiObject obj = new XWikiObject();
obj.setId(-1);
obj.set...;
obj = rpc.storeObject(obj);
System.out.println("New object ID: " + obj.getId());
Hope this helps,
Eduard
On 06/17/2011 04:17 AM, Riveira Faraldo, Bernardo wrote:
Ok, could not sleep until I tried it... :)
Changed the code to add membership to something a little bit more convoluted (a function
that finds a free ID number and also checks the user is not already a member)
This is slow via XML-RPC but the code is easily understandable...
private void XWikiAnadirUsuarioAGrupo(XWikiXmlRpcClient rpc, String usuarioXWiki, String
grupoXwiki) throws XmlRpcException {
List<XWikiObjectSummary> objs = rpc.getObjects(grupoXwiki);
int maxId = 0, currId;
boolean alreadyAMember = false;
for (XWikiObjectSummary objSum : objs) {
XWikiObject obj = rpc.getObject(objSum);
currId = obj.getId();
if (currId>maxId) {
maxId=currId;
}
if (obj.getClassName().equals("XWiki.XWikiGroups")) {
if (obj.getProperty("member").equals(usuarioXWiki)) {
alreadyAMember=true;
break; // for
}
}
}
if (!alreadyAMember) {
// anadimos membership
XWikiObject xobjgrp = new XWikiObject();
xobjgrp.setClassName("XWiki.XWikiGroups");
xobjgrp.setPageId(grupoXwiki);
xobjgrp.setId(maxId + 1);
xobjgrp.setProperty("member",usuarioXWiki);
rpc.storeObject(xobjgrp);
}
}
This way it works!
Thank you for guiding me to solve this!!
Bernardo Riveira
-----Mensaje original-----
De: devs-bounces(a)xwiki.org [mailto:devs-bounces@xwiki.org] En nombre de Riveira Faraldo,
Bernardo
Enviado el: viernes, 17 de junio de 2011 2:21
Para: XWiki Developers
Asunto: Re: [xwiki-devs] creating groups via XML-RPC
OK, following you suggestions I changed a couple of things; first of all, added a parent
to each new page...
page.setParentId("XWiki.XWikiGroups");
to each new group; with that there seems to be no difference between a group created via
UI vs a group created via XML-RPC
also to user pages with...
page.setParentId("XWiki.XWikiUsers");
that solved one of my questions...
Then I tried using a different Id (an integer) for each XWikiObject used for membership.
That allowed me to add all the users so as you said, now they won't be overwriting the
same one.
(BTW the example for creating users in
http://platform.xwiki.org/xwiki/bin/view/Features/XMLRPCJavaExamples is wrong as it would
overwrite always the last user added with the same code in the Group XWikiAllGroup)
BUT, what I see now is that if I DO TRY to overwrite an user using the same Id, they
won't overwrite the same user.
I mean, if I do the following:
// add a member
XWikiObject xobjgrp = new XWikiObject();
xobjgrp.setClassName("XWiki.XWikiGroups");
xobjgrp.setPageId("XWiki.XWikiAllGroup");
xobjgrp.setId(1);
xobjgrp.setProperty("member","username1");
rpc.storeObject(xobjgrp);
// again, same member (sounds stupid, don't you think?)
XWikiObject xobjgrp = new XWikiObject();
xobjgrp.setClassName("XWiki.XWikiGroups");
xobjgrp.setPageId("XWiki.XWikiAllGroup");
xobjgrp.setId(1);
xobjgrp.setProperty("member","username1");
rpc.storeObject(xobjgrp);
What I get is TWO membership objects for "username1" in the same Group page. I
thought I would overwrite the first one with the second.
Maybe you think "this guy is nuts, now he complains they're not overwriting each
other..." :) but my code is not like above, of course; but it would be nice if each
night, when re-populating and creating new users and the like, if I just try to re-enter
the membership for a user already a member, I could do it without ending with repeated
membership
Else, I need a way to read the XWikiObjects in a page, get the Ids, get the
"member" properties and so find out if an user is already a member (so I skip
it) or it needs to get added (so I use max(Id)+1 as new Id, and there are no examples for
accesing Objects in a Page via XML-RPC
So tomorrow I will start playing with rpc.getObjects("XWiki.XWikiAllGroup") :)
Thanks in advance
-----Mensaje original-----
De: devs-bounces(a)xwiki.org [mailto:devs-bounces@xwiki.org] En nombre de Riveira Faraldo,
Bernardo
Enviado el: jueves, 16 de junio de 2011 18:02
Para: XWiki Developers
Asunto: Re: [xwiki-devs] creating groups via XML-RPC
Thank you Florin, Sergiu, Eduard!
Will try your suggestions and I'll tell you.
Bernardo
-----Mensaje original-----
De: devs-bounces(a)xwiki.org [mailto:devs-bounces@xwiki.org] En nombre de Florin Ciubotaru
Enviado el: jueves, 16 de junio de 2011 17:49
Para: XWiki Developers
Asunto: Re: [xwiki-devs] creating groups via XML-RPC
Hi Bernardo,
As Eduard said the REST API is newer and has a better traction, though it
doesn't match the entire XML-RPC functionality(eg: syntax conversion).
Your use case is typical for some RPC style automation and our API should
work just fine.
Regarding your issue:
The XWikiObject entities also have an "id" property which identifies them in
a document. Incrementing that property should prevent the storeObject(..)
code from overriding the previous objects(group members).
I didn't test the java client for the xml-rpc, but I doubt there's something
wrong with it.
To see a more clear representation of the model you could check these
sources(.net mapping):
-
http://svn.xwiki.org/svnroot/xwiki/xoffice/trunk/xword/Connectivity/Clients…
Hope this helps,
Florin Ciubotaru
2011/6/16 Riveira Faraldo, Bernardo<briveira(a)mundo-r.net>
Hi there!
We are trying to use XWiki 3.0 enterprise, trying to create via
XML-RPC thousands of users, groups and membership programmatically so
we can keep updated AD, Liferay, other sources of users, other apps,
all with CAS integration, etc.
As there is still no confluence 2.0 API compatibility but we need to
create users& groups in XWiki from an external software, we have been
collecting pieces of code from messages and FAQs about how to
implement it.
We try to create users, their pages, and add them to the XWikiAllGroup
with :
Page page = new Page();
page.setSpace("XWiki");
page.setTitle("username01");
page.setId("XWiki.username01");
page.setContent("{{include
document=\"XWiki.XWikiUserSheet\"/}}");
rpc.storePage(page);
XWikiObject xobj = new XWikiObject();
xobj.setClassName("XWiki.XWikiUsers");
xobj.setPageId("XWiki.username01");
xobj.setProperty("first_name",
"name");
xobj.setProperty("last_name", "last
name");
xobj.setProperty("email",
"email(a)email.com")com");
xobj.setProperty("password","##########");
rpc.storeObject(xobj);
XWikiObject xobjgrp = new XWikiObject();
xobjgrp.setClassName("XWiki.XWikiGroups");
xobjgrp.setPageId("XWiki.XWikiAllGroup");
xobjgrp.setProperty("member","XWiki.username01");
rpc.storeObject(xobjgrp);
(Suppose we run that with username01 to username99)
Although first time we run this, the page gets created, the user gets
created but the properties not! :-o
If we run again the import, then every user gets their properties. :-?
Then, only the LAST user created gets correctly added to
XWiki.XWikiAllGroup. If we stop/debug the program between each user
creation process, always the last one appears at XWiki.XWikiAllGroup
when checking via UI. So they are all there for a moment, until they
are replaced by the last one. Like if the rpc.storeObject(xobjgrp)
deleted previously any other user in that XWiki.XWikiAllGroup page.
Then we try to create groups using this:
// create group 01
Page page = new Page();
page.setSpace("XWiki");
page.setTitle("group01");
page.setContent("{{include document='XWiki.XWikiGroupSheet' /}}");
rpc.storePage(page);
// create group 02
Page page = new Page();
page.setSpace("XWiki");
page.setTitle("group02");
page.setContent("{{include document='XWiki.XWikiGroupSheet' /}}");
rpc.storePage(page);
...
Those "Groups" get created as Pages with XWikiGroupSheet app that
allows us to see/edit the users via UI, but we need to add them via
XML-RPC.
ALSO, they are Pages, but they are NOT EXACTLY the same as if they
where created via the Admin UI, where when you create a "Group" it
shows slightly differently:
Autocreated via XML-RPC:
[cid:image001.png@01CC2C45.659F3540]
Versus when a group is created via Admin UI, it shows up differently,
just like this:
[cid:image002.png@01CC2C45.659F3540]
SO, we suppose the way we create a Group via XML-RPC (just a Page with
a special Sheet) is NOT ENOUGH.
And finally we try to add existing users to newly created groups with
code like this:
// user 01 to group 01
XWikiObject xobjgrp =
new XWikiObject();
xobjgrp.setClassName("XWiki.XWikiGroups");
xobjgrp.setPageId("group01");
xobjgrp.setProperty("member","XWiki.username01");
rpc.storeObject(xobjgrp);
// user 01 to group 02
XWikiObject xobjgrp =
new XWikiObject();
xobjgrp.setClassName("XWiki.XWikiGroups");
xobjgrp.setPageId("group02");
xobjgrp.setProperty("member","XWiki.username01");
rpc.storeObject(xobjgrp);
// user 02 to group 01
XWikiObject xobjgrp =
new XWikiObject();
xobjgrp.setClassName("XWiki.XWikiGroups");
xobjgrp.setPageId("group01");
xobjgrp.setProperty("member","XWiki.username02");
rpc.storeObject(xobjgrp);
// user 03 to group 01
XWikiObject xobjgrp =
new XWikiObject();
xobjgrp.setClassName("XWiki.XWikiGroups");
xobjgrp.setPageId("group01");
xobjgrp.setProperty("member","XWiki.username02");
rpc.storeObject(xobjgrp);
...
If now we check the group via UI, we can find Pages named
XWiki.group01 , XWiki.group02, and with only one user each (although
in the code up there username 01, 02 and 03 should be members of
group01 !!! only THE LAST ONE (username03) remains.
Also we tried to add also some users to the default (pre-created)
XWiki.XWikiAdminGroup so they can act as administrators:
XWikiObject xobjgrp =
new XWikiObject();
xobjgrp.setClassName("XWiki.XWikiGroups");
xobjgrp.setPageId("XWiki.XWikiAdminGroup");
xobjgrp.setProperty("member","XWiki.username01");
rpc.storeObject(xobjgrp);
XWikiObject xobjgrp =
new XWikiObject();
xobjgrp.setClassName("XWiki.XWikiGroups");
xobjgrp.setPageId("XWiki.XWikiAdminGroup");
xobjgrp.setProperty("member","XWiki.username02");
rpc.storeObject(xobjgrp);
As before only XWiki.username02 stays as member of
XWiki.XWikiAdminGroup.
We tried this with a fresh install of XWikiEnt3.0+Jetty+HSQLDB, then
in Tomcat+MySQL, same problem in all of them.
So, are we doing something wrong? Maybe something must be done with
every user or group before you can add membership via XML-RPC?
Sorry for the long post and thanks in advance!
Bernardo Riveira
*****
Este mensaje se dirige exclusivamente a su destinatario. Puede contener
información privilegiada, confidencial o legalmente protegida.
Si ha recibido este mensaje por error le rogamos que lo borre
inmediatamente, así como todas sus copias, y lo comunique al remitente.
En virtud de la legislación vigente está prohibida la utilización,
divulgación copia o impresión sin autorización.
No existe renuncia a la confidencialidad o privilegio por causa de una
transmisión errónea.
*****
_______________________________________________
devs mailing list
devs(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/devs
_______________________________________________
devs mailing list
devs(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/devs
*****
Este mensaje se dirige exclusivamente a su destinatario. Puede contener información
privilegiada, confidencial o legalmente protegida.
Si ha recibido este mensaje por error le rogamos que lo borre inmediatamente, así como
todas sus copias, y lo comunique al remitente.
En virtud de la legislación vigente está prohibida la utilización, divulgación copia o
impresión sin autorización.
No existe renuncia a la confidencialidad o privilegio por causa de una transmisión
errónea.
*****
_______________________________________________
devs mailing list
devs(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/devs
*****
Este mensaje se dirige exclusivamente a su destinatario. Puede contener información
privilegiada, confidencial o legalmente protegida.
Si ha recibido este mensaje por error le rogamos que lo borre inmediatamente, así como
todas sus copias, y lo comunique al remitente.
En virtud de la legislación vigente está prohibida la utilización, divulgación copia o
impresión sin autorización.
No existe renuncia a la confidencialidad o privilegio por causa de una transmisión
errónea.
*****
_______________________________________________
devs mailing list
devs(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/devs
*****
Este mensaje se dirige exclusivamente a su destinatario. Puede contener información
privilegiada, confidencial o legalmente protegida.
Si ha recibido este mensaje por error le rogamos que lo borre inmediatamente, así como
todas sus copias, y lo comunique al remitente.
En virtud de la legislación vigente está prohibida la utilización, divulgación copia o
impresión sin autorización.
No existe renuncia a la confidencialidad o privilegio por causa de una transmisión
errónea.
*****
_______________________________________________
devs mailing list
devs(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/devs
_______________________________________________
devs mailing list
devs(a)xwiki.org
*****
Este mensaje se dirige exclusivamente a su destinatario. Puede contener información
privilegiada, confidencial o legalmente protegida.
Si ha recibido este mensaje por error le rogamos que lo borre inmediatamente, así como
todas sus copias, y lo comunique al remitente.
En virtud de la legislación vigente está prohibida la utilización, divulgación copia o
impresión sin autorización.
No existe renuncia a la confidencialidad o privilegio por causa de una transmisión
errónea.
*****