Search in sources :

Example 51 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class LDAPUserRegistry method mapToNode.

private NodeDescription mapToNode(Map<String, String> attributeMapping, Map<String, String> attributeDefaults, SearchResult result) throws NamingException {
    NodeDescription nodeDescription = new NodeDescription(result.getNameInNamespace());
    Attributes ldapAttributes = result.getAttributes();
    // Parse the timestamp
    Attribute modifyTimestamp = ldapAttributes.get(this.modifyTimestampAttributeName);
    if (modifyTimestamp != null) {
        try {
            nodeDescription.setLastModified(this.timestampFormat.parse(modifyTimestamp.get().toString()));
        } catch (ParseException e) {
            throw new AlfrescoRuntimeException("Failed to parse timestamp.", e);
        }
    }
    // Apply the mapped attributes
    PropertyMap properties = nodeDescription.getProperties();
    for (String key : attributeMapping.keySet()) {
        QName keyQName = QName.createQName(key, this.namespaceService);
        // cater for null
        String attributeName = attributeMapping.get(key);
        if (attributeName != null) {
            Attribute attribute = ldapAttributes.get(attributeName);
            String defaultAttribute = attributeDefaults.get(key);
            if (attribute != null) {
                String value = (String) attribute.get(0);
                if (value != null) {
                    properties.put(keyQName, value);
                }
            } else if (defaultAttribute != null) {
                properties.put(keyQName, defaultAttribute);
            } else {
                // Make sure that a 2nd sync, updates deleted ldap attributes(MNT-14026)
                properties.put(keyQName, null);
            }
        } else {
            String defaultValue = attributeDefaults.get(key);
            if (defaultValue != null) {
                properties.put(keyQName, defaultValue);
            }
        }
    }
    return nodeDescription;
}
Also used : NodeDescription(org.alfresco.repo.security.sync.NodeDescription) PropertyMap(org.alfresco.util.PropertyMap) Attribute(javax.naming.directory.Attribute) QName(org.alfresco.service.namespace.QName) Attributes(javax.naming.directory.Attributes) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ParseException(java.text.ParseException)

