Integration of Salesforce.com and AutoCAD WS – Part 7

<?xml encoding=”UTF-8″>By Daniel Du

In part 6, we transferred the DWG attachment to AutoCAD WS storage, in this post, we will try to open it in AutoCAD WS online editor.

In part 4, we introduced how to pass parameters between visual force page and apex controller, please pay attention to the code in bold:

<span class="kwrd"><font color="#000000"><</font></span><font color="#000000"><font><span class="html">script</span> <span class="attr">type</span><span class="kwrd">="text/javascript"</span><span class="kwrd">></span>
<strong><span class="kwrd">function</span> openInWS(url){
window.open(url);
}
</strong><span class="kwrd"></</span><span class="html">script</span></font><span class="kwrd"><font>></font></span></font>
<span class="kwrd"><font color="#000000"><</font></span><font color="#000000"><font><span class="html">apex:column</span> <span class="attr">id</span><span class="kwrd">="colOpenInAcadWS"</span>
<span class="attr">rendered</span><span class="kwrd">="{!CONTAINS(item.ContentType, 'application/x-dwg')}"</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">p</span> <span class="attr">id</span><span class="kwrd">="{!count}.openInAcadWS"</span><span class="kwrd">></</span><span class="html">p</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:commandLink</span> <span class="attr">action</span><span class="kwrd">="{!transferToWSStorage}"</span>
<span class="attr">title</span><span class="kwrd">="Open In AutoCAD WS"</span>   <br>         <span class="attr">id</span><span class="kwrd">="btnOpenInAutoCADWs"</span>
<span class="attr">value</span><span class="kwrd">="Open In AutoCAD WS"</span>
<span class="attr">styleClass</span><span class="kwrd">="btn"</span>  <span class="attr">style</span><span class="kwrd">="text-decoration:none"</span>
<strong><span class="attr">oncomplete</span><span class="kwrd">="openInWS('{!openInAutocadWSUrl}');"</span></strong> <span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:param</span> <span class="attr">name</span><span class="kwrd">="selectedAttachmentId"</span>
<span class="attr">value</span><span class="kwrd">="{!item.Id}"</span><span class="kwrd">/></span>
<span class="kwrd"></</span><span class="html">apex:commandLink</span><span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">apex:pageBlock</span> <span class="attr">id</span><span class="kwrd">="hiddenBlock"</span> <span class="attr">rendered</span><span class="kwrd">="false"</span><span class="kwrd">></span>
<span class="kwrd"></</span><span class="html">apex:pageBlock</span><span class="kwrd">></span>
<span class="kwrd"></</span><span class="html">apex:column</span></font><span class="kwrd"><font>></font></span></font>

In controller:

//set openInAutocadWSUrl so that it can be

//opened in AutoCAD WS


openInAutocadWSUrl = client

     .getOpenDrawingUrl
(dstRelativePath);

A command link apex tag invokes an action of apex controller. In the controller, we transferred the DWG attachment to AutoCAD WS storage, and build the URL to open in WS online editor. Once the action is completed, a JavaScript function “oncomplete” will be invoked, we just use window.open() to launch the URL in a new window. We have already transferred the DWG to AutoCAD storage, now we are going to build the launching URL.

This is the implementation of getOpenDrawingURL, it returns the URL to open DWG files in AutoCAD WS online editor:

  //===========Open drawing  begin ===============

 

  //get the url to open in AutoCAD WS

  public string getOpenDrawingUrl(string path){

   

    OpenInWSHandler openHander = new OpenInWSHandler();

    String openInAutocadWSUrl = openHander

           .Open
(userName, password, path);

   

    return openInAutocadWSUrl;

  }

 

  //===========Open drawing  end ===============

I create a class OpenInWSHandler. Firstly we need to pass the username and password to AutoCAD WS authenticate URL to get the authentication token. after we get the token, we can use it to build the “AutoCAD-WS-Open-URL”. Here is the source code:

Public Class OpenInWSHandler {

      string AUTHENTICATE_URL 
        =
'https://www.autocadws.com/main/auth';

      string OPEN_IN_WS_URL 
       =
'https://www.autocadws.com/main/open';



      //return the url for open in AcadWS

      public String Open(string userName,

             string password, string relativePath
){

     

        String url = '';

        string token = Authenticate(userName, password);

        //if(token.length() > 0){

          url = OPEN_IN_WS_URL + '?path=
              +
relativePath + '&token=' + token;

        //}

         return url;

      }

     

      public string Authenticate(string userName, 
                          string password

      {


        // Request must contain "Authorization: Basic"

        // HTTP header


        string tok = userName + ':' + password ;

        string hash = EncodingUtil

            .base64Encode
(Blob.valueOf(tok))

        string authorizationHeader = 'Basic ' + hash;

       

        HttpRequest req = new HttpRequest();

        req.setHeader('Authorization'

                      ,a
uthorizationHeader );

        req.setHeader('toautocadws', 'true');

        req.setMethod('GET');

        req.setEndpoint(AUTHENTICATE_URL);

       

        Http h = new Http();

        // Send the request, and return a response

        HttpResponse res = h.send(req);

        //system.assert(false,res.getStatusCode());

        //success

        if(res.getStatusCode() >= 200 
          
&& res.getStatusCode() < 300
        {


           //system.assert(false, res.getBody());

           return String.valueOf(res.getBody()); 
            
        }

        else{

          return ''; //authenticate failed.

        }

       

      }

     

}

Since we are trying to access another remote web site https://www.autocadws.com/,  we also need to to enable Salesforce to access remote site, please check Setup->Security->Remote site settings.

We are ready to give it a run, open the visual force page(please note we need to pass a case Id to list the attachments), and click the “Open In AutoCAD WS” button, a few seconds latter, a new window will launch AutoCAD WS online editor to open this DWG file.

image 

In next post, I will introduce how to integer this visual force page to your case layout, so that you do not need to input the case id in URL manually.

Good luck and have fun!


Comments

One response to “Integration of Salesforce.com and AutoCAD WS – Part 7”

  1. This code is not working it redirect to autoCad 360 and show page file not found

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading