<commit_before>import React, { PropTypes } from 'react';
import _ from 'lodash';
import moment from 'moment';
import { connect } from 'react-redux';
//
import * as Basic from '../../components/basic';
import * as Advanced from '../../components/advanced';
import * as Utils from '../../utils';
import { IdentityContractManager, RoleTreeNodeManager, RoleManager, DataManager, IdentityRoleManager } from '../../redux';
import SearchParameters from '../../domain/SearchParameters';
import FormInstance from '../../domain/FormInstance';

const identityRoleManager = new IdentityRoleManager();
const identityContractManager = new IdentityContractManager();
const roleTreeNodeManager = new RoleTreeNodeManager();
const roleManager = new RoleManager();
let selectedRole = null;
let selectedIdentityRole = null;
const uiKeyIdentityRoleFormInstance = 'identity-role-form-instance';
const uiKeyRoleAttributeFormDefinition = 'role-attribute-form-definition';

/**
 * Detail of role concept request
 *
 * @author VS
 */
export class RoleConceptDetail extends Basic.AbstractContent {

  constructor(props, context) {
    super(props, context);
  }

  componentDidMount() {
    super.componentDidMount();
    // We have to create concept from props here, because same instance this component
    //  could be used in past (in this case may be this.prosp and nextProps same)
    this._initComponent(this.props);
  }

