pearos

Untitled

Oct 6th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.80 KB | None | 0 0
  1. package com.lokk3d.essereinfermiere;
  2.  
  3. import android.app.DatePickerDialog;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.support.v4.app.Fragment;
  8. import android.text.Editable;
  9. import android.text.TextWatcher;
  10. import android.util.Log;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.Button;
  15. import android.widget.DatePicker;
  16. import android.widget.EditText;
  17. import android.widget.Spinner;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. import com.google.firebase.database.DataSnapshot;
  22. import com.google.firebase.database.DatabaseError;
  23. import com.google.firebase.database.DatabaseReference;
  24. import com.google.firebase.database.FirebaseDatabase;
  25. import com.google.firebase.database.ValueEventListener;
  26. import com.google.gson.Gson;
  27. import com.google.gson.JsonObject;
  28. import com.google.gson.JsonParser;
  29. import com.google.gson.reflect.TypeToken;
  30.  
  31. import org.json.JSONException;
  32. import org.json.JSONObject;
  33. import org.json.simple.parser.JSONParser;
  34. import org.json.simple.parser.ParseException;
  35. import org.w3c.dom.Text;
  36.  
  37. import java.util.ArrayList;
  38. import java.util.Arrays;
  39. import java.util.Calendar;
  40. import java.util.HashMap;
  41. import java.util.Map;
  42.  
  43. public class RegistratiFragment extends Fragment {
  44.  
  45.     public View v;
  46.     private OnFragmentInteractionListener mListener;
  47.  
  48.     public Button registratiBtn;
  49.     public EditText dataT;
  50.     public DatePicker datePicker;
  51.     public Calendar calendar;
  52.     public int year, month, day;
  53.     public DatabaseReference mDb;
  54.     public Utility  utility;
  55.  
  56.     public ArrayList<EditText> campi;
  57.     public ArrayList<String> valori;
  58.     public Spinner sesso;
  59.  
  60.     public TextView user_will_be;
  61.     public String provincia_str;
  62.     public String codice_str;
  63.  
  64.  
  65.     public RegistratiFragment() {
  66.         // Required empty public constructor
  67.     }
  68.  
  69.  
  70.     @Override
  71.     public void onCreate(Bundle savedInstanceState) {
  72.         super.onCreate(savedInstanceState);
  73.  
  74.     }
  75.  
  76.     @Override
  77.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  78.                              Bundle savedInstanceState) {
  79.         // Inflate the layout for this fragment
  80.         v = inflater.inflate(R.layout.fragment_registrati, container, false);
  81.  
  82.         codice_str = "";
  83.         provincia_str = "";
  84.  
  85.         //((RegistratiFragment) getActivity()).setActionBarTitle(R.string.registrati);
  86.         getActivity().setTitle(R.string.registrati);
  87.  
  88.  
  89.         mDb = FirebaseDatabase.getInstance().getReference();
  90.         utility = new Utility(getContext());
  91.  
  92.  
  93.         dataT = (EditText) v.findViewById(R.id.data);
  94.         sesso = (Spinner) v.findViewById(R.id.sesso);
  95.  
  96.  
  97.         dataT.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  98.             @Override
  99.             public void onFocusChange(View v, boolean hasFocus) {
  100.                 if (hasFocus) {
  101.                     //showDialog(999);
  102.                 }
  103.             }
  104.         });
  105.  
  106.         campi = new ArrayList<EditText>();
  107.         valori = new ArrayList<String>();
  108.  
  109.  
  110.  
  111.         registratiBtn = (Button) v.findViewById(R.id.registra_infermiere);
  112.  
  113.         registratiBtn.setOnClickListener(new View.OnClickListener() {
  114.             @Override
  115.             public void onClick(View view) {
  116.  
  117.                 Log.d("Sesso selezionato",sesso.getSelectedItem().toString());
  118.  
  119.                 if(utility.isInternetAvailable()){
  120.                     campi.clear();
  121.                     valori.clear();
  122.  
  123.                     campi.add((EditText) v.findViewById(R.id.provincia));
  124.                     campi.add((EditText) v.findViewById(R.id.codice));
  125.                     campi.add((EditText) v.findViewById(R.id.password));
  126.  
  127.                     campi.add((EditText) v.findViewById(R.id.nome));
  128.                     campi.add((EditText) v.findViewById(R.id.cognome));
  129.                     campi.add((EditText) v.findViewById(R.id.data));
  130.                     campi.add((EditText) v.findViewById(R.id.codice_fiscale));
  131.                     campi.add((EditText) v.findViewById(R.id.email));
  132.                     campi.add((EditText) v.findViewById(R.id.telefono));
  133.  
  134.  
  135.                     for(int i = 0; i < campi.size(); i++) {
  136.                         valori.add(campi.get(i).getText().toString());
  137.                     }
  138.  
  139.                     valori.set(0, valori.get(0).toUpperCase()); //provincia --> uppercase
  140.                     valori.set(6, valori.get(6).toUpperCase()); //codice fiscale --> uppercase
  141.                     valori.set(2, utility.passwort_to_sha(valori.get(2), utility.SALT));
  142.                     valori.set(10, utility.passwort_to_sha(valori.get(10), utility.SALT));
  143.  
  144.                     boolean completo = true;
  145.                     for(int i = 0; i < campi.size(); i++){
  146.                         Log.d("valore", valori.get(i));
  147.                         if(valori.get(i).equals("")){
  148.                             campi.get(i).setBackgroundResource(R.drawable.border);
  149.                             completo = false;
  150.                         } else {
  151.                             campi.get(i).setBackgroundResource(0);
  152.                             Log.d("fatto","si");
  153.                         }
  154.                     }
  155.  
  156.  
  157.                     if(sesso.getSelectedItem().toString().equals("Sesso:")){
  158.                         sesso.setBackgroundResource(R.drawable.border);
  159.                         completo = false;
  160.                     } else {
  161.                         sesso.setBackgroundResource(0);
  162.                     }
  163.  
  164.                     if(!completo){
  165.                         Toast.makeText(getContext(), "Completare tutti i campi, prego", Toast.LENGTH_SHORT).show();
  166.                     } else{
  167.                         if(controlli(campi, valori)){
  168.                             mDb.child(valori.get(0)+valori.get(1)).addListenerForSingleValueEvent(new ValueEventListener(){
  169.                                 @Override
  170.                                 public void onDataChange(DataSnapshot snapshot) {
  171.                                     if (snapshot.exists()) {
  172.                                         Toast.makeText(getContext(),
  173.                                                 "Attenzione, l'utente è già registrato alla piattaforma",
  174.                                                 Toast.LENGTH_SHORT).show();
  175.                                     }else {
  176.                                         try {
  177.                                             JSONObject jsonString = createJson(valori);
  178.                                             Log.d("tag", jsonString.toString(4));
  179.  
  180.                                             //rendo l'oggetto JSON un oggetto Json, poi rendo quest'ultimo una mappa
  181.                                             utility.showProgressDialog("Attendere, prego");
  182.                                             JsonParser jsonParser = new JsonParser();
  183.                                             JsonObject gsonObject = (JsonObject)jsonParser.parse(jsonString.toString());
  184.                                             Map<String, Object> myMap = new Gson().fromJson(
  185.                                                     gsonObject, new TypeToken<HashMap<String, Object>>() {}.getType()
  186.                                             );
  187.                                             mDb.child(valori.get(0)+valori.get(1)).setValue(myMap);
  188.                                             utility.dismissProgressDialog();
  189.                                             Toast.makeText(getContext(), "Utente registrato correttamente!", Toast.LENGTH_SHORT).show();
  190.                                             startActivity(new Intent(getContext(),AuthenticationActivity.class));
  191.                                         } catch (JSONException e) {
  192.                                             e.printStackTrace();
  193.                                             Toast.makeText(getContext(), "Errore sconosciuto, impossibile terminare la registrazione", Toast.LENGTH_SHORT).show();
  194.                                         }
  195.                                     }
  196.                                 }
  197.                                 @Override
  198.                                 public void onCancelled(DatabaseError databaseError) {
  199.  
  200.                                 }
  201.                             });
  202.                         }
  203.                     }
  204.                 } else{
  205.                     Toast.makeText(getContext(), "Connessione internet non disponibile, prego connettersi e riprovare.", Toast.LENGTH_SHORT).show();
  206.                 }
  207.  
  208.             }
  209.         });
  210.  
  211.         return v;
  212.     }
  213.  
  214.     public boolean controlli(ArrayList<EditText> campi, ArrayList<String> valori){
  215.  
  216.         String [] province = {"TO", "VC", "NO", "CN", "AT", "AL", "BI", "VB", "AO", "VA", "CO",
  217.                 "SO", "MI", "BG", "BS", "PV", "CR", "MN", "LC", "LO", "BZ", "TN", "VR", "VI", "BL",
  218.                 "TV", "VE", "PD", "RO", "UD", "GO", "TS", "PN", "IM",  "SV",  "GE",  "SP",  "PC",
  219.                 "PR",  "RE",  "MO",  "BO",  "FE",  "RA",  "FO",  "RN",  "MS",  "LU",  "PT",  "FI",
  220.                 "LI",  "PI",  "AR",  "SI",  "GR",  "PO",  "PG",  "TR",  "PS",  "AN",  "MC",  "AP",
  221.                 "VT",  "RI",  "RM",  "LT",  "FR",  "AQ",  "TE",  "PE",  "CH",  "CB",  "IS",  "CE",
  222.                 "BN",  "NA",  "AV",  "SA",  "FG",  "BA",  "TA",  "BR",  "LE",  "PZ",  "MT",  "CS",
  223.                 "CZ",  "RC",  "KR",  "VV",  "TP",  "PA",  "ME",  "AG",  "CL",  "EN",  "CT",  "RG",
  224.                 "SR", "SS","NU","CA","OR"};
  225.  
  226.         if(!Arrays.asList(province).contains(valori.get(0))){ //se la provincia inserita nn è nella lista
  227.             Toast.makeText(getContext(), "Attenzione, provincia non valida", Toast.LENGTH_SHORT).show();
  228.             campi.get(0).setBackgroundResource(R.drawable.border);
  229.             return false;
  230.         }else {
  231.             campi.get(0).setBackgroundResource(0);
  232.             if(valori.get(1).length() < 4){  //se il codice ha meno di 4 cifre
  233.                 Toast.makeText(getContext(), "Attenzione, il codice deve avere almeno 4 cifre", Toast.LENGTH_SHORT).show();
  234.                 campi.get(1).setBackgroundResource(R.drawable.border);
  235.                 return false;
  236.             }else {
  237.                 campi.get(1).setBackgroundResource(0);
  238.                 if(valori.get(6).length() != 16 ){ //se il codice fiscale non ha 16 caratteri
  239.                     Toast.makeText(getContext(), "Attenzione, il codice fiscale deve essere di 16 caratteri", Toast.LENGTH_SHORT).show();
  240.                     campi.get(6).setBackgroundResource(R.drawable.border);
  241.                     return false;
  242.                 }else {
  243.                     campi.get(6).setBackgroundResource(0);
  244.                     return true; //se tutto va bene
  245.                 }
  246.             }
  247.         }
  248.     }
  249.  
  250.     /*
  251.     @Override
  252.     protected Dialog onCreateDialog(int id) {
  253.         if (id == 999) {
  254.             return new DatePickerDialog(getContext(), myDateListener, year, month, day);
  255.         }
  256.         return null;
  257.     }*/
  258.  
  259.     private DatePickerDialog.OnDateSetListener myDateListener = new
  260.             DatePickerDialog.OnDateSetListener() {
  261.                 @Override
  262.                 public void onDateSet(DatePicker arg0,
  263.                                       int arg1, int arg2, int arg3) {
  264.                     dataT.setText(String.valueOf(arg3)+"/"+
  265.                             String.valueOf(arg2+1)+"/"+
  266.                             String.valueOf(arg1));
  267.                 }
  268.             };
  269.  
  270.     public JSONObject createJson(ArrayList<String> valori) throws JSONException {
  271.         JSONObject userJson = new JSONObject();
  272.  
  273.         JSONObject securityjson = new JSONObject();
  274.         securityjson.put("password",valori.get(2));
  275.         securityjson.put("domanda_segreta",valori.get(9));
  276.         securityjson.put("risposta_segreta",valori.get(10));
  277.  
  278.         JSONObject personaljson = new JSONObject();
  279.         personaljson.put("nome",valori.get(3));
  280.         personaljson.put("cognome",valori.get(4));
  281.         personaljson.put("data_di_nascita",valori.get(5));
  282.         personaljson.put("codice_fiscale",valori.get(6));
  283.         personaljson.put("email",valori.get(7));
  284.         personaljson.put("telefono",valori.get(8));
  285.         personaljson.put("sesso",sesso.getSelectedItem().toString().substring(0,1));
  286.  
  287.         JSONObject tariffejson = new JSONObject();
  288.         tariffejson.put("tariffe custom","");
  289.         tariffejson.put("tariffe default", new JSONObject(utility.loadJSONFromAsset("tariffe.json")));
  290.  
  291.  
  292.         userJson.put("sicurezza", securityjson);
  293.         userJson.put("tariffe", tariffejson);
  294.         userJson.put("dati_personali", personaljson);
  295.  
  296.         return userJson;
  297.     }
  298.  
  299.     public interface OnFragmentInteractionListener {
  300.         // TODO: Update argument type and name
  301.         void onFragmentInteraction(Uri uri);
  302.     }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment