Search in sources :

Example 1 with ReadCsv

use of com.example.alphatour.ReadCsv in project AlphaTour by Frank99DG.

the class FileReceiverActivity method readCsv.

private void readCsv(Intent data) {
    if (data == null) {
        return;
    } else {
        Uri uri = data.getParcelableExtra(Intent.EXTRA_STREAM);
        Toast.makeText(FileReceiverActivity.this, uri.getPath(), Toast.LENGTH_SHORT).show();
        String path = uri.getPath();
        String filename = URLUtil.guessFileName(path, null, null);
        String downloadPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
        path = path.substring(path.lastIndexOf('/') + 1);
        File file = new File(downloadPath, filename);
        try {
            BufferedReader read = new BufferedReader(new FileReader(file));
            String line = "";
            Boolean isEmpty = true;
            while ((line = read.readLine()) != null) {
                isEmpty = false;
                String[] token = line.split(",");
                ReadCsv readCsv = new ReadCsv();
                boolean flag = false;
                for (int i = 0; i < token.length; i++) {
                    if (token[i] == null) {
                        flag = true;
                    }
                }
                if (!flag) {
                    if (!token[0].matches("namePlace")) {
                        Element elm = new Element();
                        Place place = new Place();
                        Zone zone = new Zone();
                        Constraint constraint = new Constraint();
                        if (listLineCsv.size() > 0) {
                            setDataToken(token, readCsv);
                            if (!token[0].isEmpty()) {
                                for (int i = 0; i < listPlace.size(); i++) {
                                    if (!listPlace.get(i).getName().matches(token[0]) && !listPlace.get(i).getCity().matches(token[1]) && !listPlace.get(i).getTypology().matches(token[2])) {
                                        idP++;
                                        place.setIdPlace(idP);
                                        place.setName(token[0]);
                                        place.setCity(token[1]);
                                        place.setTypology(token[2]);
                                        listPlace.add(place);
                                    }
                                }
                                for (int j = 0; j < listPlace.size(); j++) {
                                    if (!listZone.get(i).getName().matches(token[3])) {
                                        zone.setIdPl(idP);
                                        zone.setName(token[3]);
                                        idZ++;
                                        listZone.add(zone);
                                    }
                                }
                                elm.setIdZon(idZ);
                                elm.setTitle(readCsv.getTitleObject());
                                elm.setDescription(readCsv.getDescriptionObject());
                                elm.setQrData(readCsv.getQrDataObject());
                                elm.setPhoto(Uri.parse(readCsv.getLinkImageObject()));
                                String[] str = readCsv.getLinkImageObject().split("Objects_");
                                String[] st = str[1].split("\\?");
                                String idPh = st[0];
                                elm.setIdPhotoAndQrCodeString(idPh);
                                elm.setZoneRif(token[3]);
                                if (!token[8].isEmpty() && !!token[9].isEmpty()) {
                                    constraint.setFromZone(token[8]);
                                    constraint.setInZone(token[9]);
                                }
                                listElement.add(elm);
                                if (!token[8].isEmpty() && !token[9].isEmpty()) {
                                    constraint.setFromZone(token[8]);
                                    constraint.setInZone(token[9]);
                                    listConstranints.add(constraint);
                                }
                            }
                        } else {
                            setDataToken(token, readCsv);
                            if (!token[0].isEmpty()) {
                                elm.setTitle(readCsv.getTitleObject());
                                elm.setDescription(readCsv.getDescriptionObject());
                                elm.setQrData(readCsv.getQrDataObject());
                                elm.setPhoto(Uri.parse(readCsv.getLinkImageObject()));
                                String[] str = readCsv.getLinkImageObject().split("Objects_");
                                String[] st = str[1].split("\\?");
                                String idPh = st[0];
                                elm.setIdPhotoAndQrCodeString(idPh);
                                elm.setZoneRif(token[3]);
                                place.setName(token[0]);
                                place.setCity(token[1]);
                                place.setTypology(token[2]);
                                place.setIdPlace(idP);
                                zone.setIdPl(idP);
                                zone.setName(token[3]);
                                elm.setIdZon(idZ);
                                listPlace.add(place);
                                listZone.add(zone);
                                listElement.add(elm);
                            }
                            if (!token[8].isEmpty() && !token[9].isEmpty()) {
                                constraint.setFromZone(token[8]);
                                constraint.setInZone(token[9]);
                                listConstranints.add(constraint);
                            }
                        }
                    }
                } else {
                    Toast.makeText(FileReceiverActivity.this, "Errore duratnte l'importazione del file, alcuni" + "campi potrebbero essere vuoti", Toast.LENGTH_LONG).show();
                    line = null;
                }
            }
            if (isEmpty) {
                Toast.makeText(FileReceiverActivity.this, "Errore duratnte l'importazione del file, alcuni" + "campi potrebbero essere vuoti", Toast.LENGTH_LONG).show();
            } else {
                savePlace();
                saveConstraints();
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        if (success) {
                            Intent intent = new Intent(FileReceiverActivity.this, DashboardActivity.class);
                            startActivity(intent);
                            progressBar.setVisibility(View.GONE);
                        } else {
                            Toast.makeText(FileReceiverActivity.this, "Salvataggio Luogo non riuscito", Toast.LENGTH_LONG).show();
                            progressBar.setVisibility(View.GONE);
                        }
                    }
                }, 2000L);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : Constraint(com.example.alphatour.oggetti.Constraint) Zone(com.example.alphatour.oggetti.Zone) Element(com.example.alphatour.oggetti.Element) FileNotFoundException(java.io.FileNotFoundException) Handler(android.os.Handler) Intent(android.content.Intent) ElementString(com.example.alphatour.oggetti.ElementString) IOException(java.io.IOException) Uri(android.net.Uri) ReadCsv(com.example.alphatour.ReadCsv) Constraint(com.example.alphatour.oggetti.Constraint) BufferedReader(java.io.BufferedReader) DashboardActivity(com.example.alphatour.DashboardActivity) FileReader(java.io.FileReader) File(java.io.File) Place(com.example.alphatour.oggetti.Place)

Example 2 with ReadCsv

use of com.example.alphatour.ReadCsv in project AlphaTour by Frank99DG.

the class CreatePlaceWizard method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCod == requestCode && resultCode == Activity.RESULT_OK) {
        if (data == null) {
            return;
        } else {
            Uri uri = data.getData();
            Toast.makeText(getContext(), uri.getPath(), Toast.LENGTH_SHORT).show();
            String path = uri.getPath();
            path = path.substring(path.indexOf(":") + 1);
            File file = new File(path);
            try {
                BufferedReader read = new BufferedReader(new FileReader(file));
                String line = "";
                Boolean isEmpty = true;
                while ((line = read.readLine()) != null) {
                    isEmpty = false;
                    String[] token = line.split(",");
                    ReadCsv readCsv = new ReadCsv();
                    boolean flag = false;
                    for (int i = 0; i < token.length; i++) {
                        if (token[i] == null) {
                            flag = true;
                        }
                    }
                    if (!flag) {
                        if (!token[0].matches("namePlace")) {
                            if (token[0] != null && token[1] != null && token[1] != null) {
                                readCsv.setNamePlace(token[0]);
                                readCsv.setCityPlace(token[1]);
                                readCsv.setTypologyPlace(token[2]);
                                place = true;
                            }
                            readCsv.setNameZone(token[3]);
                            readCsv.setTitleObject(token[4]);
                            readCsv.setDescriptionObject(token[5]);
                            readCsv.setQrDataObject(token[6]);
                            readCsv.setLinkImageObject(token[7]);
                            readCsv.setFromZone(token[8]);
                            readCsv.setInZone(token[9]);
                            listLineCsv.add(readCsv);
                        }
                    } else {
                        Toast.makeText(getContext(), "Errore duratnte l'importazione del file, alcuni" + "campi potrebbero essere vuoti", Toast.LENGTH_LONG).show();
                        line = null;
                    }
                }
                if (isEmpty) {
                    Toast.makeText(getContext(), "Errore duratnte l'importazione del file, alcuni" + "campi potrebbero essere vuoti", Toast.LENGTH_LONG).show();
                } else {
                    if (place) {
                    // savePlace();
                    } else {
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) Uri(android.net.Uri) File(java.io.File) ReadCsv(com.example.alphatour.ReadCsv)

Aggregations

Uri (android.net.Uri)2 ReadCsv (com.example.alphatour.ReadCsv)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 Intent (android.content.Intent)1 Handler (android.os.Handler)1 DashboardActivity (com.example.alphatour.DashboardActivity)1 Constraint (com.example.alphatour.oggetti.Constraint)1 Element (com.example.alphatour.oggetti.Element)1 ElementString (com.example.alphatour.oggetti.ElementString)1 Place (com.example.alphatour.oggetti.Place)1 Zone (com.example.alphatour.oggetti.Zone)1