  getContentKey() {
    return 'content.task.IdentityRoleConceptTable';
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps && (
      JSON.stringify(nextProps.entity) !== JSON.stringify(this.props.entity) ||
      nextProps.isEdit !== this.props.isEdit ||
      nextProps.multiAdd !== this.props.multiAdd
    )) {
      this._initComponent(nextProps);
    }
  }

  getEavForm() {
    return this.refs.eavForm;
  }

  getForm() {
    return this.refs.form;
  }

  _initComponent(props) {
    const entity = props.entity;
    if (!entity) {
      return;
    }

    const entityFormData = _.merge({}, entity, {
      role: entity._embedded && entity._embedded.role ? entity._embedded.role : null
    });
    if (entityFormData.role && entityFormData.role.identityRoleAttributeDefinition) {
      selectedRole = entityFormData.role;
      selectedIdentityRole = entityFormData;
      this.context.store.dispatch(roleManager.fetchAttributeFormDefinition(entityFormData.role.id, `${uiKeyRoleAttributeFormDefinition}-${entityFormData.role.id}`, (json, error) => {
        this.handleError(error);
      }));

      if (selectedIdentityRole.id && selectedIdentityRole.operation !== 'ADD' ) {
        // Form definition will be loaded from identityRole only if selectedIdentityRole is identity-role not concept
        if (selectedIdentityRole.state === undefined) {
          this.context.store.dispatch(identityRoleManager.fetchFormInstances(selectedIdentityRole.id, `${uiKeyIdentityRoleFormInstance}-${selectedIdentityRole.id}`, (formInstances, error) => {
            if (error) {
              this.addErrorMessage({ hidden: true, level: 'info' }, error);
              this.setState({ error });
            }
          }));
        }
      } else {
        selectedIdentityRole = null;
      }
    } else {
      selectedRole = null;
      selectedIdentityRole = null;
    }
    if (this.refs.role) {
      this.refs.role.focus();
    }
  }

  /**
   * Pre-fill valid-from by contract validity
   */
  _onChangeSelectOfContract(value) {
    const{entity, isEdit, multiAdd} = this.props;

    let validFrom = value ? value.validFrom : null;
    const now = moment().utc().valueOf();
    if (validFrom && moment(validFrom).isBefore(now)) {
      validFrom = now;
    }
    const entityFormData = _.merge({}, entity);
    entityFormData.validFrom = validFrom;
    entityFormData.identityContract = value;
    this._initComponent({entity: entityFormData, isEdit, multiAdd});

    return true;
  }

  _onChangeSelectOfRole(value, originalValue) {
    if (!_.isArray(originalValue) || originalValue.length === 1) {
      selectedRole = _.isArray(originalValue) ? originalValue[0] : originalValue;
      if (selectedRole.identityRoleAttributeDefinition) {
        this.context.store.dispatch(roleManager.fetchAttributeFormDefinition(selectedRole.id, `${uiKeyRoleAttributeFormDefinition}-${selectedRole.id}`, (json, error) => {
          this.handleError(error);
        }));
      }
    } else {
      selectedRole = null;
    }

    return true;
  }


  render() {
    const {
      showLoading,
      identityUsername,
      readOnly,
      _identityRoleAttributeDefinition,
      _identityRoleFormInstance,
      style,
      entity,
      isEdit,
      multiAdd} = this.props;

    if (!entity) {
      return null;
    }

    let _formInstance = null;
    let _showEAV = false;
    if (selectedRole && selectedRole.identityRoleAttributeDefinition) {
      _showEAV = true;
    }
    if (selectedRole && _identityRoleAttributeDefinition) {
      if ( entity
        && entity._eav
        && entity._eav.length === 1) {
        _formInstance = new FormInstance(_identityRoleAttributeDefinition, entity._eav[0].values);
      } else if (multiAdd) {
        _formInstance = new FormInstance(_identityRoleAttributeDefinition, null);
      }
    }
    if (!_formInstance && _identityRoleFormInstance && _identityRoleFormInstance.size === 1) {
      _identityRoleFormInstance.map(instance => {
        _formInstance = instance;<commit_msg>#1377 - Role attributes - Fixed problem with concept detail (on change of contract)<commit_after>import React, { PropTypes } from 'react';
import _ from 'lodash';
import moment from 'moment';
import { connect } from 'react-redux';
//
import * as Basic from '../../components/basic';
import * as Advanced from '../../components/advanced';
import * as Utils from '../../utils';
import { IdentityContractManager, RoleTreeNodeManager, RoleManager, DataManager, IdentityRoleManager } from '../../redux';
import SearchParameters from '../../domain/SearchParameters';
import FormInstance from '../../domain/FormInstance';

const identityRoleManager = new IdentityRoleManager();
const identityContractManager = new IdentityContractManager();
const roleTreeNodeManager = new RoleTreeNodeManager();
const roleManager = new RoleManager();
let selectedRole = null;
let selectedIdentityRole = null;
const uiKeyIdentityRoleFormInstance = 'identity-role-form-instance';
const uiKeyRoleAttributeFormDefinition = 'role-attribute-form-definition';

/**
 * Detail of role concept request
 *
 * @author VS
 */
export class RoleConceptDetail extends Basic.AbstractContent {

  constructor(props, context) {
    super(props, context);
  }

  componentDidMount() {
    super.componentDidMount();
    // We have to create concept from props here, because same instance this component
    //  could be used in past (in this case may be this.prosp and nextProps same)
    this._initComponent(this.props);
  }

  getContentKey() {
    return 'content.task.IdentityRoleConceptTable';
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps && (
      JSON.stringify(nextProps.entity) !== JSON.stringify(this.props.entity) ||
      nextProps.isEdit !== this.props.isEdit ||
      nextProps.multiAdd !== this.props.multiAdd
    )) {
      this._initComponent(nextProps);
    }
  }

  getEavForm() {
    return this.refs.eavForm;
  }

  getForm() {
    return this.refs.form;
  }

  _initComponent(props) {
    const entity = props.entity;
    if (!entity) {
      return;
    }

    const entityFormData = _.merge({}, entity, {
      role: entity._embedded && entity._embedded.role ? entity._embedded.role : null
    });
    if (entityFormData.role && entityFormData.role.identityRoleAttributeDefinition) {
      selectedRole = entityFormData.role;
      selectedIdentityRole = entityFormData;
      this.context.store.dispatch(roleManager.fetchAttributeFormDefinition(entityFormData.role.id, `${uiKeyRoleAttributeFormDefinition}-${entityFormData.role.id}`, (json, error) => {
        this.handleError(error);
      }));

      if (selectedIdentityRole.id && selectedIdentityRole.operation !== 'ADD' ) {
        // Form definition will be loaded from identityRole only if selectedIdentityRole is identity-role not concept
        if (selectedIdentityRole.state === undefined) {
          this.context.store.dispatch(identityRoleManager.fetchFormInstances(selectedIdentityRole.id, `${uiKeyIdentityRoleFormInstance}-${selectedIdentityRole.id}`, (formInstances, error) => {
            if (error) {
              this.addErrorMessage({ hidden: true, level: 'info' }, error);
              this.setState({ error });
            }
          }));
        }
      } else {
        selectedIdentityRole = null;
      }
    } else {
      selectedRole = null;
      selectedIdentityRole = null;
    }
    if (this.refs.role) {
      this.refs.role.focus();
    }
  }

  /**
   * Pre-fill valid-from by contract validity
   */
  _onChangeSelectOfContract(value) {
    const entity = this.state && this.state.entity ? this.state.entity : this.props.entity;
    let validFrom = value ? value.validFrom : null;
    const now = moment().utc().valueOf();
    if (validFrom && moment(validFrom).isBefore(now)) {
      validFrom = now;
    }
    const entityFormData = _.merge({}, entity);
    entityFormData.validFrom = validFrom;
    entityFormData.identityContract = value;
    if ( this.refs.role) {
      entityFormData.role = this.refs.role.getValue();
    }

    this.setState({entity: entityFormData});

    return true;
  }

  _onChangeSelectOfRole(value, originalValue) {
    if (!_.isArray(originalValue) || originalValue.length === 1) {
      selectedRole = _.isArray(originalValue) ? originalValue[0] : originalValue;
      if (selectedRole.identityRoleAttributeDefinition) {
        this.context.store.dispatch(roleManager.fetchAttributeFormDefinition(selectedRole.id, `${uiKeyRoleAttributeFormDefinition}-${selectedRole.id}`, (json, error) => {
          this.handleError(error);
        }));
      }
    } else {
      selectedRole = null;
    }

    return true;
  }

  render() {
    const {
      showLoading,
      identityUsername,
      readOnly,
      _identityRoleAttributeDefinition,
      _identityRoleFormInstance,
      style,
      isEdit,
      multiAdd} = this.props;

    const entity = this.state && this.state.entity ? this.state.entity : this.props.entity;
    if (!entity) {
      return null;
    }

    let _formInstance = null;
    let _showEAV = false;
    if (selectedRole && selectedRole.identityRoleAttributeDefinition) {
      _showEAV = true;
    }
    if (selectedRole && _identityRoleAttributeDefinition) {
      if ( entity
        && entity._eav
        && entity._eav.length === 1) {
        _formInstance = new FormInstance(_identityRoleAttributeDefinition, entity._eav[0].values);
      } else if (multiAdd) {
        _formInstance = new FormInstance(_identityRoleAttributeDefinition, null);
      }
    }
    if (!_formInstance && _identityRoleFormInstance && _identityRoleFormInstance.size === 1) {
      _identityRoleFormInstance.map(instance => {
        _formInstance = instance;
{'fraction_non_alphanumeric': 0.11910959518894175, 'fraction_numerical': 0.0012566196930257607, 'mean_word_length': 3.2365019011406844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '832954', 'n_tokens_mistral': 3098, 'n_tokens_neox': 3058, 'n_words': 673}