Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.lokk3d.essereinfermiere;
- import android.app.DatePickerDialog;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.DatePicker;
- import android.widget.EditText;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- import com.google.gson.Gson;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- import com.google.gson.reflect.TypeToken;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- import org.w3c.dom.Text;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.Map;
- public class RegistratiFragment extends Fragment {
- public View v;
- private OnFragmentInteractionListener mListener;
- public Button registratiBtn;
- public EditText dataT;
- public DatePicker datePicker;
- public Calendar calendar;
- public int year, month, day;
- public DatabaseReference mDb;
- public Utility utility;
- public ArrayList<EditText> campi;
- public ArrayList<String> valori;
- public Spinner sesso;
- public TextView user_will_be;
- public String provincia_str;
- public String codice_str;
- public RegistratiFragment() {
- // Required empty public constructor
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- v = inflater.inflate(R.layout.fragment_registrati, container, false);
- codice_str = "";
- provincia_str = "";
- //((RegistratiFragment) getActivity()).setActionBarTitle(R.string.registrati);
- getActivity().setTitle(R.string.registrati);
- mDb = FirebaseDatabase.getInstance().getReference();
- utility = new Utility(getContext());
- dataT = (EditText) v.findViewById(R.id.data);
- sesso = (Spinner) v.findViewById(R.id.sesso);
- dataT.setOnFocusChangeListener(new View.OnFocusChangeListener() {
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- if (hasFocus) {
- //showDialog(999);
- }
- }
- });
- campi = new ArrayList<EditText>();
- valori = new ArrayList<String>();
- registratiBtn = (Button) v.findViewById(R.id.registra_infermiere);
- registratiBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Log.d("Sesso selezionato",sesso.getSelectedItem().toString());
- if(utility.isInternetAvailable()){
- campi.clear();
- valori.clear();
- campi.add((EditText) v.findViewById(R.id.provincia));
- campi.add((EditText) v.findViewById(R.id.codice));
- campi.add((EditText) v.findViewById(R.id.password));
- campi.add((EditText) v.findViewById(R.id.nome));
- campi.add((EditText) v.findViewById(R.id.cognome));
- campi.add((EditText) v.findViewById(R.id.data));
- campi.add((EditText) v.findViewById(R.id.codice_fiscale));
- campi.add((EditText) v.findViewById(R.id.email));
- campi.add((EditText) v.findViewById(R.id.telefono));
- for(int i = 0; i < campi.size(); i++) {
- valori.add(campi.get(i).getText().toString());
- }
- valori.set(0, valori.get(0).toUpperCase()); //provincia --> uppercase
- valori.set(6, valori.get(6).toUpperCase()); //codice fiscale --> uppercase
- valori.set(2, utility.passwort_to_sha(valori.get(2), utility.SALT));
- valori.set(10, utility.passwort_to_sha(valori.get(10), utility.SALT));
- boolean completo = true;
- for(int i = 0; i < campi.size(); i++){
- Log.d("valore", valori.get(i));
- if(valori.get(i).equals("")){
- campi.get(i).setBackgroundResource(R.drawable.border);
- completo = false;
- } else {
- campi.get(i).setBackgroundResource(0);
- Log.d("fatto","si");
- }
- }
- if(sesso.getSelectedItem().toString().equals("Sesso:")){
- sesso.setBackgroundResource(R.drawable.border);
- completo = false;
- } else {
- sesso.setBackgroundResource(0);
- }
- if(!completo){
- Toast.makeText(getContext(), "Completare tutti i campi, prego", Toast.LENGTH_SHORT).show();
- } else{
- if(controlli(campi, valori)){
- mDb.child(valori.get(0)+valori.get(1)).addListenerForSingleValueEvent(new ValueEventListener(){
- @Override
- public void onDataChange(DataSnapshot snapshot) {
- if (snapshot.exists()) {
- Toast.makeText(getContext(),
- "Attenzione, l'utente è già registrato alla piattaforma",
- Toast.LENGTH_SHORT).show();
- }else {
- try {
- JSONObject jsonString = createJson(valori);
- Log.d("tag", jsonString.toString(4));
- //rendo l'oggetto JSON un oggetto Json, poi rendo quest'ultimo una mappa
- utility.showProgressDialog("Attendere, prego");
- JsonParser jsonParser = new JsonParser();
- JsonObject gsonObject = (JsonObject)jsonParser.parse(jsonString.toString());
- Map<String, Object> myMap = new Gson().fromJson(
- gsonObject, new TypeToken<HashMap<String, Object>>() {}.getType()
- );
- mDb.child(valori.get(0)+valori.get(1)).setValue(myMap);
- utility.dismissProgressDialog();
- Toast.makeText(getContext(), "Utente registrato correttamente!", Toast.LENGTH_SHORT).show();
- startActivity(new Intent(getContext(),AuthenticationActivity.class));
- } catch (JSONException e) {
- e.printStackTrace();
- Toast.makeText(getContext(), "Errore sconosciuto, impossibile terminare la registrazione", Toast.LENGTH_SHORT).show();
- }
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- }
- }
- } else{
- Toast.makeText(getContext(), "Connessione internet non disponibile, prego connettersi e riprovare.", Toast.LENGTH_SHORT).show();
- }
- }
- });
- return v;
- }
- public boolean controlli(ArrayList<EditText> campi, ArrayList<String> valori){
- String [] province = {"TO", "VC", "NO", "CN", "AT", "AL", "BI", "VB", "AO", "VA", "CO",
- "SO", "MI", "BG", "BS", "PV", "CR", "MN", "LC", "LO", "BZ", "TN", "VR", "VI", "BL",
- "TV", "VE", "PD", "RO", "UD", "GO", "TS", "PN", "IM", "SV", "GE", "SP", "PC",
- "PR", "RE", "MO", "BO", "FE", "RA", "FO", "RN", "MS", "LU", "PT", "FI",
- "LI", "PI", "AR", "SI", "GR", "PO", "PG", "TR", "PS", "AN", "MC", "AP",
- "VT", "RI", "RM", "LT", "FR", "AQ", "TE", "PE", "CH", "CB", "IS", "CE",
- "BN", "NA", "AV", "SA", "FG", "BA", "TA", "BR", "LE", "PZ", "MT", "CS",
- "CZ", "RC", "KR", "VV", "TP", "PA", "ME", "AG", "CL", "EN", "CT", "RG",
- "SR", "SS","NU","CA","OR"};
- if(!Arrays.asList(province).contains(valori.get(0))){ //se la provincia inserita nn è nella lista
- Toast.makeText(getContext(), "Attenzione, provincia non valida", Toast.LENGTH_SHORT).show();
- campi.get(0).setBackgroundResource(R.drawable.border);
- return false;
- }else {
- campi.get(0).setBackgroundResource(0);
- if(valori.get(1).length() < 4){ //se il codice ha meno di 4 cifre
- Toast.makeText(getContext(), "Attenzione, il codice deve avere almeno 4 cifre", Toast.LENGTH_SHORT).show();
- campi.get(1).setBackgroundResource(R.drawable.border);
- return false;
- }else {
- campi.get(1).setBackgroundResource(0);
- if(valori.get(6).length() != 16 ){ //se il codice fiscale non ha 16 caratteri
- Toast.makeText(getContext(), "Attenzione, il codice fiscale deve essere di 16 caratteri", Toast.LENGTH_SHORT).show();
- campi.get(6).setBackgroundResource(R.drawable.border);
- return false;
- }else {
- campi.get(6).setBackgroundResource(0);
- return true; //se tutto va bene
- }
- }
- }
- }
- /*
- @Override
- protected Dialog onCreateDialog(int id) {
- if (id == 999) {
- return new DatePickerDialog(getContext(), myDateListener, year, month, day);
- }
- return null;
- }*/
- private DatePickerDialog.OnDateSetListener myDateListener = new
- DatePickerDialog.OnDateSetListener() {
- @Override
- public void onDateSet(DatePicker arg0,
- int arg1, int arg2, int arg3) {
- dataT.setText(String.valueOf(arg3)+"/"+
- String.valueOf(arg2+1)+"/"+
- String.valueOf(arg1));
- }
- };
- public JSONObject createJson(ArrayList<String> valori) throws JSONException {
- JSONObject userJson = new JSONObject();
- JSONObject securityjson = new JSONObject();
- securityjson.put("password",valori.get(2));
- securityjson.put("domanda_segreta",valori.get(9));
- securityjson.put("risposta_segreta",valori.get(10));
- JSONObject personaljson = new JSONObject();
- personaljson.put("nome",valori.get(3));
- personaljson.put("cognome",valori.get(4));
- personaljson.put("data_di_nascita",valori.get(5));
- personaljson.put("codice_fiscale",valori.get(6));
- personaljson.put("email",valori.get(7));
- personaljson.put("telefono",valori.get(8));
- personaljson.put("sesso",sesso.getSelectedItem().toString().substring(0,1));
- JSONObject tariffejson = new JSONObject();
- tariffejson.put("tariffe custom","");
- tariffejson.put("tariffe default", new JSONObject(utility.loadJSONFromAsset("tariffe.json")));
- userJson.put("sicurezza", securityjson);
- userJson.put("tariffe", tariffejson);
- userJson.put("dati_personali", personaljson);
- return userJson;
- }
- public interface OnFragmentInteractionListener {
- // TODO: Update argument type and name
- void onFragmentInteraction(Uri uri);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment