Example #1
0
def write_application(package, report_class):
    """ Write java class which extends android application. 
    I will handle shared information as the report 
    
    Returns a dictionary with the information to add to Manifest file. 
    """
    # Get the application class file.
    output_directory = get_filepath(ACTIVITIES)
    if (not exists(output_directory)):
        makedirs(output_directory)
    application_template_filename = get_property(TEMPLATES_SECTION,
                                                 APPLICATION_FILE)
    application_filename = join(
        output_directory,
        (Template(application_template_filename).safe_substitute(
            CLASS_NAME=report_class)))

    # Set the Environment for the jinja2 templates and get the template
    environment = set_environment(ACTIVITIES_TEMPLATES_PATH)
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, APPLICATION)
    template = environment.get_template(template_name)

    model_package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL)

    # Open and write the manifest file
    app_file = open(application_filename, 'w')
    app_file.write(
        template.render(package=package,
                        model_package=model_package,
                        report_class=report_class))
    app_file.close()
    return {'name': report_class + '_Application', 'launcher': False}
def write_application(package, report_class):
    """ Write java class which extends android application. 
    I will handle shared information as the report 
    
    Returns a dictionary with the information to add to Manifest file. 
    """
    # Get the application class file. 
    output_directory = get_filepath(ACTIVITIES)
    if (not exists(output_directory)):
        makedirs(output_directory)
    application_template_filename = get_property(TEMPLATES_SECTION,
                                                 APPLICATION_FILE)
    application_filename = join(output_directory,
                                (Template(application_template_filename).
                                 safe_substitute(CLASS_NAME=report_class)))

    # Set the Environment for the jinja2 templates and get the template
    environment = set_environment(ACTIVITIES_TEMPLATES_PATH)
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, APPLICATION)
    template = environment.get_template(template_name)

    model_package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL)

    # Open and write the manifest file
    app_file = open(application_filename, 'w')
    app_file.write(template.render(package=package,
                                   model_package=model_package,
                                   report_class=report_class))
    app_file.close()
    return {'name':report_class+'_Application', 'launcher':False}
Example #3
0
def write_group_class(environment, template_model_file, class_name,
                      attribute_variable, package):
    """ Write group class

    This class is needed to handle Expandablelistview in Android.
    It will have only two attributes:
    - String with its  concept name. It need to be internazionalized
    - ArrayList with children.

    Keyword Arguments:
    template_model_file -- template for model filename. Handles ouptput rpath.
    class_name -- Name of the class to write.

    """
    # Import Arraylist. It's always needed with a group class
    imports = []
    imports.append(IMPORT_ARRAY)

    #Load template to instantiate getters and setters
    getters_setters_template_name = get_property(MODEL_TEMPLATES_SECTION,
                                                 GETTERS_SETTERS)
    getters_setters_template = environment.get_template(
        getters_setters_template_name)

    model_filename = get_model_file(template_model_file,
                                    class_name + GROUP_CLASS)
    if (not isfile(model_filename)):
        model_file = open(model_filename, 'w')
        # Create a list with this class attributes and add a default group name
        attributes = [GROUP_STRING]
        methods = [
            getters_setters_template.render(type="type", variable_type=STRING)
        ]
        # Create custom child class
        template_name = get_property(MODEL_TEMPLATES_SECTION, TYPE_JAVA)
        template = environment.get_template(template_name)
        type_name = (Template(ARRAY).safe_substitute(CLASS=class_name +
                                                     CHILD_CLASS))

        array = template.render(type=(type_name), variable="children")
        attributes.append(array)
        methods.append(
            getters_setters_template.render(type="children",
                                            variable_type=type_name))
        # Load model template and write the class
        template_name = get_property(MODEL_TEMPLATES_SECTION, CLASS_TEMPLATE)
        template = environment.get_template(template_name)
        model_file.write(
            template.render(package=package,
                            class_type=CLASS,
                            imports=imports,
                            class_name=class_name + GROUP_CLASS,
                            attributes=attributes,
                            methods=methods))
        #TODO: Getters & setters
        model_file.close()
    else:
        print "Java class {0} already created".format(model_filename)
Example #4
0
def write_group_class(environment, template_model_file, class_name,
                      attribute_variable, package):
    """ Write group class

    This class is needed to handle Expandablelistview in Android.
    It will have only two attributes:
    - String with its  concept name. It need to be internazionalized
    - ArrayList with children.

    Keyword Arguments:
    template_model_file -- template for model filename. Handles ouptput rpath.
    class_name -- Name of the class to write.

    """
    # Import Arraylist. It's always needed with a group class
    imports = []
    imports.append(IMPORT_ARRAY)

    #Load template to instantiate getters and setters
    getters_setters_template_name = get_property(MODEL_TEMPLATES_SECTION,
                                                 GETTERS_SETTERS)
    getters_setters_template = environment.get_template(
        getters_setters_template_name)

    model_filename = get_model_file(template_model_file,
                                    class_name + GROUP_CLASS)
    if (not isfile(model_filename)):
        model_file = open(model_filename, 'w')
        # Create a list with this class attributes and add a default group name
        attributes = [GROUP_STRING]
        methods = [getters_setters_template.render(type="type",
                                                   variable_type=STRING)]
        # Create custom child class
        template_name = get_property(MODEL_TEMPLATES_SECTION, TYPE_JAVA)
        template = environment.get_template(template_name)
        type_name = (Template(ARRAY).
                     safe_substitute(CLASS=class_name + CHILD_CLASS))

        array = template.render(type=(type_name), variable="children")
        attributes.append(array)
        methods.append(
            getters_setters_template.render(type="children",
                                            variable_type=type_name))
        # Load model template and write the class
        template_name = get_property(MODEL_TEMPLATES_SECTION, CLASS_TEMPLATE)
        template = environment.get_template(template_name)
        model_file.write(template.render(package=package,
                                         class_type=CLASS,
                                         imports=imports,
                                         class_name=class_name + GROUP_CLASS,
                                         attributes=attributes,
                                         methods=methods))
        #TODO: Getters & setters
        model_file.close()
    else:
        print "Java class {0} already created".format(model_filename)
Example #5
0
def get_attributes(environment, attributes, imports):
    # Boolean variable preventing multiple imports.
    import_date = False

    java_attributes = []
    java_getters_setters = []

    #Get model string for every attribute
    for attribute in attributes:
        attribute_name = attribute.concept.\
            get_schema_code().lower().replace('-', '_')

        #Load template writte the attributes
        template_name = get_property(MODEL_TEMPLATES_SECTION, TYPE_JAVA)
        template = environment.get_template(template_name)

        #Load template to instantiate getters and setters
        getters_setters_template_name = get_property(MODEL_TEMPLATES_SECTION,
                                                     GETTERS_SETTERS)
        getters_setters_template = environment.get_template(
            getters_setters_template_name)

        # Bool ATTRIBUTE
        if (attribute.type == NUM and attribute.is_bool()):
            java_attributes.append(template.render(type=BOOL,
                                                   variable=attribute_name))
            java_getters_setters.append(
                getters_setters_template.render(type=attribute_name,
                                                variable_type=BOOL))
        # NUM ATTRIBUTE
        elif (attribute.type == NUM and not attribute.is_bool()):
            java_attributes.append(template.render(type=INT,
                                                   variable=attribute_name))
            java_getters_setters.append(
                getters_setters_template.render(type=attribute_name,
                                                variable_type=INT))
        # TEXT ATTRIBUTE
        elif(attribute.type == TEXT or attribute.type == CODE):
            java_attributes.append(template.render(type=STRING,
                                                   variable=attribute_name))
            java_getters_setters.append(
                getters_setters_template.render(type=attribute_name,
                                                variable_type=STRING))
        # DATE ATTRIBUTE
        elif(attribute.type == DATE):
            java_attributes.append(template.render(type=DATE,
                                                   variable=attribute_name))
            java_getters_setters.append(
                getters_setters_template.render(type=attribute_name,
                                                variable_type=DATE))
            if(not import_date):
                imports.append(IMPORT_DATE)
                import_date = True

    return java_attributes, java_getters_setters
Example #6
0
def get_attributes(environment, attributes, imports):
    # Boolean variable preventing multiple imports.
    import_date = False

    java_attributes = []
    java_getters_setters = []

    #Get model string for every attribute
    for attribute in attributes:
        attribute_name = attribute.concept.\
            get_schema_code().lower().replace('-', '_')

        #Load template writte the attributes
        template_name = get_property(MODEL_TEMPLATES_SECTION, TYPE_JAVA)
        template = environment.get_template(template_name)

        #Load template to instantiate getters and setters
        getters_setters_template_name = get_property(MODEL_TEMPLATES_SECTION,
                                                     GETTERS_SETTERS)
        getters_setters_template = environment.get_template(
            getters_setters_template_name)

        # Bool ATTRIBUTE
        if (attribute.type == NUM and attribute.is_bool()):
            java_attributes.append(
                template.render(type=BOOL, variable=attribute_name))
            java_getters_setters.append(
                getters_setters_template.render(type=attribute_name,
                                                variable_type=BOOL))
        # NUM ATTRIBUTE
        elif (attribute.type == NUM and not attribute.is_bool()):
            java_attributes.append(
                template.render(type=INT, variable=attribute_name))
            java_getters_setters.append(
                getters_setters_template.render(type=attribute_name,
                                                variable_type=INT))
        # TEXT ATTRIBUTE
        elif (attribute.type == TEXT or attribute.type == CODE):
            java_attributes.append(
                template.render(type=STRING, variable=attribute_name))
            java_getters_setters.append(
                getters_setters_template.render(type=attribute_name,
                                                variable_type=STRING))
        # DATE ATTRIBUTE
        elif (attribute.type == DATE):
            java_attributes.append(
                template.render(type=DATE, variable=attribute_name))
            java_getters_setters.append(
                getters_setters_template.render(type=attribute_name,
                                                variable_type=DATE))
            if (not import_date):
                imports.append(IMPORT_DATE)
                import_date = True

    return java_attributes, java_getters_setters
def write_listAdapter(environment, package, c_class, c_code,
                      children_position, imports, cardinality):
    """ Write custom list adapter for expandableListView class

    Keyword arguments:
    environment -- Jinja2 templates environment
    package -- Android package where this adapter should be
    c_class -- child class.
    c_code -- child schema and code.
    children_position -- list with children positons.
    imports -- import list to add any new class.

    """
    # Instantiate LISTVIEW ADAPTER
    # Get listView Adapter Template
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION,
                                 LISTADAPTER)
    template = environment.get_template(template_name)
    
    # Get data model package and include it in imports.
    model_package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL)
    imports.append(Template(IMPORT_CUSTOM).
                   safe_substitute(PACKAGE=model_package,
                                   CLASS=c_class + '_Group'))
    imports.append(Template(IMPORT_CUSTOM).
                   safe_substitute(PACKAGE=model_package,
                                   CLASS=c_class + '_Children'))
    
    # Create custom condition for unique attributes
    condition = Template("groupPosition == $pos")

    not_multiple=" and ".join(map(lambda x: condition.substitute(pos=x), cardinality))

    # Get   custom listView Adapter
    list_adapter = template.render(package_name=package,
                                   container_class=c_class,
                                   string_array=c_code,
                                   imports=imports,
                                   children=children_position,
                                   not_multiple_condition=not_multiple)

    # WRITE CUSTOM LISTVIEW ADAPTER
    template_filename = get_property(TEMPLATES_SECTION,
                                     LISTADAPTER_FILE)
    # Get the output path
    path = get_filepath(ACTIVITIES)

    adapter_filename = join(path,
                            (Template(template_filename).
                             safe_substitute(CLASS_NAME=c_code)))
    # TODO: Check if this file already exists
    adapter_file = open(adapter_filename, 'w')
    adapter_file.write(list_adapter)
    adapter_file.close()
