예제 #1
0
    def create(self, request, workspace_id, tab_id):

        iwidget = parse_json_request(request)
        initial_variable_values = iwidget.get('variable_values', None)

        # iWidget creation
        tab = get_object_or_404(Tab.objects.select_related('workspace'),
                                workspace__pk=workspace_id,
                                pk=tab_id)
        if not request.user.is_superuser and tab.workspace.creator != request.user:
            msg = _(
                'You have not enough permission for adding iwidgets to the workspace'
            )
            return build_error_response(request, 403, msg)

        try:
            iwidget = SaveIWidget(iwidget, request.user, tab,
                                  initial_variable_values)
            iwidget_data = get_iwidget_data(iwidget,
                                            tab.workspace,
                                            user=request.user)

            return HttpResponse(json.dumps(iwidget_data),
                                content_type='application/json; charset=UTF-8',
                                status=201)
        except (CatalogueResource.DoesNotExist, Widget.DoesNotExist) as e:
            msg = _('refered widget %(widget_uri)s does not exist.') % {
                'widget_uri': iwidget['widget']
            }
            return build_error_response(request, 422, msg)
        except TypeError as e:
            return build_error_response(request, 400, e)
        except ValueError as e:
            return build_error_response(request, 422, e)
예제 #2
0
def fillWorkspaceUsingTemplate(workspace, template):

    if not isinstance(template, TemplateParser):
        template = TemplateParser(template)

    if template.get_resource_type() != 'mashup':
        raise TypeError('Unsupported resource type: %s' % template.get_resource_type())

    user = workspace.creator

    context_values = get_context_values(workspace, workspace.creator)
    processor = TemplateValueProcessor({'user': user, 'context': context_values})

    mashup_description = template.get_resource_info()

    new_values = {}
    id_mapping = {
        'operator': {},
        'widget': {},
    }
    for preference_name in mashup_description['preferences']:
        # Filter public and sharelist preferences
        if preference_name in ("public", "sharelist"):
            continue

        new_values[preference_name] = {
            'inherit': False,
            'value': mashup_description['preferences'][preference_name],
        }

    if len(new_values) > 0:
        update_workspace_preferences(workspace, new_values)

    new_forced_values = {
        'extra_prefs': [],
        'iwidget': {},
        'ioperator': {},
    }
    for param in mashup_description['params']:
        new_forced_values['extra_prefs'].append({
            'name': param['name'],
            'inheritable': False,
            'label': param.get('label'),
            'type': param.get('type'),
            'description': param.get('description'),
            'required': param.get('required'),
        })

    for tab_entry in mashup_description['tabs']:
        tab = createTab(tab_entry.get('title'), workspace, name=tab_entry['name'], allow_renaming=True)

        new_values = {}
        for preference_name in tab_entry['preferences']:
            new_values[preference_name] = {
                'inherit': False,
                'value': tab_entry['preferences'][preference_name],
            }

        if len(new_values) > 0:
            update_tab_preferences(tab, new_values)

        for resource in tab_entry['resources']:

            position = resource['position']
            rendering = resource['rendering']

            widget = get_or_add_widget_from_catalogue(resource.get('vendor'), resource.get('name'), resource.get('version'), user)

            iwidget_data = {
                "widget": widget.uri,
                "title": resource.get('title'),
                "left": float(position.get('x')),
                "top": float(position.get('y')),
                "icon_left": 0,
                "icon_top": 0,
                "zIndex": int(position.get('z')),
                "width": float(rendering.get('width')),
                "height": float(rendering.get('height')),
                "layout": int(rendering.get('layout')),
                "minimized": rendering['minimized'],
                "fulldragboard": rendering['fulldragboard'],
                "titlevisible": rendering['titlevisible'],
            }

            iwidget = SaveIWidget(iwidget_data, user, tab, commit=False)
            if resource.get('readonly'):
                iwidget.readOnly = True

            initial_variable_values = {}
            iwidget_forced_values = {}
            iwidget_info = widget.resource.get_processed_info(process_variables=True)
            for prop_name in resource['properties']:
                prop = resource['properties'][prop_name]
                read_only = prop.get('readonly')
                if prop.get('value', None) is not None:
                    value = prop['value']
                else:
                    value = iwidget_info['variables']['properties'][prop_name]['default']
                if read_only:
                    iwidget_forced_values[prop_name] = {'value': value}
                else:
                    initial_variable_values[prop_name] = processor.process(value)

            for pref_name in resource['preferences']:
                pref = resource['preferences'][pref_name]
                read_only = pref.get('readonly')
                if pref.get('value', None) is not None:
                    value = pref['value']
                    if isinstance(value, dict):
                        value = value["users"].get("%s" % workspace.creator.id, iwidget_info['variables']['preferences'][pref_name]['default'])
                else:
                    value = iwidget_info['variables']['preferences'][pref_name]['default']

                # Build multiuser structure
                if read_only:
                    iwidget_forced_values[pref_name] = {'value': value, 'hidden': pref.get('hidden', False)}
                else:
                    initial_variable_values[pref_name] = processor.process(value)
            set_initial_values(iwidget, initial_variable_values, iwidget_info, workspace.creator)
            iwidget.save()

            if len(iwidget_forced_values) > 0:
                new_forced_values['iwidget'][str(iwidget.id)] = iwidget_forced_values

            id_mapping['widget'][resource.get('id')] = {
                'id': iwidget.id,
                'name': resource.get('vendor') + "/" + resource.get('name') + "/" + resource.get('version')
            }

    # wiring
    if len(workspace.wiringStatus) == 0:
        workspace.wiringStatus = get_wiring_skeleton()

    max_id = 0

    for id_ in workspace.wiringStatus['operators'].keys():
        if int(id_) > max_id:
            max_id = int(id_)

    # Process operators info
    for operator_id, operator in mashup_description['wiring']['operators'].items():
        max_id += 1
        new_id = "%s" % max_id
        id_mapping['operator'][operator_id] = {
            'id': new_id
        }
        workspace.wiringStatus['operators'][new_id] = {
            'id': new_id,
            'name': operator['name'],
            'preferences': operator['preferences'],
            'properties': {}
        }

        ioperator_forced_values = {}
        for pref_id, pref in operator['preferences'].items():
            if pref.get('readonly', False):
                ioperator_forced_values[pref_id] = {'value': pref.get('value'), 'hidden': pref.get('hidden', False)}

            workspace.wiringStatus['operators'][new_id]["preferences"][pref_id]["value"] = {'users': {"%s" % workspace.creator.id: pref["value"]}}

        if len(ioperator_forced_values) > 0:
            new_forced_values['ioperator'][new_id] = ioperator_forced_values

    # Remap connection ids
    source_mapping = {}
    target_mapping = {}

    for connection in mashup_description['wiring']['connections']:

        if not is_valid_connection(connection, id_mapping):
            continue

        old_source_name = get_endpoint_name(connection['source'])
        old_target_name = get_endpoint_name(connection['target'])

        connection['source']['id'] = map_id(connection['source'], id_mapping)
        connection['target']['id'] = map_id(connection['target'], id_mapping)

        source_mapping[old_source_name] = get_endpoint_name(connection['source'])
        target_mapping[old_target_name] = get_endpoint_name(connection['target'])

        # Add new connection
        workspace.wiringStatus['connections'].append(connection)

    # Merging visual description...
    _remap_component_ids(id_mapping, mashup_description['wiring']['visualdescription']['components'], isGlobal=True)
    _remap_connection_endpoints(source_mapping, target_mapping, mashup_description['wiring']['visualdescription'])

    # Remap mashup description behaviours' ids
    if len(mashup_description['wiring']['visualdescription']['behaviours']) != 0:
        for behaviour in mashup_description['wiring']['visualdescription']['behaviours']:
            _remap_component_ids(id_mapping, behaviour['components'])
            _remap_connection_endpoints(source_mapping, target_mapping, behaviour)

    if len(workspace.wiringStatus['visualdescription']['behaviours']) != 0 or len(mashup_description['wiring']['visualdescription']['behaviours']) != 0:
        if len(workspace.wiringStatus['visualdescription']['behaviours']) == 0 and not is_empty_wiring(workspace.wiringStatus['visualdescription']):
            # *TODO* flag to check if the user really want to merge both workspaces.
            _create_new_behaviour(workspace.wiringStatus['visualdescription'], _("Original wiring"), _("This is the wiring description of the original workspace"))

        if len(mashup_description['wiring']['visualdescription']['behaviours']) == 0:
            _create_new_behaviour(mashup_description['wiring']['visualdescription'], _("Merged wiring"), _("This is the wiring description of the merged mashup."))

        workspace.wiringStatus['visualdescription']['behaviours'] += mashup_description['wiring']['visualdescription']['behaviours']

    # Merge global behaviour components and connections
    workspace.wiringStatus['visualdescription']['components']['operator'].update(mashup_description['wiring']['visualdescription']['components']['operator'])
    workspace.wiringStatus['visualdescription']['components']['widget'].update(mashup_description['wiring']['visualdescription']['components']['widget'])
    workspace.wiringStatus['visualdescription']['connections'] += mashup_description['wiring']['visualdescription']['connections']

    # Forced values
    normalize_forced_values(workspace)

    workspace.forcedValues['extra_prefs'] += new_forced_values['extra_prefs']
    workspace.forcedValues['iwidget'].update(new_forced_values['iwidget'])
    workspace.forcedValues['ioperator'].update(new_forced_values['ioperator'])

    workspace.save()
예제 #3
0
def fillWorkspaceUsingTemplate(workspace, template):

    if not isinstance(template, TemplateParser):
        template = TemplateParser(template)

    if template.get_resource_type() != 'mashup':
        raise Exception()

    user = workspace.creator

    user_workspace = UserWorkspace.objects.get(user=workspace.creator, workspace=workspace)
    context_values = get_context_values(user_workspace)
    processor = TemplateValueProcessor({'user': user, 'context': context_values})

    workspace_info = template.get_resource_info()

    new_values = {}
    iwidget_id_mapping = {}
    for preference_name in workspace_info['preferences']:
        new_values[preference_name] = {
            'inherit': False,
            'value': workspace_info['preferences'][preference_name],
        }

    if len(new_values) > 0:
        update_workspace_preferences(workspace, new_values)

    new_forced_values = {
        'extra_prefs': {},
        'iwidget': {},
    }
    for param_name in workspace_info['params']:
        param = workspace_info['params'][param_name]
        new_forced_values['extra_prefs'][param_name] = {
            'inheritable': False,
            'label': param.get('label'),
            'type': param.get('type'),
        }

    for tab_entry in workspace_info['tabs']:
        tab = createTab(tab_entry.get('name'), workspace, allow_renaming=True)

        new_values = {}
        for preference_name in tab_entry['preferences']:
            new_values[preference_name] = {
                'inherit': False,
                'value': tab_entry['preferences'][preference_name],
            }

        if len(new_values) > 0:
            update_tab_preferences(tab, new_values)

        for resource in tab_entry['resources']:

            position = resource['position']
            rendering = resource['rendering']

            initial_variable_values = {}
            iwidget_forced_values = {}
            for prop_name in resource['properties']:
                prop = resource['properties'][prop_name]
                read_only = prop.get('readonly')
                if read_only:
                    iwidget_forced_values[prop_name] = {'value': prop.get('value')}
                else:
                    initial_variable_values[prop_name] = processor.process(prop.get('value'))

            for pref_name in resource['preferences']:
                pref = resource['preferences'][pref_name]
                read_only = pref.get('readonly')
                if read_only:
                    iwidget_forced_values[pref_name] = {'value': pref.get('value'), 'hidden': pref.get('hidden', False)}
                else:
                    initial_variable_values[pref_name] = processor.process(pref.get('value'))

            widget = get_or_add_widget_from_catalogue(resource.get('vendor'), resource.get('name'), resource.get('version'), user, None)

            iwidget_data = {
                "left": int(position.get('x')),
                "top": int(position.get('y')),
                "icon_left": -1,
                "icon_top": -1,
                "zIndex": int(position.get('z')),
                "width": int(rendering.get('width')),
                "height": int(rendering.get('height')),
                "name": resource.get('title'),
                "layout": int(rendering.get('layout')),
                "widget": widget.uri,
            }

            iwidget = SaveIWidget(iwidget_data, user, tab, initial_variable_values)
            if resource.get('readonly'):
                iwidget.readOnly = True
                iwidget.save()

            new_forced_values['iwidget'][str(iwidget.id)] = iwidget_forced_values
            iwidget_id_mapping[resource.get('id')] = iwidget

    if workspace.forcedValues is not None and workspace.forcedValues != '':
        forced_values = json.loads(workspace.forcedValues)
    else:
        forced_values = {
            'extra_prefs': {},
            'iwidget': {},
        }

    forced_values['extra_prefs'].update(new_forced_values['extra_prefs'])
    forced_values['iwidget'].update(new_forced_values['iwidget'])
    workspace.forcedValues = json.dumps(forced_values, ensure_ascii=False)

    # wiring
    wiring_status = {
        'operators': {},
        'connections': [],
    }

    if workspace.wiringStatus != '':
        workspace_wiring_status = json.loads(workspace.wiringStatus)
    else:
        workspace_wiring_status = {
            'operators': {},
            'connections': [],
            'views': []
        }

    if 'views' not in workspace_wiring_status:
        workspace_wiring_status['views'] = []

    max_id = 0
    operators = {}

    for id_ in workspace_wiring_status['operators'].keys():
        if int(id_) > max_id:
            max_id = int(id_)

    # Change string ids by integer ids and install unavailable operators
    for id_, op in workspace_info['wiring']['operators'].iteritems():
        max_id += 1
        #mapping between string ids and integer id
        operators[id_] = max_id
        wiring_status['operators'][max_id] = {
            'id': max_id,
            'name': op['name']
        }
        op_id_args = op['name'].split('/')
        op_id_args.append(user)
        get_or_add_resource_from_available_marketplaces(*op_id_args)

    wiring_status['operators'].update(workspace_wiring_status['operators'])

    if workspace_wiring_status['connections']:
        wiring_status['connections'] = workspace_wiring_status['connections']

    for connection in workspace_info['wiring']['connections']:
        source_id = connection['source']['id']
        target_id = connection['target']['id']

        if connection['source']['type'] == 'iwidget':
            source_id = iwidget_id_mapping[source_id].id
        elif connection['source']['type'] == 'ioperator':
            source_id = operators[source_id]

        if connection['target']['type'] == 'iwidget':
            target_id = iwidget_id_mapping[target_id].id
        elif connection['target']['type'] == 'ioperator':
            target_id = operators[target_id]

        wiring_status['connections'].append({
            'readOnly': connection['readonly'],
            'source': {
                'id': source_id,
                'type': connection['source']['type'],
                'endpoint': connection['source']['endpoint'],
            },
            'target': {
                'id': target_id,
                'type': connection['target']['type'],
                'endpoint': connection['target']['endpoint'],
            },
        })

    wiring_status['views'] = workspace_wiring_status['views']

    if 'views' in workspace_info['wiring']:
        for wiring_view in workspace_info['wiring']['views']:
            iwidgets_views = {}
            for key, widget in wiring_view['iwidgets'].iteritems():
                iwidgets_views[iwidget_id_mapping[key].id] = widget

            operators_views = {}
            for key, operator in wiring_view['operators'].iteritems():
                operators_views[operators[key]] = operator

            wiring_status['views'].append({
                'iwidgets': iwidgets_views,
                'operators': operators_views,
                'label': wiring_view['label'],
                'multiconnectors': {},
                'connections': []
            })

    workspace.wiringStatus = json.dumps(wiring_status)

    workspace.save()
