cancel
Showing results for 
Search instead for 
Did you mean: 

Change Name and Title for document

irenailievska
Champ on-the-rise
Champ on-the-rise
I need to create pop-up dialog action to change the name and title of the documents. I tried doing this by using Java, but even if I change the name and the title there is one thing that I can not change. When I open from the admin console Node Browser and find the node that needed change of name and title I see that the old name stands for the node.
Ex:
I have document Proposition.pdf in folder Folder 1.
It has:
cm:name = Proposition.pdf
cm:title = Proposition.pdf
…. other stuff….
and the child name when I see it from the parent folder cmSmiley Tongueroposition.pdf
==========================================================================
I want to change the name from Proposition.pdf to New proposition.pdf
After I click on the action the I get:
============================================================================
cm:name = New proposition.pdf
cm:title = New proposition.pdf
…. other stuff….
and the child name when I see it from the parent folder is the old name cmSmiley Tongueroposition.pdf.
It turns out if I try to execute from the java script console from the admin panel :
document.name = "New proposition.pdf"; I get the thing that I want. But I have no idea how to do this in Java. Is there a way to do this in java code or to execute java script in the java class ?
I don't know how to create JavaScript functions such as in actions.js. If someone wants to enlighten me I would be forever grateful Smiley Very Happy

Thanks in advance,
Irena
4 REPLIES 4

rjohnson
Star Contributor
Star Contributor
Popups run in the browser, you cant do a popup in Java. You can create an Alfresco simpledialog with 2 input boxes & process the responses through a AJAX wbescript which could be Java, but for something so simple I would use the standard rest api.

I meant the ActionFormDialog Smiley Very Happy. I am a beginner in Alfresco so if possible could you please show me how to do this. I tried with java but my knowledge is limited. I tried to execute javaScript inside the java class but with no success. I have no idea how to use the standard rest api. I would appreciate it very much if you can explain this a bit more. I was looking through some tutorials but I am a bit at loss.

Thanks in advance,
Irena

rjohnson
Star Contributor
Star Contributor
From which form / screen / display do you want to pop up this dialog?

I have action and when I click on the action there is a dialog that has one text field and OK and Cancel buttons. The code is below.


I have this code in share-config-custom.xml:




<config evaluator="string-compare" condition="change-title">
      <forms>
         <form>
            <field-visibility>
               <show id="newName"/>
            </field-visibility>
            <appearance>
               <field id="newName" label-id="prop.newName">
               <!–    <control template="/org/alfresco/components/form/controls/textarea.ftl" />   –>
               </field>
            </appearance>
         </form>
      </forms>
</config>




<config evaluator="string-compare" condition="DocLibActions">
        <actions>
            <action id="change-title-doc" type="javascript"  icon="activate-reg" label="actions.document.changeTitle">
            <param name="function">onActionFormDialog</param>
            <!– Additional parameters for onFormDialog function  –>
            <param name="itemKind">action</param>
            <param name="itemId">change-title</param>
            <param name="mode">create</param>
            <param name="destination">{node.nodeRef}</param>
            <param name="successMessage">message.changeTitle.success</param>
            <param name="failureMessage">message.changeTitle.failure</param>
            <evaluator>my.evaluator.doclib.indicator.isDocumentAndArchiver</evaluator>
      </action>
        </actions>
        <actionGroups>
            <actionGroup id="document-details">
                <action index="540" id="change-title-doc" /> 
               
            </actionGroup>
            <actionGroup id="document-browse">
                <action index="540" id="change-title-doc" />            
            </actionGroup>
        </actionGroups>       
    </config>




The evaluator is written by me and is working - this is why I am not writting the code here.

This is in service-context.xml:





<bean id="change-title" class="my.action.executer.ChangeTitle" parent="action-executer">
      <property name="nodeService">
         <ref bean="NodeService" />
      </property>
      <property name="publicAction">
         <value>false</value>
      </property>
   </bean>





And last the executor ChangeTitle.java class :


package my.action.executer;

import java.util.List;
import my.model.myModel;
import javax.script.*;
import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;

public class ChangeTitle extends ActionExecuterAbstractBase {
   
   public final static String NAME = "newName";
   public final static String FILENAME = "fileName";
   public final static String FILETITLE = "fileTitle";
   