Example #8
0
def write_listAdapter(environment, package, c_class, c_code, children_position,
                      imports, cardinality):
    """ Write custom list adapter for expandableListView class

    Keyword arguments:
    environment -- Jinja2 templates environment
    package -- Android package where this adapter should be
    c_class -- child class.
    c_code -- child schema and code.
    children_position -- list with children positons.
    imports -- import list to add any new class.

    """
    # Instantiate LISTVIEW ADAPTER
    # Get listView Adapter Template
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, LISTADAPTER)
    template = environment.get_template(template_name)

    # Get data model package and include it in imports.
    model_package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL)
    imports.append(
        Template(IMPORT_CUSTOM).safe_substitute(PACKAGE=model_package,
                                                CLASS=c_class + '_Group'))
    imports.append(
        Template(IMPORT_CUSTOM).safe_substitute(PACKAGE=model_package,
                                                CLASS=c_class + '_Children'))

    # Create custom condition for unique attributes
    condition = Template("groupPosition == $pos")

    not_multiple = " and ".join(
        map(lambda x: condition.substitute(pos=x), cardinality))

    # Get   custom listView Adapter
    list_adapter = template.render(package_name=package,
                                   container_class=c_class,
                                   string_array=c_code,
                                   imports=imports,
                                   children=children_position,
                                   not_multiple_condition=not_multiple)

    # WRITE CUSTOM LISTVIEW ADAPTER
    template_filename = get_property(TEMPLATES_SECTION, LISTADAPTER_FILE)
    # Get the output path
    path = get_filepath(ACTIVITIES)

    adapter_filename = join(
        path, (Template(template_filename).safe_substitute(CLASS_NAME=c_code)))
    # TODO: Check if this file already exists
    adapter_file = open(adapter_filename, 'w')
    adapter_file.write(list_adapter)
    adapter_file.close()
Example #9
0
def write_children_interface(environment, template_model_file, class_name,
                             package):
    """ Write interface children class

    This class is needed to handle Expandablelistview in Android.
    It will have only one attribute: a string with its identifier.

    Keyword Arguments:
    template_model_file -- template for model filename. Handles ouptput rpath.
    class_name -- Name of the class to write.
    environment -- Jinga 2 template environment.
    package -- child model package.

    """
    model_filename = get_model_file(template_model_file,
                                    class_name + CHILD_CLASS)
    #print "*", model_filename
    if (not isfile(model_filename)):
        model_file = open(model_filename, 'w')
        # Create a list with this class attributes and add a default get Id
        attributes = [INTERFACE_IDENTIFIER]
        # Load model template and write the class
        template_name = get_property(MODEL_TEMPLATES_SECTION, CLASS_TEMPLATE)
        template = environment.get_template(template_name)
        model_file.write(
            template.render(class_type=INTERFACE,
                            package=package,
                            class_name=class_name + CHILD_CLASS,
                            attributes=attributes))
        model_file.close()
    else:
        print "Java class {0} already created".format(model_filename)
Example #10
0
def write_children_interface(environment, template_model_file,
                             class_name, package):
    """ Write interface children class

    This class is needed to handle Expandablelistview in Android.
    It will have only one attribute: a string with its identifier.

    Keyword Arguments:
    template_model_file -- template for model filename. Handles ouptput rpath.
    class_name -- Name of the class to write.
    environment -- Jinga 2 template environment.
    package -- child model package.

    """
    model_filename = get_model_file(template_model_file,
                                    class_name + CHILD_CLASS)
    #print "*", model_filename
    if (not isfile(model_filename)):
        model_file = open(model_filename, 'w')
        # Create a list with this class attributes and add a default get Id
        attributes = [INTERFACE_IDENTIFIER]
        # Load model template and write the class
        template_name = get_property(MODEL_TEMPLATES_SECTION, CLASS_TEMPLATE)
        template = environment.get_template(template_name)
        model_file.write(template.render(class_type=INTERFACE,
                                         package=package,
                                         class_name=class_name + CHILD_CLASS,
                                         attributes=attributes))
        model_file.close()
    else:
        print "Java class {0} already created".format(model_filename)
Example #11
0
def get_expandable_attributes(environment, c_class, c_code):
    """ Return a string with expandable listView children attributes """
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION,
                                 EXPANDABLE_ATTRIBUTES)
    template = environment.get_template(template_name)
    return template.render(container_class=c_class,
                           string_array_class=c_code,
                           string_array=c_code.lower())
Example #12
0
def get_expandable_methods(environment, c_class, c_code):
    """ Return a string with expandable listView methods """
    # Set_children() : fill expandable listview with its data
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, SET_CHILDREN)
    template = environment.get_template(template_name)

    return template.render(container_class=c_class,
                           string_array=c_code.lower())
Example #13
0
def get_expandable_attributes(environment, c_class, c_code):
    """ Return a string with expandable listView children attributes """
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION,
                                 EXPANDABLE_ATTRIBUTES)
    template = environment.get_template(template_name)
    return template.render(container_class=c_class,
                           string_array_class=c_code,
                           string_array=c_code.lower())
Example #14
0
def get_expandable_methods(environment, c_class, c_code):
    """ Return a string with expandable listView methods """
    # Set_children() : fill expandable listview with its data
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION,
                                 SET_CHILDREN)
    template = environment.get_template(template_name)

    return template.render(container_class=c_class,
                           string_array=c_code.lower())
Example #15
0
def write_manifest(package_name, activities,app_name):
    """ Write the Android Manifest file """

    # Get the AndroidManifest
    output_directory = get_filepath(MANIFEST)
    if (not exists(output_directory)):
            makedirs(output_directory)
    manifest_filename = get_property(ACTIVITIES_TEMPLATES_SECTION, MANIFEST)
    manifest_path = join(output_directory, manifest_filename)

    # Set the Environment for the jinja2 templates and get the template
    environment = set_environment(ACTIVITIES_TEMPLATES_PATH)
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, MANIFEST)
    template = environment.get_template(template_name)

    # Open and write the manifest file
    manifest_file = open(manifest_path, 'w')
    manifest_file.write(template.render(package_name=package_name,
                                        app_name=app_name,
                                        activities=activities))
    manifest_file.close()
Example #16
0
def write_manifest(package_name, activities, app_name):
    """ Write the Android Manifest file """

    # Get the AndroidManifest
    output_directory = get_filepath(MANIFEST)
    if (not exists(output_directory)):
        makedirs(output_directory)
    manifest_filename = get_property(ACTIVITIES_TEMPLATES_SECTION, MANIFEST)
    manifest_path = join(output_directory, manifest_filename)

    # Set the Environment for the jinja2 templates and get the template
    environment = set_environment(ACTIVITIES_TEMPLATES_PATH)
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, MANIFEST)
    template = environment.get_template(template_name)

    # Open and write the manifest file
    manifest_file = open(manifest_path, 'w')
    manifest_file.write(
        template.render(package_name=package_name,
                        app_name=app_name,
                        activities=activities))
    manifest_file.close()
Example #17
0
def get_init_children(environment, children_layout, children_position, a_name,
                      layout_id, etext_list):
    """ Return a string with children initialized """

    # Get template name of the children layout
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, children_layout)
    template = environment.get_template(template_name)

    # Get the child_list variable
    child_list = layout_id.split('_')[-1]

    # Get initialize string for listview child.
    return template.render(string_array=child_list.lower(),
                           string_array_class=child_list.upper(),
                           children=children_position,
                           etext_list=etext_list,
                           activity_name=a_name)
Example #18
0
def get_init_children(environment, children_layout, children_position, a_name,
                      layout_id, etext_list):
    """ Return a string with children initialized """

    # Get template name of the children layout
    template_name = get_property(ACTIVITIES_TEMPLATES_SECTION,
                                 children_layout)
    template = environment.get_template(template_name)

    # Get the child_list variable
    child_list = layout_id.split('_')[-1]
    
    # Get initialize string for listview child.
    return template.render(string_array=child_list.lower(),
                           string_array_class=child_list.upper(),
                           children=children_position,
                           etext_list=etext_list,
                           activity_name=a_name)
Example #19
0
def write_one_column_layout_one_level(layout_filename, container, children,
                                      children_layout):
    """ Write a layout with one columns where there are only children

    Keyword Arguments:
    report_level -- level of the report to write
    xml_filename -- file name where the layout should be written
    dict_level -- DICOM report where information is to write in the xml layout

    """
    #If the concept have already generate a layout don't do it again.
    if (not isfile(layout_filename)):
        #print(" * {0}".format(container))
        layout_file = open(layout_filename, 'w')
        # Set the Environment for the jinja2 templates.
        environment = set_environment(LAYOUT_TEMPLATES_PATH)

        # There are ONLY CHILDREN in this layout
        if (len(children) > 0):
            # Get the template
            template_name = get_property(LAYOUT_TEMPLATES_SECTION,
                                         ONE_COLUMN)
            template = environment.get_template(template_name)
            # Store previous concept id
            previous_item = "code_{0}".format(container.get_code()).lower()
            items, current_item  = get_children(environment,
                                                container.get_concept(),
                                                previous_item,
                                                children_layout)

            # Render layout template with correct values.
            layout_file.write(template.render(level_code=container.get_code().lower(),
                                              content=items)
                              .encode('utf-8'))
            layout_file.close()

        # TODO: There are ONLY ATTRIBUTES in this layout
        if (len(container.attributes) > 0):
            pass
    else:
        print "Layout {0} already created".format(layout_filename)
Example #20
0
def write_two_columns_layout(layout_filename, container, children,
                             children_layout, language_code):
    #print container.concept, children
    #If the concept have already generate a layout don't replicate.
    if (not isfile(layout_filename)):
        layout_file = open(layout_filename, 'w')
        #   print(" * {0}".format(container))
        # Set the Environment for the jinja2 templates.
        environment = set_environment(LAYOUT_TEMPLATES_PATH)

        # Get templates
        # Layout template
        template_name = get_property(LAYOUT_TEMPLATES_SECTION,
                                     TWO_COLUMNS)
        template = environment.get_template(template_name)
        #Atrtibutes template
        attributes_template = environment.get_template(
            get_property(LAYOUT_TEMPLATES_SECTION, ATTRIBUTES))

        # Store previous concept id
        layout_prev_item = "code_{0}".format(container.get_code().lower())
        left_content = ""
        right_content = ""
        # There are ONLY ATTRIBUTES in this layout
        if (len(container.attributes) > 0 and len(children) == 0):

            # Split attributes in two columns
            num_attributes = len(container.attributes)
            left_attributes = container.attributes[0:(num_attributes / 2)]
            right_attributes = container.attributes[(num_attributes / 2):]
            # Get the attributes list
            left_items, previous_item  = get_attributes_list(environment,
                                                             left_attributes,
                                                             layout_prev_item,
                                                             language_code)
            left_content = attributes_template.render(
                previous_item=layout_prev_item,
                items=left_items)
            right_items, previous_item = get_attributes_list(
                environment,
                right_attributes,
                previous_item,
                language_code)

            right_content = attributes_template.render(
                previous_item='right_layout',
                items=right_items)
        # There are ATTRIBUTES and CHILDREN in this layout
        elif((len(container.attributes) > 0) and (len(children) > 0)):
            attributes = container.attributes
            concept = container.get_concept()
            # Get the attributes list
            left_items, previous_item  = get_attributes_list(environment,
                                                             attributes,
                                                             layout_prev_item,
                                                             language_code)
            left_content = attributes_template.render(
                previous_item=layout_prev_item,
                items=left_items)
            right_content, previous_item = get_children(environment,
                                                        concept,
                                                        previous_item,
                                                        children_layout)
        try:
            # Render layout template with correct values.
            layout_file.write(template.render(level_code=container.get_code().lower(),
                                              left_content=left_content,
                                              right_content=right_content)
                              .encode('utf-8'))
            layout_file.close()

        except NameError:
            layout_file.close()
            print "Error generating layout items for layout {0}".format(
                layout_filename)
    else:
        print "Layout {0} already created".format(layout_filename)