def fillWorkspaceUsingTemplate(workspace, template):

    if not isinstance(template, TemplateParser):
        template = TemplateParser(template)

    if template.get_resource_type() != 'mashup':
        raise TypeError('Unsupported resource type: %s' % template.get_resource_type())

    user = workspace.creator

    context_values = get_context_values(workspace, workspace.creator)
    processor = TemplateValueProcessor({'user': user, 'context': context_values})

    mashup_description = template.get_resource_info()

    new_values = {}
    id_mapping = {
        'operator': {},
        'widget': {},
    }
    for preference_name in mashup_description['preferences']:
        new_values[preference_name] = {
            'inherit': False,
            'value': mashup_description['preferences'][preference_name],
        }

    if len(new_values) > 0:
        update_workspace_preferences(workspace, new_values)

    new_forced_values = {
        'extra_prefs': [],
        'iwidget': {},
        'ioperator': {},
    }
    for param in mashup_description['params']:
        new_forced_values['extra_prefs'].append({
            'name': param['name'],
            'inheritable': False,
            'label': param.get('label'),
            'type': param.get('type'),
        })

    for tab_entry in mashup_description['tabs']:
        tab = createTab(tab_entry.get('name'), workspace, allow_renaming=True)

        new_values = {}
        for preference_name in tab_entry['preferences']:
            new_values[preference_name] = {
                'inherit': False,
                'value': tab_entry['preferences'][preference_name],
            }

        if len(new_values) > 0:
            update_tab_preferences(tab, new_values)

        for resource in tab_entry['resources']:

            position = resource['position']
            rendering = resource['rendering']

            widget = get_or_add_widget_from_catalogue(resource.get('vendor'), resource.get('name'), resource.get('version'), user)

            iwidget_data = {
                "widget": widget.uri,
                "title": resource.get('title'),
                "left": int(position.get('x')),
                "top": int(position.get('y')),
                "icon_left": 0,
                "icon_top": 0,
                "zIndex": int(position.get('z')),
                "width": int(rendering.get('width')),
                "height": int(rendering.get('height')),
                "layout": int(rendering.get('layout')),
                "minimized": rendering['minimized'],
                "fulldragboard": rendering['fulldragboard'],
            }

            iwidget = SaveIWidget(iwidget_data, user, tab, commit=False)
            if resource.get('readonly'):
                iwidget.readOnly = True

            initial_variable_values = {}
            iwidget_forced_values = {}
            iwidget_info = widget.resource.get_processed_info(process_variables=True)
            for prop_name in resource['properties']:
                prop = resource['properties'][prop_name]
                read_only = prop.get('readonly')
                if prop.get('value', None) is not None:
                    value = prop['value']
                else:
                    value = iwidget_info['variables']['properties'][prop_name]['default']
                if read_only:
                    iwidget_forced_values[prop_name] = {'value': value}
                else:
                    initial_variable_values[prop_name] = processor.process(value)

            for pref_name in resource['preferences']:
                pref = resource['preferences'][pref_name]
                read_only = pref.get('readonly')
                if pref.get('value', None) is not None:
                    value = pref['value']
                else:
                    value = iwidget_info['variables']['preferences'][pref_name]['default']
                if read_only:
                    iwidget_forced_values[pref_name] = {'value': value, 'hidden': pref.get('hidden', False)}
                else:
                    initial_variable_values[pref_name] = processor.process(value)

            set_initial_values(iwidget, initial_variable_values, iwidget_info)
            iwidget.save()

            if len(iwidget_forced_values) > 0:
                new_forced_values['iwidget'][six.text_type(iwidget.id)] = iwidget_forced_values

            id_mapping['widget'][resource.get('id')] = {
                'id': iwidget.id,
                'name': resource.get('vendor') + "/" + resource.get('name') + "/" + resource.get('version')
            }

    # wiring
    if len(workspace.wiringStatus) == 0:
        workspace.wiringStatus = get_wiring_skeleton()

    max_id = 0

    for id_ in workspace.wiringStatus['operators'].keys():
        if int(id_) > max_id:
            max_id = int(id_)

    # Process operators info
    for operator_id, operator in six.iteritems(mashup_description['wiring']['operators']):
        max_id += 1
        new_id = "%s" % max_id
        id_mapping['operator'][operator_id] = {
            'id': new_id
        }
        workspace.wiringStatus['operators'][new_id] = {
            'id': new_id,
            'name': operator['name'],
            'preferences': operator['preferences'],
        }

        ioperator_forced_values = {}
        for pref_id, pref in six.iteritems(operator['preferences']):
            if pref.get('readonly', False):
                ioperator_forced_values[pref_id] = {'value': pref.get('value'), 'hidden': pref.get('hidden', False)}

        if len(ioperator_forced_values) > 0:
            new_forced_values['ioperator'][new_id] = ioperator_forced_values

    # Remap connection ids
    source_mapping = {}
    target_mapping = {}

    for connection in mashup_description['wiring']['connections']:
        old_source_name = get_endpoint_name(connection['source'])
        old_target_name = get_endpoint_name(connection['target'])

        connection['source']['id'] = map_id(connection['source'], id_mapping)
        connection['target']['id'] = map_id(connection['target'], id_mapping)

        source_mapping[old_source_name] = get_endpoint_name(connection['source'])
        target_mapping[old_target_name] = get_endpoint_name(connection['target'])

    # Add new connections
    workspace.wiringStatus['connections'] += mashup_description['wiring']['connections']

    # Merging visual description...
    _remap_component_ids(id_mapping, mashup_description['wiring']['visualdescription']['components'], isGlobal=True)
    _remap_connection_endpoints(source_mapping, target_mapping, mashup_description['wiring']['visualdescription'])

    # Remap mashup description behaviours' ids
    if len(mashup_description['wiring']['visualdescription']['behaviours']) != 0:
        for behaviour in mashup_description['wiring']['visualdescription']['behaviours']:
            _remap_component_ids(id_mapping, behaviour['components'])
            _remap_connection_endpoints(source_mapping, target_mapping, behaviour)

    if len(workspace.wiringStatus['visualdescription']['behaviours']) != 0 or len(mashup_description['wiring']['visualdescription']['behaviours']) != 0:
        if len(workspace.wiringStatus['visualdescription']['behaviours']) == 0 and not is_empty_wiring(workspace.wiringStatus['visualdescription']):
            # *TODO* flag to check if the user really want to merge both workspaces.
            _create_new_behaviour(workspace.wiringStatus['visualdescription'], _("Original wiring"), _("This is the wiring description of the original workspace"))

        if len(mashup_description['wiring']['visualdescription']['behaviours']) == 0:
            _create_new_behaviour(mashup_description['wiring']['visualdescription'], _("Merged wiring"), _("This is the wiring description of the merged mashup."))

        workspace.wiringStatus['visualdescription']['behaviours'] += mashup_description['wiring']['visualdescription']['behaviours']

    # Merge global behaviour components and connections
    workspace.wiringStatus['visualdescription']['components']['operator'].update(mashup_description['wiring']['visualdescription']['components']['operator'])
    workspace.wiringStatus['visualdescription']['components']['widget'].update(mashup_description['wiring']['visualdescription']['components']['widget'])
    workspace.wiringStatus['visualdescription']['connections'] += mashup_description['wiring']['visualdescription']['connections']

    # Forced values
    normalize_forced_values(workspace)

    workspace.forcedValues['extra_prefs'] += new_forced_values['extra_prefs']
    workspace.forcedValues['iwidget'].update(new_forced_values['iwidget'])
    workspace.forcedValues['ioperator'].update(new_forced_values['ioperator'])

    workspace.save()
