AnsweredAssumed Answered

Deploy Share Config Dinamicly

Question asked by alabbas on Sep 24, 2014
Hi Dears

I am trying to deploy form share config Dinamicly , by using this Java Code :



private static final String WORKFLOW_DEFINITION_FOLDER = "/Data Dictionary/Workflow Definitions";
   
private static final String FORM_CONFIG_UPLOAD_URL = Constant.SHARE_BASE_URL + "page/modules/module";

…..

public static void uploadFormConfig(String baseFileName) {
      
      int version = 0;
       String formConfig = generateFormConfig( version, baseFileName);
       int result = executeFormConfigUpload(formConfig);
      
       uploadStringToDocument(formConfig, WORKFLOW_DEFINITION_FOLDER, baseFileName , "application/xml");
   }
   
public static int executeFormConfigUpload(String formConfig) {
       HttpState state = new HttpState();
       state.setCredentials(new AuthScope(null, AuthScope.ANY_PORT),
             new UsernamePasswordCredentials(Constant.alfrescoAdminUser, Constant.alfrescoAdminPassword));
       System.out.println("Deploying form config XML: ");
       prettyLogXml(formConfig);
       PostMethod postMethod = new PostMethod(FORM_CONFIG_UPLOAD_URL);
       try {
          System.out.println(formConfig);
          postMethod.setRequestEntity(new StringRequestEntity(formConfig, "application/xml", "UTF-8"));
          HttpClient httpClient = new HttpClient();
          int result = httpClient.executeMethod(null, postMethod, state);
          // Display status code
          System.out.println("Response status code: " + result);
          // Display response
          System.out.println("Response body: ");
          System.out.println(postMethod.getResponseBodyAsString());
          return result;
       } catch (Throwable t) {
          System.err.println("Error: " + t.getMessage());
          t.printStackTrace();
       } finally {
          postMethod.releaseConnection();
       }
       throw new RuntimeException("Programmatic error. You shouldn't be here.");
     }
   
   