Example 52 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class PersonServiceLoader method main.

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    sb.append("\n").append("Usage\n").append("   PersonServiceLoader  --user=<username> --pwd=<password> --batch-count=<batch-count> --batch-size=<batch-size> --threads=<threads>\n");
    String usage = sb.toString();
    ArgumentHelper argHelper = new ArgumentHelper(usage, args);
    try {
        String user = argHelper.getStringValue("user", true, true);
        String pwd = argHelper.getStringValue("pwd", true, true);
        int batchCount = argHelper.getIntegerValue("batch-count", true, 1, Integer.MAX_VALUE);
        int batchSize = argHelper.getIntegerValue("batch-size", true, 1, Integer.MAX_VALUE);
        int threads = argHelper.getIntegerValue("threads", true, 1, Integer.MAX_VALUE);
        ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
        // Create the worker instance
        PersonServiceLoader loader = new PersonServiceLoader(ctx, batchSize, batchCount);
        loader.run(user, pwd, threads);
        // check the lazy creation
        AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
        final ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
        final AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
        final PersonService personService = serviceRegistry.getPersonService();
        final TransactionService transactionService = serviceRegistry.getTransactionService();
        final NodeService nodeService = serviceRegistry.getNodeService();
        String firstName = "" + System.currentTimeMillis();
        String lastName = String.format("%05d", -1);
        final String username = GUID.generate();
        String emailAddress = String.format("%s.%s@xyz.com", firstName, lastName);
        PropertyMap properties = new PropertyMap(7);
        properties.put(ContentModel.PROP_USERNAME, username);
        properties.put(ContentModel.PROP_FIRSTNAME, firstName);
        properties.put(ContentModel.PROP_LASTNAME, lastName);
        properties.put(ContentModel.PROP_EMAIL, emailAddress);
        NodeRef madePerson = personService.createPerson(properties);
        NodeRef homeFolder = DefaultTypeConverter.INSTANCE.convert(NodeRef.class, nodeService.getProperty(madePerson, ContentModel.PROP_HOMEFOLDER));
        if (homeFolder != null) {
            throw new IllegalStateException("Home folder created eagerly");
        }
        RetryingTransactionHelper helper = transactionService.getRetryingTransactionHelper();
        helper.doInTransaction(new RetryingTransactionCallback<Void>() {

            public Void execute() throws Throwable {
                NodeRef person = personService.getPerson(username);
                NodeRef homeFolder = DefaultTypeConverter.INSTANCE.convert(NodeRef.class, nodeService.getProperty(person, ContentModel.PROP_HOMEFOLDER));
                if (homeFolder == null) {
                    throw new IllegalStateException("Home folder not created lazily");
                }
                return null;
            }
        }, true, true);
        NodeRef autoPerson = personService.getPerson(GUID.generate());
        NodeRef autoHomeFolder = DefaultTypeConverter.INSTANCE.convert(NodeRef.class, nodeService.getProperty(autoPerson, ContentModel.PROP_HOMEFOLDER));
        if (autoHomeFolder == null) {
            throw new IllegalStateException("Home folder not created lazily for auto created users");
        }
        // All done
        ApplicationContextHelper.closeApplicationContext();
        System.exit(0);
    } catch (IllegalArgumentException e) {
        System.out.println(e.getMessage());
        argHelper.printUsage();
        System.exit(1);
    } catch (Throwable e) {
        logger.error("PersonServiceLoader (userCount, batchSize) failed.", e);
        System.exit(1);
    }
}
Also used : TransactionService(org.alfresco.service.transaction.TransactionService) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) PersonService(org.alfresco.service.cmr.security.PersonService) NodeService(org.alfresco.service.cmr.repository.NodeService) ArgumentHelper(org.alfresco.util.ArgumentHelper) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ApplicationContext(org.springframework.context.ApplicationContext) PropertyMap(org.alfresco.util.PropertyMap) ServiceRegistry(org.alfresco.service.ServiceRegistry) AuthenticationService(org.alfresco.service.cmr.security.AuthenticationService)

Example 53 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class ScriptNodeTest method testVersioningPropsDefaultChanged.

/**
 * MNT-9369
 * <p>
 * Initially the ContentModel.PROP_AUTO_VERSION and ContentModel.PROP_AUTO_VERSION_PROPS are true by defaults. We'll set them to false.
 */
@Test
public void testVersioningPropsDefaultChanged() {
    setUpBootstrap();
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            try {
                // Authenticate as the system user
                AuthenticationUtil.pushAuthentication();
                AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
                log.debug("Adding new model.");
                // Create a model node
                PropertyMap properties = new PropertyMap(1);
                properties.put(ContentModel.PROP_MODEL_ACTIVE, true);
                final NodeRef modelNode = NODE_SERVICE.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(NamespaceService.ALFRESCO_URI, "dictionaryModels"), ContentModel.TYPE_DICTIONARY_MODEL, properties).getChildRef();
                assertNotNull(modelNode);
                // Add the model content to the model node
                ContentWriter contentWriter = CONTENT_SERVICE.getWriter(modelNode, ContentModel.PROP_CONTENT, true);
                contentWriter.setEncoding("UTF-8");
                contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML);
                InputStream cmStream = getClass().getClassLoader().getResourceAsStream(TEST_CONTENT_MODEL);
                contentWriter.putContent(IOUtils.toString(cmStream));
                cmStream.close();
            } finally {
                AuthenticationUtil.popAuthentication();
            }
            return null;
        }
    });
    Map<QName, PropertyDefinition> versionableProps = DICTIONARY_SERVICE.getAspect(ContentModel.ASPECT_VERSIONABLE).getProperties();
    autoVersion = Boolean.parseBoolean(versionableProps.get(ContentModel.PROP_AUTO_VERSION).getDefaultValue());
    autoVersionProps = Boolean.parseBoolean(versionableProps.get(ContentModel.PROP_AUTO_VERSION_PROPS).getDefaultValue());
    createTestContent(false);
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            log.debug("Adding versionable aspect.");
            ScriptNode sn = new ScriptNode(testNode, SERVICE_REGISTRY);
            sn.addAspect("cm:versionable");
            return null;
        }
    });
    assertEquals("Incorrect Auto Version property.", autoVersion, NODE_SERVICE.getProperty(testNode, ContentModel.PROP_AUTO_VERSION));
    assertEquals("Incorrect Auto Version Props property.", autoVersionProps, NODE_SERVICE.getProperty(testNode, ContentModel.PROP_AUTO_VERSION_PROPS));
    revertBootstrap();
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) PropertyMap(org.alfresco.util.PropertyMap) InputStream(java.io.InputStream) QName(org.alfresco.service.namespace.QName) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) Test(org.junit.Test)