예제 #5
0
def fillWorkspaceUsingTemplate(workspace, template):

    if not isinstance(template, TemplateParser):
        template = TemplateParser(template)

    if template.get_resource_type() != "mashup":
        raise TypeError("Unsupported resource type: %s" % template.get_resource_type())

    user = workspace.creator

    context_values = get_context_values(workspace, workspace.creator)
    processor = TemplateValueProcessor({"user": user, "context": context_values})

    mashup_description = template.get_resource_info()

    new_values = {}
    id_mapping = {"operator": {}, "widget": {}}
    for preference_name in mashup_description["preferences"]:
        new_values[preference_name] = {"inherit": False, "value": mashup_description["preferences"][preference_name]}

    if len(new_values) > 0:
        update_workspace_preferences(workspace, new_values)

    new_forced_values = {"extra_prefs": [], "iwidget": {}, "ioperator": {}}
    for param in mashup_description["params"]:
        new_forced_values["extra_prefs"].append(
            {"name": param["name"], "inheritable": False, "label": param.get("label"), "type": param.get("type")}
        )

    for tab_entry in mashup_description["tabs"]:
        tab = createTab(tab_entry.get("name"), workspace, allow_renaming=True)

        new_values = {}
        for preference_name in tab_entry["preferences"]:
            new_values[preference_name] = {"inherit": False, "value": tab_entry["preferences"][preference_name]}

        if len(new_values) > 0:
            update_tab_preferences(tab, new_values)

        for resource in tab_entry["resources"]:

            position = resource["position"]
            rendering = resource["rendering"]

            widget = get_or_add_widget_from_catalogue(
                resource.get("vendor"), resource.get("name"), resource.get("version"), user
            )

            iwidget_data = {
                "left": int(position.get("x")),
                "top": int(position.get("y")),
                "icon_left": 0,
                "icon_top": 0,
                "zIndex": int(position.get("z")),
                "width": int(rendering.get("width")),
                "height": int(rendering.get("height")),
                "title": resource.get("title"),
                "layout": int(rendering.get("layout")),
                "widget": widget.uri,
            }

            iwidget = SaveIWidget(iwidget_data, user, tab, commit=False)
            if resource.get("readonly"):
                iwidget.readOnly = True

            initial_variable_values = {}
            iwidget_forced_values = {}
            iwidget_info = widget.resource.get_processed_info(process_variables=True)
            for prop_name in resource["properties"]:
                prop = resource["properties"][prop_name]
                read_only = prop.get("readonly")
                if prop.get("value", None) is not None:
                    value = prop["value"]
                else:
                    value = iwidget_info["variables"]["properties"][prop_name]["default"]
                if read_only:
                    iwidget_forced_values[prop_name] = {"value": value}
                else:
                    initial_variable_values[prop_name] = processor.process(value)

            for pref_name in resource["preferences"]:
                pref = resource["preferences"][pref_name]
                read_only = pref.get("readonly")
                if pref.get("value", None) is not None:
                    value = pref["value"]
                else:
                    value = iwidget_info["variables"]["preferences"][pref_name]["default"]
                if read_only:
                    iwidget_forced_values[pref_name] = {"value": value, "hidden": pref.get("hidden", False)}
                else:
                    initial_variable_values[pref_name] = processor.process(value)

            set_initial_values(iwidget, initial_variable_values, iwidget_info)
            iwidget.save()

            if len(iwidget_forced_values) > 0:
                new_forced_values["iwidget"][six.text_type(iwidget.id)] = iwidget_forced_values

            id_mapping["widget"][resource.get("id")] = {
                "id": iwidget.id,
                "name": resource.get("vendor") + "/" + resource.get("name") + "/" + resource.get("version"),
            }

    # wiring
    if len(workspace.wiringStatus) == 0:
        workspace.wiringStatus = get_wiring_skeleton()

    max_id = 0

    for id_ in workspace.wiringStatus["operators"].keys():
        if int(id_) > max_id:
            max_id = int(id_)

    # Process operators info
    for operator_id, operator in six.iteritems(mashup_description["wiring"]["operators"]):
        max_id += 1
        new_id = "%s" % max_id
        id_mapping["operator"][operator_id] = {"id": new_id}
        workspace.wiringStatus["operators"][new_id] = {
            "id": new_id,
            "name": operator["name"],
            "preferences": operator["preferences"],
        }

        ioperator_forced_values = {}
        for pref_id, pref in six.iteritems(operator["preferences"]):
            if pref.get("readonly", False):
                ioperator_forced_values[pref_id] = {"value": pref.get("value"), "hidden": pref.get("hidden", False)}

        if len(ioperator_forced_values) > 0:
            new_forced_values["ioperator"][new_id] = ioperator_forced_values

    # Remap connection ids
    source_mapping = {}
    target_mapping = {}

    for connection in mashup_description["wiring"]["connections"]:
        old_source_name = get_endpoint_name(connection["source"])
        old_target_name = get_endpoint_name(connection["target"])

        connection["source"]["id"] = map_id(connection["source"], id_mapping)
        connection["target"]["id"] = map_id(connection["target"], id_mapping)

        source_mapping[old_source_name] = get_endpoint_name(connection["source"])
        target_mapping[old_target_name] = get_endpoint_name(connection["target"])

    # Add new connections
    workspace.wiringStatus["connections"] += mashup_description["wiring"]["connections"]

    # Merging visual description...
    _remap_component_ids(id_mapping, mashup_description["wiring"]["visualdescription"]["components"], isGlobal=True)
    _remap_connection_endpoints(source_mapping, target_mapping, mashup_description["wiring"]["visualdescription"])

    # Remap mashup description behaviours' ids
    if len(mashup_description["wiring"]["visualdescription"]["behaviours"]) != 0:
        for behaviour in mashup_description["wiring"]["visualdescription"]["behaviours"]:
            _remap_component_ids(id_mapping, behaviour["components"])
            _remap_connection_endpoints(source_mapping, target_mapping, behaviour)

    if (
        len(workspace.wiringStatus["visualdescription"]["behaviours"]) != 0
        or len(mashup_description["wiring"]["visualdescription"]["behaviours"]) != 0
    ):
        if len(workspace.wiringStatus["visualdescription"]["behaviours"]) == 0 and not is_empty_wiring(
            workspace.wiringStatus["visualdescription"]
        ):
            # *TODO* flag to check if the user really want to merge both workspaces.
            _create_new_behaviour(
                workspace.wiringStatus["visualdescription"],
                _("Original wiring"),
                _("This is the wiring description of the original workspace"),
            )

        if len(mashup_description["wiring"]["visualdescription"]["behaviours"]) == 0:
            _create_new_behaviour(
                mashup_description["wiring"]["visualdescription"],
                _("Merged wiring"),
                _("This is the wiring description of the merged mashup."),
            )

        workspace.wiringStatus["visualdescription"]["behaviours"] += mashup_description["wiring"]["visualdescription"][
            "behaviours"
        ]

    # Merge global behaviour components and connections
    workspace.wiringStatus["visualdescription"]["components"]["operator"].update(
        mashup_description["wiring"]["visualdescription"]["components"]["operator"]
    )
    workspace.wiringStatus["visualdescription"]["components"]["widget"].update(
        mashup_description["wiring"]["visualdescription"]["components"]["widget"]
    )
    workspace.wiringStatus["visualdescription"]["connections"] += mashup_description["wiring"]["visualdescription"][
        "connections"
    ]

    # Forced values
    normalize_forced_values(workspace)

    workspace.forcedValues["extra_prefs"] += new_forced_values["extra_prefs"]
    workspace.forcedValues["iwidget"].update(new_forced_values["iwidget"])
    workspace.forcedValues["ioperator"].update(new_forced_values["ioperator"])

    workspace.save()
