cancel
Showing results for 
Search instead for 
Did you mean: 

Problem using services

dgildeh
Champ in-the-making
Champ in-the-making
Hi,

I'm developing my own Service class which uses most of the other Alfresco services such as nodeService, tenantAdminService and personService. Everything seems to be working fine except for when I use the tenantAdminService and personService.

For example - when I use the tenantAdminService to create a new tenant it appears to do everything fine, throws no exceptions, creates a new folder for the content store for that tenant, however, when I use the tenant admin console to show tenants, it doesn't appear. When I check the alfresco_attributes table where the tenants info is stored, also there is no record of the created tenant there. I've copied the code exactly as it is used by with Alfresco for the tenant admin console and test classes, but for whatever reason it doesn't work.

Secondly - I am now trying to create new users in Alfresco, and I've copied several variations of the code from Alfresco SVN, but  again - Alfresco's code works, mine doesn't. I've run it as systemUser to ensure there's no permission error. Again, no exceptions are thrown AND a NodeRef is passed with the correct person details, but when I look in Alfresco's admin console and node explorer, there is no record of the new user.

Below is an example of the code I'm using:


// Run as System user to create new user
            NodeRef newPerson = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<NodeRef>() {

                public NodeRef doWork() throws Exception {

                    // create the ACEGI Authentication instance for the new user
                    authenticationService.createAuthentication(userName, password.toCharArray());
                    //mutableAuthenticationDao.createUser(userName, password.toCharArray());

                    // create properties for Person
                    PropertyMap props = new PropertyMap();
                    props.put(ContentModel.PROP_USERNAME, userName);
                    props.put(ContentModel.PROP_FIRSTNAME, firstName);
                    props.put(ContentModel.PROP_LASTNAME, lastName);
                    props.put(ContentModel.PROP_EMAIL, email);
                    props.put(ContentModel.PROP_ORGANIZATION, account.getAccountName());

                    // create the node to represent the Person
                    NodeRef newPerson = personService.createPerson(props);
                    // ensure the user can access their own Person object
                    permissionService.setPermission(newPerson, userName, permissionService.getAllPermission(), true);

                    return newPerson;
                }
            }, AuthenticationUtil.getSystemUserName());

Any help for this extremely frustrating problem appreciated - I'm suspecting either my Spring content file is wrong or permissions on the server (I'm running Alfresco EE 3.1 trial on Windows 7) and testing these methods using some custom webscripts I've also built to go with them.

Thanks,

David
5 REPLIES 5

dgildeh
Champ in-the-making
Champ in-the-making
Just in case - here is the class bean in my Spring context file for this class:



<bean id="MyServiceImpl" class="com.test.alfresco.services.MyServiceImpl" init-method="init">
       <property name="serviceRegistry" ref="ServiceRegistry"/>
       <property name="nodeService" ref="NodeService"/>
       <property name="searchService" ref="SearchService"/>
       <property name="namespaceService" ref="NamespaceService"/>
       <property name="permissionService" ref="PermissionService" />  
       <property name="personService" ref="personService"/>
       <property name="authorityService" ref="authorityService"/>
       <property name="authenticationService" ref="authenticationService"/>
       <property name="mutableAuthenticationDao" ref="authenticationDao"/>
       <property name="dictionaryService" ref="DictionaryService"/>
       <property name="tenantAdminService" ref="tenantAdminService"/>
       <property name="transactionHelper" ref="retryingTransactionHelper"/>
       <property name="mailService" ref="mailService"/>
    </bean>

mrogers
Star Contributor
Star Contributor
How are you calling your service?

dgildeh
Champ in-the-making
Champ in-the-making
I'm calling it from a webscript. I've created a root script object, which essentially wraps the service and makes it available to the JavaScript API.

So in my JS controller file:

myService.createUser(…… while logged in as admin.

I've actually managed to get the user creation to work now since this post. Basically I think this issue is to do with transactions. The code immediately after this was failing (I was using a custom template to send an email to the new user which had a bug in it) and when I fixed that all of a sudden the users were getting created.

Is my hunch about transactions right do you think? How come Alfresco doesn't throw any exceptions when this happens? Where can you look to see if a transaction has failed? And how can you tell where Alfresco deems the transaction to start and when it ends as I would assume the transaction completed after the user was created successfully and not rolled back when an error in unrelated code after was thrown.

I still haven't tried the tenantAdminService yet.

Thanks

mrogers
Star Contributor
Star Contributor
In general, each web script executes as a single transaction so it either all succeeds or all fails.  Your email error pulled over the entire transaction.   It's easiest to consider that any error wihin a web script is fatal to the transaction.   It is problematic to try to handle an error and carry on in the java script environment. 

You may also need to consider injecting transaction definitions into your service definition file, some of your methods may make sense to be run in their own transaction(s) rather than being part of the enclosing transaction and some of them may be read-only.

dgildeh
Champ in-the-making
Champ in-the-making
Thanks Mark, that makes a lot of sense and explains why the tenantAdminService creates everything successfully but then dissapears at the end. I'll be sure to keep this in mind as I continue to develop.

Regarding your solution about transaction definitions, do you have any examples for that? I haven't seen or used those before.

Thanks,

David