public static String generateFormConfig(int version, String baseFileName) {
       String formId = "Abass_form_" + baseFileName;
      
       if (version > 0) {
         formId += "_" + version;
       }
      
       String formConfig;
      try {
         formConfig = readFile(baseFileName).replaceAll("^(<id>[a-zA-Z0-9]*\\.</id>)$", "<id>"+formId+"</id>");
         System.out.println(formConfig);
         return formConfig;
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      return "error";
     }
   
public static void uploadStringToDocument(String string, String folderPath, String documentName, String mimetype) {
          Session cmisSession = getCmisSession();
          Folder folder = (Folder) cmisSession.getObjectByPath(folderPath);
         
          ContentStream contentStream = new ContentStreamImpl(documentName, null, mimetype, new ByteArrayInputStream(string.getBytes()));
          Document document = getDocumentFromFolder(folder.getPath(), documentName);
         
          if (document == null) {
            HashMap<String, Object> properties = new HashMap<String, Object>();
            properties.put("cmis:name", documentName.replace("D://", ""));
            properties.put("cmis:objectTypeId", "cmis:document");
            folder.createDocument(properties, contentStream, VersioningState.MAJOR);
          } else {
            document.setContentStream(contentStream, true);
          }
        }
   
public static Document getDocumentFromFolder(String folderPath, String documentName) {
          try {
            Session cmisSession = getCmisSession();
            Folder workflowDefinitionFolder = (Folder) cmisSession.getObjectByPath(folderPath);
            String path = workflowDefinitionFolder.getPath() + "/" + documentName;
            return (Document) cmisSession.getObjectByPath(path);
          } catch (CmisObjectNotFoundException e) {
            return null;
          }
        }
    
public static void prettyLogXml(String xml) {
      try {
         Transformer transformer = TransformerFactory.newInstance().newTransformer();
         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
         
         Source xmlInput = new StreamSource(new StringReader(xml));
         
         StreamResult xmlOutput = new StreamResult(new StringWriter());
         transformer.transform(xmlInput, xmlOutput);
         //LOGGER.info(xmlOutput.getWriter().toString());
      } catch (Exception e) {
         e.printStackTrace();
      }
   }




I cold this code in this Statment :



uploadFormConfig("test-form-config.XML");



the content of the file test-form-config.XML :



<?xml version="1.0" encoding="UTF-8"?>
<module>
   <id>kickstart_form_email-share-config-custom_0</id>
   <auto-deploy>true</auto-deploy>
   <configurations>
      <config evaluator="string-compare" condition="activiti$vaeOrder">
        <forms>
         <form> 
            <field-visibility>
                  <show id="vaes:nationalNumber"/>
                  <show id="vaes:agency"/>
                  <show id="vaes:provider"/>
                  <show id="vaes:fullname"/>
                  <show id="vaes:position"/>
                  ….

                  <show id="vaes:establishmentNotes"/>
                  
                  <show id="vaes:othertext"/>
            </field-visibility>
               
               <appearance>
                  <set id="info" appearance="" template="/work-flow-template/1-column-set-arabic.ftl" label-id="Info_label" />
                  
                  <field id="bpm:workflowDueDate" label-id="workflow.field.due" set="info" >
                     <control template="/org/alfresco/components/form/controls/date.ftl">
                        <control-param name="showTime">false</control-param>
                        <control-param name="submitTime">false</control-param>
                     </control>
                  </field>
                  
                  <field id="bpm:workflowPriority" label-id="workflow.field.priority" set="info">
                     <control template="/org/alfresco/components/form/controls/workflow/priority.ftl" />
                  </field>
                 
                  <set id="ApplicantInfo"  template="/work-flow-template/1-column-set-arabic.ftl" label-id="ApplicantInfo"/>
                  <field id="vaes:nationalNumber" label-id="vaes_nationalNumber"    set="ApplicantInfo"/>
                  <field id="vaes:fullname"       label-id="vaes_fullname"       set="ApplicantInfo"/>
                  <field id="vaes:agency"       label-id="vaes_agency"          set="ApplicantInfo"/>
                  <field id="vaes:provider"       label-id="vaes_provider"       set="ApplicantInfo"/>
                  <field id="vaes:position"       label-id="vaes_position"       set="ApplicantInfo"/>
                  <field id="vaes:mobile"       label-id="vaes_mobile"          set="ApplicantInfo"/>

                  <field id="vaes:dumpingNumber2"    label-id="vaes_dumpingNumber2"       set="EstablishmentRequirement">
                     <control template="/work-flow-template/fild.ftl">
                        <control-param name="style">width: 30%</control-param>
                        <control-param name="type">filed</control-param>
                     </control>
                  </field>
                  <field id="vaes:establishmentNotes" label-id="vaes_establishmentNotes"    set="EstablishmentRequirement">
                     <control template="/work-flow-template/textarea-arabic.ftl">
                         <control-param name="style">width: 95%</control-param>
                     </control>
                  </field>
                  
                  <set id="OtherRequirement"  template="/work-flow-template/1-column-set-arabic.ftl" label-id="OtherRequirement"/>
                  <field id="vaes:othertext"    label-id="vaes_othertext"    set="OtherRequirement">
                     <control template="/work-flow-template/textarea-arabic.ftl">
                         <control-param name="style">width: 95%</control-param>
                     </control>
                  </field>
               </appearance>
               
            </form>
         </forms>
      </config>
      
      
      
      
      <config evaluator="task-type" condition="vaew:reEnterTask">
        <forms>
         <form> 
            <field-visibility>
               <show id="vaes:nationalNumber"/>
               <show id="vaes:agency"/>
               <show id="vaes:provider"/>
               <show id="vaes:fullname"/>
               <show id="vaes:position"/>
               <show id="vaes:mobile"/>
               <show id="vaes:applicationDate"/>
               <show id="vaes:email"/>
                  
               ….
               
               ….
                  
               <show id="vaes:othertext"/>
                  
               <show id="transitions" />
            </field-visibility>
            <appearance>
                  <set id="info" appearance="" template="/work-flow-template/1-column-set-arabic.ftl" label-id="Info_label" />
                  <field id="bpm:workflowDueDate" label-id="workflow.field.due" set="info" >
                     <control template="/org/alfresco/components/form/controls/date.ftl">
                        <control-param name="showTime">false</control-param>
                        <control-param name="submitTime">false</control-param>
                     </control>
                  </field>
                  
                  <field id="bpm:workflowPriority" label-id="workflow.field.priority" set="info">
                     <control template="/org/alfresco/components/form/controls/workflow/priority.ftl" />
                  </field>
                  
                  <set id="ApplicantInfo"  template="/work-flow-template/1-column-set-arabic.ftl" label-id="ApplicantInfo"/>
                  <field id="vaes:nationalNumber" label-id="vaes_nationalNumber"    set="ApplicantInfo"/>
                  <field id="vaes:fullname"       label-id="vaes_fullname"       set="ApplicantInfo"/>
                  <field id="vaes:agency"       label-id="vaes_agency"          set="ApplicantInfo"/>
                  <field id="vaes:provider"       label-id="vaes_provider"       set="ApplicantInfo"/>
                  <field id="vaes:position"       label-id="vaes_position"       set="ApplicantInfo"/>
                  <field id="vaes:mobile"       label-id="vaes_mobile"          set="ApplicantInfo"/>
                  
                  …..
                  
                  …..
                  
                  <set id="response" template="/work-flow-template/1-column-set-arabic.ftl" label-id="workflow.set.response" />
                  <field id="transitions" set="response" />
               </appearance>
            </form>
         </forms>
      </config>
      
      
      
      <config evaluator="task-type" condition="vaew:provostTask">
         <forms>
            <form>
               <field-visibility>
                  <show id="vaes:nationalNumber"/>
                  <show id="vaes:agency"/>
                  <show id="vaes:provider"/>
                  <show id="vaes:fullname"/>
                  <show id="vaes:position"/>
                  <show id="vaes:mobile"/>
                  
                  ….
                  ….
                  
                  <show id="vaew:reviewOutcome1"/>
               </field-visibility>
               <appearance>
                  <set id="ApplicantInfo"  template="/work-flow-template/1-column-set-arabic.ftl" label-id="ApplicantInfo"/>
                  <field id="vaes:nationalNumber" read-only="true" label-id="vaes_nationalNumber"    set="ApplicantInfo"/>
                  <field id="vaes:fullname"       read-only="true" label-id="vaes_fullname"       set="ApplicantInfo"/>
                  <field id="vaes:agency"       read-only="true" label-id="vaes_agency"          set="ApplicantInfo"/>
                  <field id="vaes:provider"       read-only="true" label-id="vaes_provider"       set="ApplicantInfo"/>
                  <field id="vaes:position"       read-only="true" label-id="vaes_position"       set="ApplicantInfo"/>
                  <field id="vaes:mobile"       read-only="true" label-id="vaes_mobile"          set="ApplicantInfo"/>
                  
                  …..
                  …..

                  <set id="response" template="/work-flow-template/1-column-set-arabic.ftl" label-id="workflow.set.response" />
                  <field id="vaew:reviewOutcome1" set="response" >
                     <control template="/org/alfresco/components/form/controls/workflow/activiti-transitions.ftl" />
                  </field>
               </appearance>
            </form>
         </forms>
      </config>
   </configurations>
</module>



I have the result :

Response body:
{
    "status" :
  {
    "code" : 409,
    "name" : "Conflict",
    "description" : "Request could not be completed due to a conflict with the current state of the resource."
  }, 
 
  "message" : "A module already exists with the requested id", 
  "exception" : "",
 
  "callstack" :
  [
       
  ],
 
  "server" : "Spring WebScripts - v1.2.0 (Release 1392) schema 1,000",
  "time" : "Sep 24, 2014 8:59:11 AM"
}


if any one know how to do this "programmicly deploy share form config" , or what is the problem pleas tell me

thanks

Outcomes