   /** The NodeService to be used by the bean */
   protected NodeService nodeService;
   
   @Override
   protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
      
      //READ PROPERTY NAME FROM DOCUMENT
      QName PROP_QNAME_NAME = QName.createQName(myModel.NAMESPACE_ALFRESCO_MODEL, "name");   
      String fileName = (String) nodeService.getProperty(actionedUponNodeRef, PROP_QNAME_NAME);
      //SPLIT NAME
      String[] array = fileName.split("_");
      
      //GET NEW NAME FROM DIALOG FORM
      String newName = (String) action.getParameterValue(NAME);
      array[3] = newName + ".pdf";
      newName = String.join("_", array);
      QName AL_NAME = QName.createQName(myModel.NAMESPACE_ALFRESCO_MODEL, "name");
      nodeService.setProperty(actionedUponNodeRef, AL_NAME, newName);
      
      //READ PROPERTY TITLE FROM DOCUMENT
      QName PROP_QNAME_TITLE = QName.createQName(myModel.NAMESPACE_ALFRESCO_MODEL, "title");   
      String fileTitle = (String) nodeService.getProperty(actionedUponNodeRef, PROP_QNAME_TITLE);
      //GET INDEX OF FIRST SPACE
      int indexSpace = fileTitle.indexOf(" ");
      
      String newTitle = fileTitle.substring(0, (indexSpace + 1)) + array[3];
      
      QName AL_TITLE = QName.createQName(myModel.NAMESPACE_ALFRESCO_MODEL, "title");
      nodeService.setProperty(actionedUponNodeRef, AL_TITLE, newTitle);
      
      
      //ScriptEngineManager manager = new ScriptEngineManager();
       //ScriptEngine engine = manager.getEngineByName("JavaScript");







       //try{
       // JavaScript code in a String
      // String script1 = (String)"function changeName(node,newName) {var nodeRef = search.findNode(node);"
      //                      + "nodeRef.name = newName; nodeRef.save();}";
   
      //    String skripta = (String)"function main() { runAsAdminUtil.runAsAdmin(changeTitle);}"
       //         + "var changeTitle = function changeTitle() {"
       //         + "/*var doc = search.findNode("+actionedUponNodeRef+");*/"
       //         + "doc.name = "+newTitle+";"
       //         + "}"
       //         + "main();";
          
          
       // evaluate script
       //engine.eval(skripta);
      
      
       //Log.debug("posle eval");
       //Invocable inv = (Invocable) engine;

       //inv.invokeFunction("changeName", actionedUponNodeRef, newTitle );  //This one works.     
       //}
       //catch(Exception e){
       //}




   }

   @Override
   protected void addParameterDefinitions(List<ParameterDefinition> paramList) {
      paramList.add(
               new ParameterDefinitionImpl(               // Create a new parameter definition to add to the list
                  NAME,                       // The name used to identify the parameter
                  DataTypeDefinition.TEXT,             // The parameter value type
                  false,                                  // Indicates whether the parameter is mandatory
                  getParamDisplayLabel(NAME)));   // The parameters display label
      
      paramList.add(
               new ParameterDefinitionImpl(               // Create a new parameter definition to add to the list
                 FILENAME,                       // The name used to identify the parameter
                  DataTypeDefinition.TEXT,             // The parameter value type
                  false,                                  // Indicates whether the parameter is mandatory
                  getParamDisplayLabel(FILENAME)));
      
      paramList.add(
               new ParameterDefinitionImpl(               // Create a new parameter definition to add to the list
                 FILETITLE,                       // The name used to identify the parameter
                  DataTypeDefinition.TEXT,             // The parameter value type
                  false,                                  // Indicates whether the parameter is mandatory
                  getParamDisplayLabel(FILETITLE)));
      
   }

   /**
   * @param nodeService The NodeService to set.
   */
   public void setNodeService(NodeService nodeService) {
      this.nodeService = nodeService;
   }

}



It changes the title and the name, but when I open the admin console and find the node through the Node Browser the link is the old one Smiley Sad

In the class there is a commented section. I was trying to execute a java script to change the name that way but I was unable. I even found some run as admin class and bean and put them in the project but still no use. Maybe there is a way to do this by creating JavaScript executer but I have no idea how to do this. If you could help me I would be really grateful Smiley Happy.