/* Authors: * Pavel Zuna * Endi Sukma Dewata * Adam Young * Petr Vobornik * * Copyright (C) 2010-2011 Red Hat * see file 'COPYING' for use and warranty information * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ define([ 'dojo/_base/lang', './metadata', './_base/Singleton_registry', './builder', './ipa', './jquery', './reg', './text', './facet'], function(lang, metadata_provider, Singleton_registry, builder, IPA, $, reg, text) { var exp = {}; exp.entity = IPA.entity = function(spec) { spec = spec || {}; spec.policies = spec.policies || [ IPA.search_facet_update_policy, IPA.details_facet_update_policy ]; var that = IPA.object(); that.name = spec.name; that.label = text.get(spec.label); that.defines_key = spec.defines_key !== undefined ? spec.defines_key : true; that.metadata = spec.metadata; that.dialogs = $.ordered_map(); that.dialog_specs = spec.dialogs || []; that.dialogs_created = false; that.policies = IPA.entity_policies({ entity: that, policies: spec.policies }); that.facets = $.ordered_map(); that.facet_groups = $.ordered_map(); that.facet_group_specs = spec.facet_groups; that.facet_specs = spec.facets || []; that.facets_created = false; // current facet that.facet = null; that.redirect_facet = spec.redirect_facet; that.containing_entity = null; that.init = function() { if (!that.metadata) { that.metadata = that.get_default_metadata(); if (!that.metadata) { throw { expected: true, message: "Entity " + that.name + " not supported by server." }; } } that.label = text.get(that.label) || that.metadata.label || that.name; }; that.get_default_metadata = function() { return metadata_provider.get('@mo:'+that.name); }; that.get_containing_entity = function() { return that.containing_entity; }; that.dialog_build_overrides = { $pre_ops: [ function (spec, context) { spec.entity = context.entity; return spec; } ], $post_opts: [ function (obj, spec, context) { context.entity.add_dialog(obj); return obj; } ], $factory: IPA.dialog }; that.get_dialog = function(name) { //build all dialogs on the first time if(!that.dialogs_created) { that.add_dialog(that.dialog_specs); that.dialogs_created = true; } return that.dialogs.get(name); }; that.add_dialog = function(dialog) { var add = function (dialog) { dialog.entity = that; that.dialogs.put(dialog.name, dialog); }; var context = { entity: that }; dialog = builder.build('', dialog, context, that.dialog_build_overrides); if (lang.isArray(dialog)) { for (var i=0; i -1) { var direct_attribute_member = attribute_member.substring(0, index); return get_spec_by_name(facets, direct_attribute_member+'_'+other_entity); } return null; } function add_redirect_info(facet_name){ facet_name = facet_name || 'search'; if (!entity.redirect_facet){ entity.redirect_facet = facet_name; } } that.containing_entity = function(entity_name) { add_redirect_info(); entity.containing_entity = IPA.get_entity(entity_name); return that; }; that.dialog = function(spec) { if (spec instanceof Object) { spec.$factory = spec.$factory || IPA.dialog; spec.entity = entity; } else { spec = { $factory: IPA.dialog, name: spec, entity: entity }; } entity.dialog_specs.push(spec); return that; }; that.adder_dialog = function(spec) { spec.$factory = spec.$factory || IPA.entity_adder_dialog; spec.name = spec.name || 'add'; if (!spec.title) { var title = text.get('@i18n:dialogs.add_title'); var label = entity.metadata.label_singular; spec.title = title.replace('${entity}', label); } return that.dialog(spec); }; that.deleter_dialog = function(spec) { spec.$factory = spec.$factory || IPA.search_deleter_dialog; spec.name = spec.name || 'remove'; return that.dialog(spec); }; that.facet_groups(entity.facet_group_specs || that.default_facet_groups); return that; }; exp.entity_post_ops = { init: function(entity, spec, context) { if (typeof spec.enable_test === 'function') { if (!spec.enable_test()) throw { expected: true }; } if (entity.init) { entity.init(spec, context); } return entity; }, containing_entity: function(entity, spec, context) { if (spec.containing_entity) { entity.builder.containing_entity(spec.containing_entity); } return entity; }, standard_association_facets: function(entity, spec, context) { var saf = spec.standard_association_facets; if (saf) { var facet_spec; if (typeof saf === 'object') facet_spec = saf; entity.builder.standard_association_facets(facet_spec); } return entity; }, adder_dialog: function(entity, spec, context) { if (spec.adder_dialog) { entity.builder.adder_dialog(spec.adder_dialog); } return entity; }, deleter_dialog: function(entity, spec, context) { if (spec.deleter_dialog) { entity.builder.deleter_dialog(spec.deleter_dialog); } return entity; } }; exp.entity_policy = IPA.entity_policy = function(spec) { spec = spec || {}; var that = IPA.object(); that.entity = spec.entity; that.facets_created = function() { }; return that; }; exp.entity_policies = IPA.entity_policies = function(spec) { var that = IPA.object(); that.entity = spec.entity; that.policies = []; that.add_policy = function(policy) { policy.entity = that.entity; that.policies.push(policy); }; that.add_policies = function(policies) { if (!policies) return; for (var i=0; i