Example 54 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-repository by Alfresco.

the class PeopleTest method createUsers.

private void createUsers() throws HeuristicRollbackException, RollbackException, HeuristicMixedException, SystemException, NotSupportedException {
    txn = transactionService.getUserTransaction();
    txn.begin();
    for (UserInfo user : userInfos) {
        String username = user.getUserName();
        NodeRef nodeRef = personService.getPersonOrNull(username);
        boolean create = nodeRef == null;
        if (create) {
            PropertyMap testUser = new PropertyMap();
            testUser.put(ContentModel.PROP_USERNAME, username);
            testUser.put(ContentModel.PROP_FIRSTNAME, user.getFirstName());
            testUser.put(ContentModel.PROP_LASTNAME, user.getLastName());
            testUser.put(ContentModel.PROP_EMAIL, user.getUserName() + "@acme.test");
            testUser.put(ContentModel.PROP_PASSWORD, "password");
            nodeRef = personService.createPerson(testUser);
        }
        userNodeRefs.add(nodeRef);
    // System.out.println((create ? "create" : "existing")+" user " + username + " nodeRef=" + nodeRef);
    }
    txn.commit();
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) PropertyMap(org.alfresco.util.PropertyMap)

Example 55 with PropertyMap

use of org.alfresco.util.PropertyMap in project alfresco-remote-api by Alfresco.

the class RemoteAlfrescoTicketServiceTest method createUser.

private void createUser(String userName) {
    // if user with given user name doesn't already exist then create user
    if (this.authenticationService.authenticationExists(userName) == false) {
        // create user
        this.authenticationService.createAuthentication(userName, PASSWORD.toCharArray());
        // create person properties
        PropertyMap personProps = new PropertyMap();
        personProps.put(ContentModel.PROP_USERNAME, userName);
        personProps.put(ContentModel.PROP_FIRSTNAME, "First");
        personProps.put(ContentModel.PROP_LASTNAME, "Last");
        personProps.put(ContentModel.PROP_EMAIL, "FirstName123.LastName123@email.com");
        personProps.put(ContentModel.PROP_JOBTITLE, "JobTitle123");
        personProps.put(ContentModel.PROP_JOBTITLE, "Organisation123");
        // create person node for user
        this.personService.createPerson(personProps);
    }
}
Also used : PropertyMap(org.alfresco.util.PropertyMap)

Aggregations

PropertyMap (org.alfresco.util.PropertyMap)111 NodeRef (org.alfresco.service.cmr.repository.NodeRef)43 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)15 Test (org.junit.Test)14 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)13 QName (org.alfresco.service.namespace.QName)11 Serializable (java.io.Serializable)9 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)8 ContentReader (org.alfresco.service.cmr.repository.ContentReader)8 BaseSpringTest (org.alfresco.util.BaseSpringTest)8 FileContentReader (org.alfresco.repo.content.filestore.FileContentReader)7 DictionaryException (org.alfresco.service.cmr.dictionary.DictionaryException)7 ExpectedException (org.junit.rules.ExpectedException)7 ApplicationContext (org.springframework.context.ApplicationContext)7 StoreRef (org.alfresco.service.cmr.repository.StoreRef)6 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)5 NodeService (org.alfresco.service.cmr.repository.NodeService)5 Date (java.util.Date)4 HashMap (java.util.HashMap)4 ServiceRegistry (org.alfresco.service.ServiceRegistry)4