Example #21
0
def write_activity_file(environment, ontology_id, package,
                        activities_filenames, activity_filename, activity_name,
                        container, children, position, cardinality, parent,
                        report_class, app_classname):
    """ Write activity file for given container and its children """
    imports = []
    tree_level = container.tree_level

    if parent:
        parent_code = parent.get_code()
        parent_schema = parent.get_schema()
    else:
        parent_code, parent_schema = None, None

    # Don't overwrite activities
    if (not isfile(activity_filename)):
        activity_file = open(activity_filename, 'w')

        # This variables should store attributes for listview children
        init = ""
        attributes = ""
        methods = ""

        # Get the layout id and the activity name. It matches partially
        # with the activity filename.
        layout_id = activity_filename.split('/')[-1].split('.')[0].lower()

        # If you are at third level you will need to know if its parent its
        # an array or not.
        if parent and container.get_level() == 3:
            print parent.get_meaning(), parent.get_attributes()
            print container.get_meaning(), container.get_code()
            if container.is_multilple():
                print container.properties.is_multilple()
            print

        # Get a list of attributes that will be shown as spinners.
        spinners = get_spinners(container.attributes)

        # Get a list of attributes that will be shown as text fields.
        etext_list = get_edit_fields(container.attributes)
        print etext_list
        # Get data model package and include it in imports.
        model_package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL)
        imports.append(
            Template(IMPORT_CUSTOM).safe_substitute(PACKAGE=model_package,
                                                    CLASS=report_class))

        if etext_list:
            imports.append(IMPORT_EDITTEXT)

        # Get children layout if there are childrens in this container.
        if children:
            children_layout = get_children_settings(ontology_id,
                                                    str(tree_level))
            container_key = container.get_schema_code().lower()
            card = cardinality[container_key]
            #TODO: This is done for all children it should be done only
            # when there is a listadapter
            unique = [k for k, v in card.iteritems() if v == 1]
            # If there are children, willb e a listview or
            # an expandable listview to load
            init, attributes, methods = get_children(
                environment, package, activities_filenames, tree_level,
                activity_name, position[container_key], unique, imports,
                container, children_layout, layout_id, parent_schema,
                parent_code)

        # Write Activity Tempalte
        template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, ACTIVITY)
        template = environment.get_template(template_name)

        #Write the template
        activity_file.write(
            template.render(imports=imports,
                            package_name=package,
                            activity_name=activity_name,
                            childview=init,
                            layout_file=layout_id,
                            etext_list=etext_list,
                            spinners=spinners,
                            setChildren=methods,
                            attributes=attributes,
                            app_classname=app_classname,
                            report_classname=report_class))
        activity_file.close()
    else:
        print "Activity {0} already created".format(activity_filename)
Example #22
0
def write_activity_file(environment, ontology_id, package,
                        activities_filenames, activity_filename, activity_name,
                        container, children, position, cardinality,
                        parent, report_class,
                        app_classname):
    """ Write activity file for given container and its children """
    imports = []
    tree_level = container.tree_level
    
    if parent:
        parent_code = parent.get_code()
        parent_schema = parent.get_schema()
    else:
        parent_code, parent_schema = None, None

    # Don't overwrite activities
    if (not isfile(activity_filename)):
        activity_file = open(activity_filename, 'w')

        # This variables should store attributes for listview children
        init = ""
        attributes = ""
        methods = ""

        # Get the layout id and the activity name. It matches partially
        # with the activity filename.
        layout_id = activity_filename.split('/')[-1].split('.')[0].lower()

        # If you are at third level you will need to know if its parent its
        # an array or not. 
        if parent and container.get_level() == 3:
            print parent.get_meaning(), parent.get_attributes()
            print container.get_meaning(),container.get_code()
            if container.is_multilple():
                print container.properties.is_multilple()
            print

        # Get a list of attributes that will be shown as spinners.
        spinners = get_spinners(container.attributes)


        # Get a list of attributes that will be shown as text fields.
        etext_list=get_edit_fields(container.attributes)
        print etext_list
        # Get data model package and include it in imports.
        model_package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL)
        imports.append(Template(IMPORT_CUSTOM).
                       safe_substitute(PACKAGE=model_package,
                                       CLASS=report_class))

        if etext_list:
            imports.append(IMPORT_EDITTEXT)

        # Get children layout if there are childrens in this container.
        if children:
            children_layout = get_children_settings(ontology_id,
                                                    str(tree_level))
            container_key = container.get_schema_code().lower()
            card = cardinality[container_key]
            #TODO: This is done for all children it should be done only 
            # when there is a listadapter 
            unique = [k for k,v in card.iteritems() if v == 1]
            # If there are children, willb e a listview or
            # an expandable listview to load
            init, attributes, methods = get_children(environment,
                                                     package,
                                                     activities_filenames,
                                                     tree_level, activity_name,
                                                     position[container_key],
                                                     unique,
                                                     imports,
                                                     container,
                                                     children_layout,
                                                     layout_id,
                                                     parent_schema,
                                                     parent_code)

        # Write Activity Tempalte
        template_name = get_property(ACTIVITIES_TEMPLATES_SECTION,
                                     ACTIVITY)
        template = environment.get_template(template_name)

        
        #Write the template
        activity_file.write(template.render(imports=imports,
                                            package_name=package,
                                            activity_name=activity_name,
                                            childview=init,
                                            layout_file=layout_id,
                                            etext_list=etext_list,
                                            spinners=spinners,
                                            setChildren=methods,
                                            attributes=attributes,
                                            app_classname=app_classname,
                                            report_classname=report_class))
        activity_file.close()
    else:
        print "Activity {0} already created".format(activity_filename)
Example #23
0
def write_model(java_filenames, report, language_code):
    template_model_file = get_template_model_file()

    # Set the Environment for the jinja2 templates and get the template
    environment = set_environment(MODEL_TEMPLATES_PATH)
    package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL)
    # Store an ids list of expandable.
    expandables = []
    flat_tree = {}

    written = []
    #Write model for every container.
    for container, children in report.report.depthFirstChildren():
        imports = []

        #Get parent code, parent schema and grandparent class
        parent_code, parent_schema, gparent_class = get_parent_class(
            flat_tree, container)

        # Build this container java filename using its parent codes and its own
        class_name = get_class_name(container.get_schema(),
                                    container.get_code(), parent_schema,
                                    parent_code).replace('-', '_')

        # Build a parent/children codes hash table.
        # We will use it for parent_code and schema.
        if (children):
            add_tree_hierarchy(flat_tree, container, children, class_name)

        model_filename = get_model_file(template_model_file, class_name)

        # Write model
        if (not isfile(model_filename)):
            model_file = open(model_filename, 'w')
            # Get the attributes.
            attributes, methods = get_attributes(environment,
                                                 container.attributes, imports)
            # Get children attributes and its methods
            parent_class = (container.get_schema().lower().capitalize() + '_' +
                            container.get_code().lower())

            c_attributes, c_methods = get_children(environment, children,
                                                   imports, parent_class,
                                                   class_name, expandables,
                                                   template_model_file,
                                                   package)

            attributes.extend(c_attributes)
            methods.extend(c_methods)
            #Write the model
            # If this class will be expandable in activity.
            # Child class should extend its parent interface.
            template_name = get_property(MODEL_TEMPLATES_SECTION,
                                         CLASS_TEMPLATE)
            template = environment.get_template(template_name)
            # If this class has expandables it has to implement
            # the children interface class
            has_multiple_children = container.properties.max_cardinality == -1
            if (class_name in expandables and has_multiple_children):
                implement_class = Template(IMPLEMENTS).safe_substitute(
                    PARENT_CLASS=gparent_class + CHILD_CLASS)
                model_file.write(
                    template.render(package=package,
                                    class_type=CLASS,
                                    class_name=class_name,
                                    attributes=attributes,
                                    imports=imports,
                                    implements_class=implement_class,
                                    methods=methods))
            else:
                model_file.write(
                    template.render(package=package,
                                    class_type=CLASS,
                                    class_name=class_name,
                                    attributes=attributes,
                                    imports=imports,
                                    methods=methods))
            model_file.close()
        else:
            print "Java class {0} already created".format(model_filename)
Example #24
0
def write_activities(activities_filenames, report):
    flat = report.get_flat_data()

    #Get the ontology id of the report
    ontology_id = report.get_ontology()

    # Set the Environment for the jinja2 templates and get the template
    environment = set_environment(ACTIVITIES_TEMPLATES_PATH)

    # Get report root container. This will be the launcher activity.
    report_root = report.get_root().get_schema_code()

    # Variables to store data
    activities = []
    launcher_activity = ""

    package = get_property(ANDROID_PACKAGES, BASE_MODEL)

    # Get containers position. It must match strings-array position.
    position = {}
    cardinality = {}
    report.get_data_from_report(CHILDREN_ARRAYS,
                                position=position,
                                cardinality=cardinality)
    report_class = report_root.lower().capitalize()
    app_classname = report_class + '_Application'
    old_container = None

    aux_dict = {}
    #Write layout for every file
    for container, children in report.report.depthFirstChildren():
        actual_level = container.get_level()
        #for container, children in flat.iteritems():
        parent_container = None
        if len(children):
            aux_dict[container.get_level()] = container

        activity = {}
        # Get this container's parent
        parent = None
        if actual_level is not 1:
            parent = aux_dict[actual_level - 1]
        if parent:
            parent_code = parent.get_code()
            parent_schema = parent.get_schema()
        else:
            parent_code, parent_schema = None, None

        activity_filename = get_activity_filename(activities_filenames, flat,
                                                  container, parent_code,
                                                  parent_schema)
        activity_name = get_activity_name(activity_filename)

        # Store info to write the Android Manifest
        # Check if this activity is the launcher
        # TODO : (on liner) activity['launcher'] = (container.get_schema_code() == report_root)
        if (container.get_schema_code() == report_root):
            activity['launcher'] = True
        else:
            activity['launcher'] = False
        activity['name'] = activity_name
        activities.append(activity)
        # Log purpose info
        #print "[Level {0}] {1}".format(container.tree_level, children_layout)
        #print activity_filename, package, a_name

        write_activity_file(environment, ontology_id, package,
                            activities_filenames, activity_filename,
                            activity_name, container, children, position,
                            cardinality, parent, report_class, app_classname)
        old_container = container
        #print type(old_container)

    activity = write_application(package, report_class)
    activities.append(activity)
    write_manifest(package, activities, '.' + app_classname)
Example #25
0
def get_children(environment, children, imports, parent_class,
                 parent_class_name, expandables, template_model_file, package):
    # Boolean variable preventing multiple imports.
    import_array = False
    #Boolean variable set true if we need to handle Expandable Children
    # If at least one of the children has multiple items all of them
    # will be inside an expandablelist.
    has_expandable = False
    # Find out if you need an ExpandableList
    for child in children:
        if (child.value.properties.max_cardinality == -1):
            has_expandable = True
            break

    attribute_variable = parent_class_name.lower()
    if (has_expandable):
        write_group_class(environment, template_model_file, parent_class_name,
                          attribute_variable, package)
        write_children_interface(environment, template_model_file,
                                 parent_class_name, package)

    #Load template to instantiate getters and setters
    getters_setters_template_name = get_property(MODEL_TEMPLATES_SECTION,
                                                 GETTERS_SETTERS)
    getters_setters_template = environment.get_template(
        getters_setters_template_name)

    attributes = []
    methods = []

    #print
    #print "********** Get children ****************"
    for child in children:
        # Create the class name
        attribute_variable = child.value.concept.get_schema_code().lower()
        child_class_name = parent_class + '_' + attribute_variable
        child_class_name = child_class_name.replace('-', '_')

        # This child has multiple items. Write a Group class.
        if (has_expandable):
            expandables.append(child_class_name)

        # Load template for java types
        template_name = get_property(MODEL_TEMPLATES_SECTION, TYPE_JAVA)
        template = environment.get_template(template_name)

        render_template = ""
        #Children with multiple items. Array
        if (child.value.properties.max_cardinality == -1):
            type_name = Template(ARRAY).safe_substitute(CLASS=child_class_name)
            render_template = template.render(type=type_name,
                                              variable=attribute_variable)
            # Default getters and setters
            methods.append(
                getters_setters_template.render(type=attribute_variable,
                                                variable_type=type_name))
            # Append method.
            append_method_template_name = get_property(MODEL_TEMPLATES_SECTION,
                                                       ADD_ARRAY)
            append_method_template = environment.get_template(
                append_method_template_name)
            #print type_name, attribute_variable
            append_render_template = append_method_template.render(
                type=child_class_name, variable=attribute_variable)
            methods.append(append_render_template)
            #print render_template
            #print
            if (not import_array):
                imports.append(IMPORT_ARRAY)
                import_array = True

        else:
            render_template = template.render(type=child_class_name,
                                              variable=attribute_variable)
            methods.append(
                getters_setters_template.render(
                    type=attribute_variable, variable_type=child_class_name))

        attributes.append(render_template)

    return attributes, methods
