Search code examples
javascriptajaxodooalfrescoodoo-10

Alfresco and Odoo integration based on Requirement


requirement is when ever attach files (ir_attachment) these file store also alfresco

posted below code in alfresco-folder/tomcat/conf/web.xml this code allow my odoo server to access alfresco source

<!-- CORS Filter Begin -->
<filter>
    <filter-name>CORS</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    <init-param>
      <param-name>cors.allowed.origins</param-name>
      <param-value>http://192.99.76.114:8068</param-value>
    </init-param>
    <init-param>
      <param-name>cors.allowed.methods</param-name>
      <param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE</param-value>
    </init-param>
    <init-param>
      <param-name>cors.allowed.headers</param-name>
      <param-value>origin, authorization, x-file-size, x-file-name, content-type, accept, x-file-type, DNT, x-customheader ,keep-alive ,user-agent ,x-requested-with ,if-modified-since, cache-control,accept-ranges,content-encoding,content-length</param-value>
    </init-param>
    <init-param>
      <param-name>cors.exposed.headers</param-name>
      <param-value>origin, authorization, x-file-size, x-file-name, content-type, accept, x-file-type, DNT, x-customheader ,keep-alive ,user-agent ,x-requested-with ,if-modified-since, cache-control,accept-ranges,content-encoding,content-length</param-value>
    </init-param>
    <init-param>
      <param-name>cors.support.credentials</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>cors.preflight.maxage</param-name>
      <param-value>3600</param-value>
    </init-param>
 </filter>
 <!-- CORS Filter End -->

 <!-- CORS Filter Mappings Begin -->
 <filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/api/*</url-pattern>
    <url-pattern>/service/*</url-pattern>
    <url-pattern>/s/*</url-pattern>
    <url-pattern>/cmisbrowser/*</url-pattern>
 </filter-mapping>

below js code written for storing every attachment store alfresco server these code writen cmis js library.

odoo.define('alfresco_odoo', function(require) {
    "use strict";

    console.log("44444444444");
    var core = require('web.core');
    var data = require('web.data');
    var framework = require('web.framework');
    var sideWidget = require('web.Sidebar');
    var Model = require('web.Model');
    var Dialog = require('web.Dialog');
    var _t = core._t;
    var QWeb = core.qweb;


    var sidebar = sideWidget.include({


        events: {
            'change .btn-file :file': 'on_file_change',
            'click button.oe_highlight': 'on_click_att',
        },


        redraw: function() {

            var self = this;
            this._super.apply(this, arguments);
            if (this.getParent()) {
                var view = this.getParent();

                if (view.fields_view && view.fields_view.type === "form") {
                    this.$('.o_form_binary_form .o_form_input_file').change(this.on_cmis_document_creation);                                 
                }
            }
        },


        on_cmis_document_creation: function(e) {
            var self = this;

            this.cmis_session_initialized = $.Deferred();
            this.cmis_config_loaded = $.Deferred();
            <!-- 222.129.299.200:8080 this is public ip -->                         
            self.cmis_session = cmis.createSession("http://222.129.299.200:8080/alfresco/api/-default-/public/cmis/versions/1.1/browser");
            self.cmis_session.setGlobalHandlers(this.on_cmis_error, this.on_cmis_error);
            self.cmis_session_initialized.resolve();
            self.cmis_config_loaded.resolve();
            var $e = $(e.target);

            if ($e.val() !== '') {
                this.$('form.o_form_binary_form').submit();
                var self = this;
                var input = this.$el.find("input[type='file']")[0],
                    numFiles = input.files ? input.files.length : 1;
                var processedFiles = [];
                if (numFiles > 0) {
                    framework.blockUI();
                }

                var dfd = $.Deferred();
                    console.log("/...........4555")
                    if (self.cmis_session.repositories) {
                        console.log("/.");
                        console.log("here repositories created");
                        return dfd.resolve();
                    } else {
                        console.log("/./'''''''''''''");
                        self.cmis_session
                            .loadRepositories()
                            .ok(function(data) {
                                console.log("/./'''''''''''''>>>>>");
                                var root_folder_path_id = data['-default-']['rootFolderId'];
                                var actChild = null;
                                var root_foldername = 'Test Calibration Schedule and Plan Documents';
                                console.log("root json" + JSON.stringify(root_folder_path_id, null, 4));
                                self.cmis_session
                                    .getChildren(root_folder_path_id, {
                                        includeAllowableActions: true,

                                    }).ok(function(child) {
                                        var i;
                                        var folderList = [];
                                        var processedFiles = [];
                                        for (i = 0; i <= child['objects'].length - 1; i++) {

                                            folderList.push(child['objects'][i]['object']['succinctProperties']['cmis:name']);

                                        }



                                        if (folderList.indexOf(root_foldername) == -1) {
                                            self.cmis_session.createFolder(data['-default-']['rootFolderId'], root_foldername).ok(function(childfolder) {
                                                console.log("111111111" + JSON.stringify(childfolder, null, 4));
                                                self.child_folder_id = childfolder['succinctProperties']['cmis:objectId'];
                                                _.each(input.files, function(file, index, list) {

                                                    self.cmis_session
                                                        .createDocument(self.child_folder_id, file, {
                                                            'cmis:name': file.name
                                                        }, file.mimeType)
                                                        .ok(function(datat) {
                                                            processedFiles.push(datat);
                                                            console.log("Document Creation" + datat)
                                                            console.log("Docmentation" + JSON.stringify(datat, null, 4));
                                                        });
                                                }, self);

                                                framework.unblockUI();

                                            });

                                        } else {
                                            var j;
                                            for (j = 0; j <= child['objects'].length - 1; j++) {

                                                if (child['objects'][j]['object']['succinctProperties']['cmis:name'] == root_foldername) {
                                                    self.childfolder_id = child['objects'][j]['object']['succinctProperties']['cmis:objectId'];
                                                    _.each(input.files, function(file, index, list) {
                                                        console.log("hhhhhhhhhhhhhh" + file + "" + index + list)
                                                        self.cmis_session
                                                            .createDocument(self.childfolder_id, file, {
                                                                'cmis:name': file.name
                                                            }, file.mimeType)
                                                            .ok(function(datat) {
                                                                processedFiles.push(datat);
                                                                console.log("wwwwwwwwwwwwww" + datat);
                                                                console.log("Document Creation exists folder" + datat)
                                                                console.log("Docmentation" + JSON.stringify(datat, null, 4));
                                                            })
                                                            .notOk(function(error) {
                                                                console.log("././.,/./."+JSON.stringify(error, null, 4));
                                                                console.log("././.,/./."+error['body']['exception'] +error['body']['message'] );
                                                                new Dialog(this, {
                                                                    title: _t(error['body']['exception']),
                                                                    size: 'medium',
                                                                    $content: $("<div/>").html(_t(error['body']['message']))
                                                                }).open();
                                                                dfd.reject(error);

                                                            });
                                                    }, self);

                                                    framework.unblockUI();

                                                    break;
                                                }

                                            }

                                        }

                                    });


                                dfd.resolve();
                            })
                            .notOk(function(error) {
                                console.log("Docmentation" + JSON.stringify(error, null, 4));
                                new Dialog(this, {
                                    title: _t("Warning"),
                                    size: 'medium',
                                    $content: $("<div/>").html(_t("Problem In Alfresco Repository..Please Check Your Connection"))
                                }).open();

                            });
                    }



            }

        },


        on_cmis_error: function(error) {
            console.log("././.,/./."+JSON.stringify(error, null, 4));
            framework.unblockUI();
            new Dialog(this, {
                title: _t("Warning"),
                size: 'medium',
                $content: $("<div/>").html(_t("Alfresco DMS Have Some Problem...Please Check Your Connection"))
            }).open();
        },


    });

});

xml file : added js file.

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <template id="assets_backend_niwe" name="alfresco_odoo assets" inherit_id="web.assets_backend">
            <xpath expr="." position="inside">

                <script type="text/javascript" src="/alfresco_odoo/static/src/js/cmisjs/superagent.7.2.0.js"/>
                <script type="text/javascript" src="/alfresco_odoo/static/src/js/cmisjs/cmis.0.3.1.js"/>
                <script type="text/javascript" src="/alfresco_odoo/static/src/js/niwe_custom.js"/>

            </xpath>
        </template>
    </data>
</odoo>

most of the thinks i did corrent

but i got error CORS header 'Access-Control-Allow-Origin' missing)

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://222.129.299.200:8080/alfresco/api/-default-/public/cmis/versions/1.1/browser. (Reason: CORS header 'Access-Control-Allow-Origin' missing).


Solution

  • Looks to me like you need to adjust the CORS filter on the Alfresco server. See if this page in the Alfresco docs helps.