<commit_before>package ${localnamespace};

import ${namespace}.R;

import android.os.Bundle;
import android.os.AsyncTask;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.app.ProgressDialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.widget.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;

import ${namespace}.data.${name}Adapter;
import ${namespace}.entity.${name};
<#list relations as relation>
	<#if (relation.relation_type=="@OneToOne" | relation.relation_type=="@ManyToOne")>
import ${namespace}.data.${relation.type}Adapter;
import ${namespace}.entity.${relation.type};
	</#if>
</#list>

/** ${name} create fragment
 * 
 * @see android.app.Fragment
 */
public class ${name}CreateFragment extends Fragment implements OnClickListener {
	/* Model data */
	protected ${name} model = new ${name}();

	/* Fields View */
	<#list fields as field>
	protected ${field.customEditType} ${field.name}View;
	</#list>
	<#list relations as relation>
		<#if relation.relation_type=="@ManyToOne">
	protected ${relation.customEditType} ${relation.name}View;
	protected List<${relation.type}> ${relation.type}list;
		</#if>
	</#list>
	
	protected Button saveButton;

	/** Initialize view of fields 
	 * 
	 * @param view The layout inflating
	 */
	protected void initializeComponent(View view) {
		<#foreach field in fields>
		this.${field.name}View = (${field.customEditType}) view.findViewById(R.id.${name?lower_case}_${field.name?lower_case}); 
		</#foreach>
		<#list relations as relation>
			<#if (relation.relation_type=="@OneToOne" | relation.relation_type=="@ManyToOne")>
		this.${relation.name}View = (${relation.customEditType}) view.findViewById(R.id.${name?lower_case}_${relation.name?lower_case}_spinner);
			</#if>
		</#list>
		this.saveButton = (Button) view.findViewById(R.id.${name?lower_case}_btn_save);
		this.saveButton.setOnClickListener(this);
	}