def fillWorkspaceUsingTemplate(workspace, template):

    if not isinstance(template, TemplateParser):
        template = TemplateParser(template)

    if template.get_resource_type() != "mashup":
        raise Exception()

    user = workspace.creator

    context_values = get_context_values(workspace, workspace.creator)
    processor = TemplateValueProcessor({"user": user, "context": context_values})

    workspace_info = template.get_resource_info()

    new_values = {}
    iwidget_id_mapping = {}
    for preference_name in workspace_info["preferences"]:
        new_values[preference_name] = {"inherit": False, "value": workspace_info["preferences"][preference_name]}

    if len(new_values) > 0:
        update_workspace_preferences(workspace, new_values)

    new_forced_values = {"extra_prefs": [], "iwidget": {}, "ioperator": {}}
    for param in workspace_info["params"]:
        new_forced_values["extra_prefs"].append(
            {"name": param["name"], "inheritable": False, "label": param.get("label"), "type": param.get("type")}
        )

    for tab_entry in workspace_info["tabs"]:
        tab = createTab(tab_entry.get("name"), workspace, allow_renaming=True)

        new_values = {}
        for preference_name in tab_entry["preferences"]:
            new_values[preference_name] = {"inherit": False, "value": tab_entry["preferences"][preference_name]}

        if len(new_values) > 0:
            update_tab_preferences(tab, new_values)

        for resource in tab_entry["resources"]:

            position = resource["position"]
            rendering = resource["rendering"]

            initial_variable_values = {}
            iwidget_forced_values = {}
            for prop_name in resource["properties"]:
                prop = resource["properties"][prop_name]
                read_only = prop.get("readonly")
                if read_only:
                    iwidget_forced_values[prop_name] = {"value": prop.get("value")}
                else:
                    initial_variable_values[prop_name] = processor.process(prop.get("value"))

            for pref_name in resource["preferences"]:
                pref = resource["preferences"][pref_name]
                read_only = pref.get("readonly")
                if read_only:
                    iwidget_forced_values[pref_name] = {"value": pref.get("value"), "hidden": pref.get("hidden", False)}
                else:
                    initial_variable_values[pref_name] = processor.process(pref.get("value"))

            widget = get_or_add_widget_from_catalogue(
                resource.get("vendor"), resource.get("name"), resource.get("version"), user, None
            )

            iwidget_data = {
                "left": int(position.get("x")),
                "top": int(position.get("y")),
                "icon_left": -1,
                "icon_top": -1,
                "zIndex": int(position.get("z")),
                "width": int(rendering.get("width")),
                "height": int(rendering.get("height")),
                "name": resource.get("title"),
                "layout": int(rendering.get("layout")),
                "widget": widget.uri,
            }

            iwidget = SaveIWidget(iwidget_data, user, tab, initial_variable_values)
            if resource.get("readonly"):
                iwidget.readOnly = True
                iwidget.save()

            if len(iwidget_forced_values) > 0:
                new_forced_values["iwidget"][str(iwidget.id)] = iwidget_forced_values

            iwidget_id_mapping[resource.get("id")] = iwidget

    # wiring
    if workspace.wiringStatus != "":
        workspace_wiring_status = json.loads(workspace.wiringStatus)
    else:
        workspace_wiring_status = {"operators": {}, "connections": [], "views": []}

    if "views" not in workspace_wiring_status:
        workspace_wiring_status["views"] = []

    max_id = 0
    ioperator_id_mapping = {}

    for id_ in workspace_wiring_status["operators"].keys():
        if int(id_) > max_id:
            max_id = int(id_)

    wiring_status = {
        "operators": workspace_wiring_status["operators"],
        "connections": workspace_wiring_status["connections"],
    }

    # Process operators info
    for operator_id, operator in six.iteritems(workspace_info["wiring"]["operators"]):
        max_id += 1
        new_id = unicode(max_id)
        ioperator_id_mapping[operator_id] = new_id
        wiring_status["operators"][new_id] = {
            "id": new_id,
            "name": operator["name"],
            "preferences": operator["preferences"],
        }

        ioperator_forced_values = {}
        for pref_id, pref in six.iteritems(operator["preferences"]):
            if pref.get("readonly", False):
                ioperator_forced_values[pref_id] = {"value": pref.get("value"), "hidden": pref.get("hidden", False)}

        if len(ioperator_forced_values) > 0:
            new_forced_values["ioperator"][new_id] = ioperator_forced_values

    for connection in workspace_info["wiring"]["connections"]:
        source_id = str(connection["source"]["id"])
        target_id = str(connection["target"]["id"])

        if connection["source"]["type"] == "iwidget":
            source_id = iwidget_id_mapping[source_id].id
        elif connection["source"]["type"] == "ioperator":
            source_id = ioperator_id_mapping[source_id]

        if connection["target"]["type"] == "iwidget":
            target_id = iwidget_id_mapping[target_id].id
        elif connection["target"]["type"] == "ioperator":
            target_id = ioperator_id_mapping[target_id]

        wiring_status["connections"].append(
            {
                "readOnly": connection["readonly"],
                "source": {
                    "id": source_id,
                    "type": connection["source"]["type"],
                    "endpoint": connection["source"]["endpoint"],
                },
                "target": {
                    "id": target_id,
                    "type": connection["target"]["type"],
                    "endpoint": connection["target"]["endpoint"],
                },
            }
        )

    wiring_status["views"] = workspace_wiring_status["views"]

    if "views" in workspace_info["wiring"]:
        for wiring_view in workspace_info["wiring"]["views"]:
            iwidgets_views = {}
            for key, widget in six.iteritems(wiring_view["iwidgets"]):
                iwidgets_views[iwidget_id_mapping[key].id] = widget

            operators_views = {}
            for key, operator in six.iteritems(wiring_view["operators"]):
                operators_views[ioperator_id_mapping[key]] = operator

            wiring_status["views"].append(
                {
                    "iwidgets": iwidgets_views,
                    "operators": operators_views,
                    "label": wiring_view["label"],
                    "multiconnectors": {},
                    "connections": [],
                }
            )

    workspace.wiringStatus = json.dumps(wiring_status)

    # Forced values
    if workspace.forcedValues is not None and workspace.forcedValues != "":
        forced_values = json.loads(workspace.forcedValues)
    else:
        forced_values = {"extra_prefs": [], "iwidget": {}, "ioperator": {}}

    if "ioperator" not in forced_values:
        forced_values["ioperator"] = {}

    if "iwidget" not in forced_values:
        forced_values["iwidget"] = {}

    forced_values["extra_prefs"] += new_forced_values["extra_prefs"]
    forced_values["iwidget"].update(new_forced_values["iwidget"])
    forced_values["ioperator"].update(new_forced_values["ioperator"])
    workspace.forcedValues = json.dumps(forced_values, ensure_ascii=False)

    workspace.save()