0

I need to convert this javascript file into java code.Please help

if (document.isContainer && document.displayPath == "/Company Home/User Homes") {
    var owner = document.properties["cm:owner"];
    var pNode = people.getPerson(owner);
    if (pNode!=null && pNode.exists()){

        var userName = pNode.properties.userName;
        var email = pNode.properties.email;
        var randPassword = Math.random().toString(36).substr(2, 30)+"-"+(Date.now());

        people.setPassword(userName, randPassword);
        logger.debug("Invitation mail: User "+userName+" password has been changed.");

        var mail = actions.create("mail");
        //mail.parameters.from = "noreply@customdomain";
        mail.parameters.to = email; 
        mail.parameters.subject = "Welcome to the Site, login: "+userName+", password: "+randPassword;
        mail.parameters.template = companyhome.childByNamePath("Data Dictionary/Email Templates/Invite Email Templates/invite_user_email.ftl");
        var templateModel = new Array();
        templateModel['newPassword'] = randPassword; // use ${newPassword} expression inside template
        mail.parameters.template_model = templateModel;
        mail.executeAsynchronously(document);
        logger.debug("Invitation mail has been sent to "+email);
    } else {
        logger.warn("Invitation mail: User not found: "+owner);
    }
} else {
    logger.warn("Invitation mail: Document "+document.name+" / "+document.nodeRef+" is not a user home folder.");
} 
Akah
  • 1,890
  • 20
  • 28
APM
  • 29
  • 1
  • 9
  • 1
    Is there any specific reason, you want to convert them? Both java code and this server side js will be executed on the server side. Please share, what you're trying to achieve here? – Muralidharan Deenathayalan Nov 10 '16 at 09:30
  • I have to auto generate a mail with username and random password. The requirement is in java. This script actual requires a rule and each time when a new user is created I have to run this rule.Instead of this I am trying to create a behavior which will enable me to do the same task without any manual intervention of running the rule each time. – APM Nov 10 '16 at 09:44
  • Alfresco has already this method to notify the user. org.alfresco.repo.security.person.PersonServiceImpl.notifyPerson(final String userName, final String password) – Muralidharan Deenathayalan Nov 10 '16 at 10:08

1 Answers1

1

Hope this should help you.

public void createUser()
{
    final String randPassword = getRandonPassword();
    final String userName= "someuser";
    final String email = "someuser@example.com";
    authenticationService.setAuthentication(userName, randPassword.toCharArray());
    System.out.println(randPassword);

    AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Void>()
    {
        public Void doWork() throws Exception
        {
            Map<QName, Serializable> properties = new HashMap<QName, Serializable>();
            properties.put(ContentModel.PROP_USERNAME,userName);
            properties.put(ContentModel.PROP_PASSWORD,randPassword);
            properties.put(ContentModel.PROP_EMAIL,email);
            NodeRef personNodeRef = personService.createPerson(properties);
            personService.notifyPerson(userName, randPassword);
            return null;
        }
    }, AuthenticationUtil.getSystemUserName());        

}

private String getRandonPassword()
{
    Calendar calendar = Calendar.getInstance();
    SecureRandom random = new SecureRandom();
    String randomPassword = new BigInteger(130, random).toString(32);
    randomPassword  = randomPassword +"-" + calendar.getTimeInMillis();
    return randomPassword ;        
}