	/** Load data from model to fields view */
	public void loadData() {
		<#foreach field in fields>
			<#if (field.customEditType == "EditText") >
				<#if (field.type == "String")>
		this.${field.name}View.setText(this.model.get${field.name?cap_first}()); 
				</#if>
				<#if (field.type == "Date")>
		this.${field.name}View.setText(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(this.model.get${field.name?cap_first}()) ); 
				</#if>
				<#if (field.type == "int")>
		this.${field.name}View.setText(String.valueOf(this.model.get${field.name?cap_first}()));
				</#if>
			</#if>
			<#if (field.customEditType == "CheckBox") >
		this.${field.name}View.setSelected(this.model.${field.name?uncap_first}()); 
			</#if>
		</#foreach>
		<#foreach relation in relations>
			<#if (relation.relation_type=="@OneToOne" | relation.relation_type=="@ManyToOne")>
		${relation.type}Adapter ${relation.type?lower_case}adapter = new ${relation.type}Adapter(getActivity());
		${relation.type?lower_case}adapter.open();
		this.${relation.type}list = ${relation.type?lower_case}adapter.getAll();
		List<String> ${relation.type?lower_case}strings = new ArrayList<String>();
		for(${relation.type} item : this.${relation.type}list) {
			${relation.type?lower_case}strings.add( String.valueOf(item.getId()) );
		}
		ArrayAdapter<String> ${relation.type?lower_case}DataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, ${relation.type?lower_case}strings);
		this.${relation.name}View.setAdapter(${relation.type?lower_case}DataAdapter);
			</#if>
		</#foreach>
	}
	
	/** Save data from fields view to model */
	public void saveData() {
		<#foreach field in fields>
			<#if (field.customEditType == "EditText") >
				<#if (field.type == "String")>
		this.model.set${field.name?cap_first}(this.${field.name}View.getEditableText().toString());
				</#if>
				<#if (field.type == "Date")>
		try {
			this.model.set${field.name?cap_first}(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(this.${field.name}View.getEditableText().toString()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				</#if>
				<#if (field.type == "int")>
		this.model.set${field.name?cap_first}(Integer.parseInt(this.${field.name}View.getEditableText().toString()));
				</#if>
			</#if>
			<#if (field.customEditType == "CheckBox") >
		this.model.${field.name?uncap_first}(this.${field.name}View.isChecked());
			</#if>
		</#foreach>

		<#foreach relation in relations>
			<#if (relation.relation_type=="@OneToOne" | relation.relation_type=="@ManyToOne")>
		${relation.type}Adapter ${relation.type?lower_case}adapter = new ${relation.type}Adapter(getActivity());
		${relation.type?lower_case}adapter.open();
		${relation.type} ${relation.type?lower_case}item = ${relation.type?lower_case}adapter.getByID( this.${relation.type}list.get((int) this.${relation.name}View.getSelectedItemId()).getId() );
		this.model.set${relation.name?cap_first}(${relation.type?lower_case}item);
		
			</#if>
		</#foreach>
	}

	/** Check data is valid
	 * 
	 * @return true if valid
	 */
	public boolean validateData() {
		return true;
	}

	/** Sets up the UI.
	 * 
	 * @see android.support.v4.app.Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    	
		// Inflate the layout for this fragment
		View view = inflater.inflate(R.layout.fragment_${name?lower_case}_create, container, false);

		this.initializeComponent(view);
		this.loadData();
		return view;
	}

	/** 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		if (this.validateData()) {
			this.saveData();
			new CreateTask(this, this.model).execute();
		}
	}

	public static class CreateTask extends AsyncTask<Void, Void, Integer> {
		protected final Context context;
		protected final ${name}CreateFragment fragment;
		protected final ${name} entity;
		protected String errorMsg;
		protected ProgressDialog progress;

		public CreateTask(${name}CreateFragment fragment, ${name} entity) {
			this.fragment = fragment;
			this.context = fragment.getActivity();
			this.entity = entity;
		}

		/* (non-Javadoc)
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override<commit_msg>Removed loadData method in CreateFragment template<commit_after>package ${localnamespace};

import ${namespace}.R;

import android.os.Bundle;
import android.os.AsyncTask;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.app.ProgressDialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.widget.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;

import ${namespace}.data.${name}Adapter;
import ${namespace}.entity.${name};
<#list relations as relation>
	<#if (relation.relation_type=="@OneToOne" | relation.relation_type=="@ManyToOne")>
import ${namespace}.data.${relation.type}Adapter;
import ${namespace}.entity.${relation.type};
	</#if>
</#list>

/** ${name} create fragment
 * 
 * @see android.app.Fragment
 */
public class ${name}CreateFragment extends Fragment implements OnClickListener {
	/* Model data */
	protected ${name} model = new ${name}();

	/* Fields View */
	<#list fields as field>
	protected ${field.customEditType} ${field.name}View;
	</#list>
	<#list relations as relation>
		<#if relation.relation_type=="@ManyToOne">
	protected ${relation.customEditType} ${relation.name}View;
	protected List<${relation.type}> ${relation.type}list;
		</#if>
	</#list>
	
	protected Button saveButton;

	/** Initialize view of fields 
	 * 
	 * @param view The layout inflating
	 */
	protected void initializeComponent(View view) {
		<#foreach field in fields>
		this.${field.name}View = (${field.customEditType}) view.findViewById(R.id.${name?lower_case}_${field.name?lower_case}); 
		</#foreach>
		<#list relations as relation>
			<#if (relation.relation_type=="@OneToOne" | relation.relation_type=="@ManyToOne")>
		this.${relation.name}View = (${relation.customEditType}) view.findViewById(R.id.${name?lower_case}_${relation.name?lower_case}_spinner);
			</#if>
		</#list>
		this.saveButton = (Button) view.findViewById(R.id.${name?lower_case}_btn_save);
		this.saveButton.setOnClickListener(this);
	}

	
	/** Save data from fields view to model */
	public void saveData() {
		<#foreach field in fields>
			<#if (field.customEditType == "EditText") >
				<#if (field.type == "String")>
		this.model.set${field.name?cap_first}(this.${field.name}View.getEditableText().toString());
				</#if>
				<#if (field.type == "Date")>
		try {
			this.model.set${field.name?cap_first}(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(this.${field.name}View.getEditableText().toString()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				</#if>
				<#if (field.type == "int")>
		this.model.set${field.name?cap_first}(Integer.parseInt(this.${field.name}View.getEditableText().toString()));
				</#if>
			</#if>
			<#if (field.customEditType == "CheckBox") >
		this.model.${field.name?uncap_first}(this.${field.name}View.isChecked());
			</#if>
		</#foreach>

		<#foreach relation in relations>
			<#if (relation.relation_type=="@OneToOne" | relation.relation_type=="@ManyToOne")>
		${relation.type}Adapter ${relation.type?lower_case}adapter = new ${relation.type}Adapter(getActivity());
		${relation.type?lower_case}adapter.open();
		${relation.type} ${relation.type?lower_case}item = ${relation.type?lower_case}adapter.getByID( this.${relation.type}list.get((int) this.${relation.name}View.getSelectedItemId()).getId() );
		this.model.set${relation.name?cap_first}(${relation.type?lower_case}item);
		
			</#if>
		</#foreach>
	}

	/** Check data is valid
	 * 
	 * @return true if valid
	 */
	public boolean validateData() {
		return true;
	}

	/** Sets up the UI.
	 * 
	 * @see android.support.v4.app.Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    	
		// Inflate the layout for this fragment
		View view = inflater.inflate(R.layout.fragment_${name?lower_case}_create, container, false);

		this.initializeComponent(view);
		return view;
	}

	/** 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		if (this.validateData()) {
			this.saveData();
			new CreateTask(this, this.model).execute();
		}
	}

	public static class CreateTask extends AsyncTask<Void, Void, Integer> {
		protected final Context context;
		protected final ${name}CreateFragment fragment;
		protected final ${name} entity;
		protected String errorMsg;
		protected ProgressDialog progress;

		public CreateTask(${name}CreateFragment fragment, ${name} entity) {
			this.fragment = fragment;
			this.context = fragment.getActivity();
			this.entity = entity;
		}

		/* (non-Javadoc)
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
{'fraction_non_alphanumeric': 0.16699310227369496, 'fraction_numerical': 0.0005109426892616878, 'mean_word_length': 5.038046272493573, 'pattern_counts': {'":': 0, '<': 91, '<?xml version=': 0, '>': 91, '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': '2487977', 'n_tokens_mistral': 4114, 'n_tokens_neox': 3764, 'n_words': 701}