Example #26
0
def write_activities(activities_filenames, report):
    flat = report.get_flat_data() 

    #Get the ontology id of the report
    ontology_id = report.get_ontology()

    # Set the Environment for the jinja2 templates and get the template
    environment = set_environment(ACTIVITIES_TEMPLATES_PATH)

    # Get report root container. This will be the launcher activity.
    report_root = report.get_root().get_schema_code()

    # Variables to store data
    activities = []
    launcher_activity = ""

    package = get_property(ANDROID_PACKAGES, BASE_MODEL)

    # Get containers position. It must match strings-array position.
    position = {}
    cardinality = {}
    report.get_data_from_report(CHILDREN_ARRAYS, position=position, 
                                cardinality=cardinality)
    report_class = report_root.lower().capitalize()
    app_classname = report_class+'_Application'
    old_container = None

    aux_dict = {}
    #Write layout for every file
    for container, children in report.report.depthFirstChildren():
        actual_level = container.get_level()
    #for container, children in flat.iteritems():
        parent_container = None
        if len(children):
            aux_dict[container.get_level()] = container

        activity = {}
        # Get this container's parent
        parent = None
        if actual_level is not 1:
            parent = aux_dict[actual_level - 1]
        if parent:
            parent_code = parent.get_code()
            parent_schema = parent.get_schema()
        else:
            parent_code, parent_schema = None, None

        activity_filename = get_activity_filename(activities_filenames, flat,
                                                  container, parent_code,
                                                  parent_schema)
        activity_name = get_activity_name(activity_filename)
        
        # Store info to write the Android Manifest
        # Check if this activity is the launcher
        # TODO : (on liner) activity['launcher'] = (container.get_schema_code() == report_root) 
        if (container.get_schema_code() == report_root):
            activity['launcher'] = True
        else:
            activity['launcher'] = False
        activity['name'] = activity_name
        activities.append(activity)
        # Log purpose info
        #print "[Level {0}] {1}".format(container.tree_level, children_layout)
        #print activity_filename, package, a_name

        write_activity_file(environment, ontology_id, package,
                            activities_filenames, activity_filename,
                            activity_name, container, children, position,
                            cardinality, parent,
                            report_class, app_classname)
        old_container = container
        #print type(old_container)

    activity = write_application(package, report_class)
    activities.append(activity)
    write_manifest(package, activities, '.'+app_classname)
Example #27
0
def write_model(java_filenames, report, language_code):
    template_model_file = get_template_model_file()

    # Set the Environment for the jinja2 templates and get the template
    environment = set_environment(MODEL_TEMPLATES_PATH)
    package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL)
    # Store an ids list of expandable.
    expandables = []
    flat_tree = {}

    written = []
    #Write model for every container.
    for container, children in report.report.depthFirstChildren():
        imports = []

        #Get parent code, parent schema and grandparent class
        parent_code, parent_schema, gparent_class = get_parent_class(flat_tree,
                                                                     container)

        # Build this container java filename using its parent codes and its own
        class_name = get_class_name(container.get_schema(),
                                    container.get_code(),
                                    parent_schema,
                                    parent_code).replace('-', '_')

        # Build a parent/children codes hash table.
        # We will use it for parent_code and schema.
        if (children):
            add_tree_hierarchy(flat_tree, container, children, class_name)

        model_filename = get_model_file(template_model_file, class_name)

        # Write model
        if (not isfile(model_filename)):
            model_file = open(model_filename, 'w')
            # Get the attributes.
            attributes, methods = get_attributes(environment,
                                                 container.attributes,
                                                 imports)
            # Get children attributes and its methods
            parent_class = (container.get_schema().lower().capitalize() +
                            '_' + container.get_code().lower())

            c_attributes, c_methods = get_children(environment,
                                                   children,
                                                   imports,
                                                   parent_class,
                                                   class_name,
                                                   expandables,
                                                   template_model_file,
                                                   package)

            attributes.extend(c_attributes)
            methods.extend(c_methods)
            #Write the model
            # If this class will be expandable in activity.
            # Child class should extend its parent interface.
            template_name = get_property(MODEL_TEMPLATES_SECTION,
                                         CLASS_TEMPLATE)
            template = environment.get_template(template_name)
            # If this class has expandables it has to implement
            # the children interface class
            has_multiple_children = container.properties.max_cardinality == -1
            if (class_name in expandables and has_multiple_children):
                implement_class = Template(IMPLEMENTS).safe_substitute(
                    PARENT_CLASS=gparent_class + CHILD_CLASS)
                model_file.write(
                    template.render(package=package,
                                    class_type=CLASS,
                                    class_name=class_name,
                                    attributes=attributes,
                                    imports=imports,
                                    implements_class=implement_class,
                                    methods=methods))
            else:
                model_file.write(
                    template.render(package=package,
                                    class_type=CLASS,
                                    class_name=class_name,
                                    attributes=attributes,
                                    imports=imports,
                                    methods=methods))
            model_file.close()
        else:
            print "Java class {0} already created".format(model_filename)
