/*
 * $Id$
 *
 * JSDAI(TM), a way to implement STEP, ISO 10303
 * Copyright (C) 1997-2008, LKSoftWare GmbH, Germany
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License
 * version 3 as published by the Free Software Foundation (AGPL v3).
 *
 * 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * JSDAI is a registered trademark of LKSoftWare GmbH, Germany
 * This software is also available under commercial licenses.
 * See also http://www.jsdai.net/
 */

/* Generated By:JJTree: Do not edit this line. X_EntityConstructor.java */
package jsdai.expressCompiler;

import java.util.*;
import jsdai.SExtended_dictionary_schema.*;
import jsdai.lang.*;


public class X_EntityConstructor extends ExpressionNode {
  EEntity_definition ed;
  int actual_count;
  Vector arguments;
  Vector actual_parameters; // strings with argument expressions from the parse tree
  SdaiModel current_model;

  public X_EntityConstructor(int id) {
    super(id);
  }

  public X_EntityConstructor(Compiler2 p, int id) {
    super(p, id);
  }

  /** Accept the visitor. **/
  public Object jjtAccept(Compiler2Visitor visitor, Object data)
                   throws SdaiException {
    return visitor.visit(this, data);
  }

  public Object childrenAccept(Compiler2Visitor visitor, Object data)
                        throws SdaiException {
    JavaClass jc = (JavaClass) data;

    if (children != null) {
      // for contained statements
      variable_names = new Vector();
      variable_declarations = new Vector();
      statements = new Vector();
      initializing_code = new Vector();
      actual_count = 0;
      actual_parameters = new Vector();

      for (int i = 0; i < children.length; ++i) {
        children[i].jjtAccept(visitor, data);

        if (jc != null) {
          if (jc.active) {
            if (((SimpleNode) children[i]).java_contains_statements) {
              java_contains_statements = true;

              for (int j = 0; j < ((SimpleNode) children[i]).variable_names.size(); j++) {
                variable_names.add(((SimpleNode) children[i]).variable_names.elementAt(j));
              }

              for (int j = 0; j < ((SimpleNode) children[i]).variable_declarations.size(); j++) {
                variable_declarations.add(((SimpleNode) children[i]).variable_declarations.elementAt(j));
              }

              for (int j = 0; j < ((SimpleNode) children[i]).statements.size(); j++) {
                statements.add(((SimpleNode) children[i]).statements.elementAt(j));
              }

              for (int j = 0; j < ((SimpleNode) children[i]).initializing_code.size(); j++) {
                initializing_code.add(((SimpleNode) children[i]).initializing_code.elementAt(j));
              }
            }

            // what children type is it?
						printDDebug	("child of entityConstructor: " +children[i], jc);
						if (children[i] instanceof X_EntityConstructorParameter){

							actual_parameters.addElement(jc.generated_java);
// 							if (jc.print_string.length() == 0) {
// 								actual_parameters.addElement(jc.generated_java);
// 							} else {
// 								actual_parameters.addElement(jc.print_string);
// 							}
							actual_count++;
						}


          }
        }
      }
    }

    if (jc != null) {
      if (jc.active) {
        //								System.out.println("out flag_non_temp: " + jc.flag_non_temporary_value_instance +"  assignment depth: " +jc.assignment_depth);
        String tmp_str = "new jsdai.lang.Value(" + getStaticTypeFieldName(jc) + ")";
				String tmp_argument = "";
        for (int i = 0; i < actual_count; i++) {
					tmp_argument = (String) actual_parameters.elementAt(i);
// System.out.println("XEEX: " + tmp_argument);
					tmp_str += tmp_argument;
        }
// System.out.println("XEEX2: " + tmp_str);

//         for (int i = 0; i < actual_count; i++) {
//           String tmp_argument = (String) actual_parameters.elementAt(i);
//           tmp_str += (".addParameter(" + tmp_argument + ")");
//         }

//         System.out.println("flag_non_temp: " + jc.flag_non_temporary_value_instance + 
//                            "  assignment depth: " + jc.assignment_depth + "indent: " + jc.indent);

        // check, if this entity_constructor is directly in assignment statement
				// moved to Value class:
//         if (jc.flag_non_temporary_value_instance && ((jc.assignment_depth + 1) == jc.indent)) {
//           tmp_str = "(" + tmp_str + ")";
//           tmp_str += ".makeInstance(_context)";
//         }

//         if ((jc.in_derive_definition || jc.in_constant_definition) && (5 == jc.indent)) {
//           tmp_str = "(" + tmp_str + ")";
//           tmp_str += ".makeInstance(_context)";
//         }

        jc.generated_java = tmp_str;
// System.out.println("XEEX3: " + jc.generated_java);
				printDDebug ("### X_EntityConstructor - generated java: " + jc.generated_java, jc);
      } // jc.active
    } // jc != null

    return data;
  }

  String getStaticTypeFieldName(JavaClass jc) throws SdaiException {
    current_model = jc.model;

    String type_name = "";

    String base_entity_name = ((EEntity_definition) ed).getName(null);
    String base_name = getEntityPackage((EEntity_definition) ed) + "C" + 
                       base_entity_name.substring(0, 1).toUpperCase() + 
                       base_entity_name.substring(1).toLowerCase();
    type_name = base_name + ".definition";

    return type_name;
  }

  String getEntityPackage(EEntity_definition eds) throws SdaiException {
    // return an empty string if ed and eds in the same schema, and the package of eds otherwise
    String entity_package = "";
    SdaiModel entity_model = eds.findEntityInstanceSdaiModel();

    if (entity_model != current_model) {
      String entity_schema_name = getSchema_definitionFromModel(entity_model).getName(null);

      if (entity_schema_name.equalsIgnoreCase("Sdai_dictionary_schema")) {
        entity_package = "jsdai.dictionary.";
      } else {
        entity_package = "jsdai.S" + entity_schema_name.substring(0, 1).toUpperCase() + 
                         entity_schema_name.substring(1).toLowerCase() + ".";
      }
    }

    return entity_package;
  }

  ESchema_definition getSchema_definitionFromModel(SdaiModel sm)
                                            throws SdaiException {
    Aggregate ia = sm.getEntityExtentInstances(ESchema_definition.class);
    SdaiIterator iter_inst = ia.createIterator();

    while (iter_inst.next()) {
      ESchema_definition inst = (ESchema_definition) ia.getCurrentMemberObject(iter_inst);

      return inst;
    }

    return null;
  }
}