Example #28
0
def get_children(environment, children, imports, parent_class,
                 parent_class_name, expandables, template_model_file,
                 package):
    # Boolean variable preventing multiple imports.
    import_array = False
    #Boolean variable set true if we need to handle Expandable Children
    # If at least one of the children has multiple items all of them
    # will be inside an expandablelist.
    has_expandable = False
    # Find out if you need an ExpandableList
    for child in children:
        if (child.value.properties.max_cardinality == -1 ):
            has_expandable = True
            break

    attribute_variable = parent_class_name.lower()
    if (has_expandable):
        write_group_class(environment, template_model_file, parent_class_name,
                          attribute_variable, package)
        write_children_interface(environment, template_model_file,
                                 parent_class_name, package)

    #Load template to instantiate getters and setters
    getters_setters_template_name = get_property(MODEL_TEMPLATES_SECTION,
                                                 GETTERS_SETTERS)
    getters_setters_template = environment.get_template(
        getters_setters_template_name)

    attributes = []
    methods = []

    #print
    #print "********** Get children ****************"
    for child in children:
        # Create the class name
        attribute_variable = child.value.concept.get_schema_code().lower()
        child_class_name = parent_class + '_' + attribute_variable
        child_class_name = child_class_name.replace('-', '_')

        # This child has multiple items. Write a Group class.
        if(has_expandable):
            expandables.append(child_class_name)

        # Load template for java types
        template_name = get_property(MODEL_TEMPLATES_SECTION, TYPE_JAVA)
        template = environment.get_template(template_name)

        render_template = ""
        #Children with multiple items. Array 
        if (child.value.properties.max_cardinality == -1 ):
            type_name = Template(ARRAY).safe_substitute(CLASS=child_class_name)
            render_template = template.render(type=type_name,
                                              variable=attribute_variable)
            # Default getters and setters 
            methods.append(getters_setters_template.render(
                type=attribute_variable, variable_type=type_name))
            # Append method. 
            append_method_template_name = get_property(MODEL_TEMPLATES_SECTION,
                                                       ADD_ARRAY)
            append_method_template = environment.get_template(
                append_method_template_name)
            #print type_name, attribute_variable
            append_render_template = append_method_template.render(
                type=child_class_name,
                variable=attribute_variable)
            methods.append(append_render_template)
            #print render_template
            #print
            if(not import_array):
                imports.append(IMPORT_ARRAY)
                import_array = True

        else:
            render_template = template.render(type=child_class_name,
                                              variable=attribute_variable)
            methods.append(getters_setters_template.render(
                type=attribute_variable, variable_type=child_class_name))

        attributes.append(render_template)

    return attributes, methods
Example #29
0
def get_template_substitution(environment, template_type, concept=None,
                              report_level=None, previous_item=None,
                              language=None, first=None):
    """ Return a temp snippet that does requiere subtitution
    and the current item to nest template levels.

    """
    #print template_type
    template_name = get_property(LAYOUT_TEMPLATES_SECTION, template_type)
    template = environment.get_template(template_name)
    render_template = ""
    concept_schema = concept.schema.lower().replace('-','_')
    # Strore in a variable the current item id. We will need this when
    # we write self dependent layout items.
    current_item = ""
    # TITLE of a tree section
    if (template_type == TREE_TITLE):
        render_template = template.render(level=concept.value.lower())
        # TODO: find a better way to do this. Dependent on the template
        # using a regex to find "#+id/    \n", and not hardcoded here.
        current_item = "level_{0}_label".format(concept.value.lower())
    # TITLE of a generic section
    elif (template_type == GENERIC_TITLE):
        render_template = template.render(level=report_level.lower())
        current_item = "level_{0}_label".format(report_level.lower())
     # Listview for next (deeper) level of the dicom tree.
    elif (template_type == NEXT_LEVEL):
        render_template = template.render(code=concept.value.lower(),
                                          parent_code=concept.value.lower())
        current_item = "children_{0}_list".format(concept.value)
    # NUM type attribute & TEXT type attribute
    elif (template_type == NUM or template_type == TEXT):
        localized_concept = concept.meaning[language]
        render_template = template.render(concept_name=localized_concept,
                                          concept_schema = concept_schema,
                                          concept_value=concept.value.lower(),
                                          previous_item=previous_item,
                                          first_attribute=first)
        current_item = "etext_{0}".format(concept_schema.lower()
                                          +'_'+concept.value.lower())
    # BOOL type attribute
    elif (template_type == BOOL):
        localized_concept = concept.meaning[language]
        render_template = template.render(concept_name=localized_concept,
                                          concept_value=concept.value.lower(), 
                                          previous_item=previous_item,
                                          first_attribute=first)
        current_item = "cbox_{0}".format(concept.value.lower())
    # DATE type attribute
    elif (template_type == DATE):
        localized_concept = concept.meaning[language]
        render_template = template.render(concept_name=localized_concept,
                                          concept_value=concept.value.lower(),
                                          previous_item=previous_item,
                                          first_attribute=first)
        current_item = "etext_{0}".format(concept.value.lower())
    # CODE type attribute
    elif (template_type == CODE):
        localized_concept = concept.meaning[language]
        render_template = template.render(concept_name=localized_concept,
                                          concept_value=concept.value.lower(),
                                          previous_item=previous_item,
                                          first_attribute=first)
        current_item = "spinner_{0}".format(concept.value.lower())
    # SCROLL
    elif (template_type == SCROLL):
        render_template = template.render(parent=previous_item)
    # LISTVIEW & EXPANDABLELISVIEW
    elif (template_type == LISTVIEW or template_type == EXPANDABLELISVIEW):
        render_template = template.render(concept_value=concept.value.lower(),
                                          previous_item=previous_item)
        current_item = "list_{0}".format(concept.value)

    return render_template, current_item