Package it.marcoberri.mbmeteo.model

Examples of it.marcoberri.mbmeteo.model.Cache


        if (cacheReadEnable) {

            final Query q = ds.find(Cache.class);
            q.filter("cacheKey", cacheKey).filter("servletName", this.getClass().getName());

            final Cache c = (Cache) q.get();

            if (c == null) {
                log.info("cacheKey:" + cacheKey + " on servletName: " + this.getClass().getName() + " not found");
            }

            if (c != null) {
                final GridFSDBFile imageForOutput = MongoConnectionHelper.getGridFS().findOne(new ObjectId(c.getGridId()));
                if (imageForOutput != null) {
                    ds.save(c);

                    try {
                        response.setHeader("Content-Length", "" + imageForOutput.getLength());
                        response.setHeader("Content-Disposition", "inline; filename=\"" + imageForOutput.getFilename() + "\"");
                        final OutputStream out = response.getOutputStream();
                        final InputStream in = imageForOutput.getInputStream();
                        final byte[] content = new byte[(int) imageForOutput.getLength()];
                        in.read(content);
                        out.write(content);
                        in.close();
                        out.close();
                        return;
                    } catch (Exception e) {
                        log.error(e);
                    }

                } else {
                    log.error("file not in db");
                }
            }
        }


        final String formatIn = getFormatIn(period);
        final String formatOut = getFormatOut(period);

        final Query q = ds.createQuery(MapReduceMinMax.class).disableValidation();

        final Date dFrom = DateTimeUtil.getDate("yyyy-MM-dd hh:mm:ss", from);
        final Date dTo = DateTimeUtil.getDate("yyyy-MM-dd hh:mm:ss", to);


        final List<Date> datesIn = getRangeDate(dFrom, dTo);
        final HashSet<String> datesInString = new HashSet<String>();

        for (Date d : datesIn) {
            datesInString.add(DateTimeUtil.dateFormat(formatIn, d));
        }

        if (datesIn != null && !datesIn.isEmpty()) {
            q.filter("_id in", datesInString);
        }
        q.order("_id");

        final List<MapReduceMinMax> mapReduceResult = q.asList();
        final TimeSeries serieMin = new TimeSeries("Min");
        final TimeSeries serieMax = new TimeSeries("Max");

        for (MapReduceMinMax m : mapReduceResult) {
            try {

                final Date tmpDate = DateTimeUtil.getDate(formatIn, m.getId().toString());
                if (tmpDate == null) {
                    continue;
                }

                final Millisecond t = new Millisecond(tmpDate);

                ChartEnumMinMaxHelper chartEnum = ChartEnumMinMaxHelper.getByFieldAndType(field, "min");
                Method method = m.getClass().getMethod(chartEnum.getMethod());
                Number n = (Number) method.invoke(m);
                serieMin.add(t, n);


                chartEnum = ChartEnumMinMaxHelper.getByFieldAndType(field, "max");
                method = m.getClass().getMethod(chartEnum.getMethod());
                n = (Number) method.invoke(m);
                serieMax.add(t, n);


            } catch (IllegalAccessException ex) {
                log.error(ex);
            } catch (IllegalArgumentException ex) {
                log.error(ex);
            } catch (InvocationTargetException ex) {
                log.error(ex);
            } catch (NoSuchMethodException ex) {
                log.error(ex);
            } catch (SecurityException ex) {
                log.error(ex);
            }
        }


        final ChartEnumMinMaxHelper chartData = ChartEnumMinMaxHelper.getByFieldAndType(field, "min");


        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(serieMin);
        dataset.addSeries(serieMax);

        final JFreeChart chart = ChartFactory.createTimeSeriesChart("Max/Min", "", chartData.getUm(), dataset, true, false, false);
        final XYPlot plot = (XYPlot) chart.getPlot();
        final DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat(formatOut));

        axis.setVerticalTickLabels(true);

        if (field.toUpperCase().indexOf("PRESSURE") != -1) {
            plot.getRangeAxis().setRange(chartPressureMin, chartPressureMax);
        }

        final File f = File.createTempFile("mbmeteo", ".jpg");
        ChartUtilities.saveChartAsJPEG(f, chart, dimx, dimy);

        try {

            if (cacheWriteEnable) {
                final GridFSInputFile gfsFile = MongoConnectionHelper.getGridFS().createFile(f);
                gfsFile.setFilename(f.getName());
                gfsFile.save();

                final Cache c = new Cache();
                c.setServletName(this.getClass().getName());
                c.setCacheKey(cacheKey);
                c.setGridId(gfsFile.getId().toString());

                ds.save(c);

            }
View Full Code Here


        if (cacheReadEnable) {
            final Query q = ds.find(Cache.class);
            q.filter("cacheKey", cacheKey).filter("servletName", this.getClass().getName());

            final Cache c = (Cache) q.get();

            if (c == null) {
                log.info("cacheKey:" + cacheKey + " on servletName: " + this.getClass().getName() + " not found");
            }

            if (c != null) {
                log.debug("get file from cache id: " + c.getGridId());
                final GridFSDBFile imageForOutput = MongoConnectionHelper.getGridFS().findOne(new ObjectId(c.getGridId()));
                if (imageForOutput != null) {

                    ds.save(c);

                    try {
                        response.setHeader("Content-Length", "" + imageForOutput.getLength());
                        response.setHeader("Content-Disposition", "inline; filename=\"" + imageForOutput.getFilename() + "\"");
                        final OutputStream out = response.getOutputStream();
                        final InputStream in = imageForOutput.getInputStream();
                        final byte[] content = new byte[(int) imageForOutput.getLength()];
                        in.read(content);
                        out.write(content);
                        in.close();
                        out.close();
                        return;
                    } catch (Exception e) {
                        log.error(e);
                    }

                } else {
                    log.error("file not in db");
                }
            }
        }

        final String titleChart = ChartEnumHelper.getByName(field).getTitle();
        final String umChart = ChartEnumHelper.getByName(field).getUm();
        final Query q = ds.createQuery(Meteolog.class);
        final Date dFrom = DateTimeUtil.getDate("yyyy-MM-dd hh:mm:ss", from);
        final Date dTo = DateTimeUtil.getDate("yyyy-MM-dd hh:mm:ss", to);

        q.disableValidation().filter("time >=", dFrom).filter("time <=", dTo);

        final List<Meteolog> meteoLogList = q.asList();
        final TimeSeries series = new TimeSeries(umChart);

        for (Meteolog m : meteoLogList) {
            final Millisecond t = new Millisecond(m.getTime());
            try {
                //violenza di una reflection
                final Method method = m.getClass().getMethod(ChartEnumHelper.getByName(field).getMethod());
                final Number n = (Number) method.invoke(m);
                series.add(t, n);
            } catch (NoSuchMethodException ex) {
                log.error(ex);
            } catch (InvocationTargetException ex) {
                log.error(ex);
            } catch (IllegalAccessException ex) {
                log.error(ex);
            } catch (SecurityException ex) {
                log.error(ex);
            }

        }



        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(series);

        final JFreeChart chart = ChartFactory.createTimeSeriesChart(titleChart, "", umChart, dataset, false, false, false);
        final XYPlot plot = (XYPlot) chart.getPlot();
        final DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yyyy HH:mm"));
        axis.setVerticalTickLabels(true);
     
        if (field.toUpperCase().indexOf("PRESSURE") != -1) {
            plot.getRangeAxis().setRange(chartPressureMin, chartPressureMax);
        }


        final File f = File.createTempFile("mbmeteo", ".jpg");
        ChartUtilities.saveChartAsJPEG(f, chart, dimx, dimy);

        try {

            if (cacheWriteEnable) {

                final GridFSInputFile gfsFile = MongoConnectionHelper.getGridFS().createFile(f);
                gfsFile.setFilename(f.getName());
                gfsFile.save();

                final Cache c = new Cache();
                c.setServletName(this.getClass().getName());
                c.setCacheKey(cacheKey);
                c.setGridId(gfsFile.getId().toString());

                ds.save(c);

            }
View Full Code Here

        if (cacheReadEnable) {

            final Query q = ds.find(Cache.class);
            q.filter("cacheKey", cacheKey).filter("servletName", this.getClass().getName());

            final Cache c = (Cache) q.get();

            if (c == null) {
                log.info("cacheKey:" + cacheKey + " on servletName: " + this.getClass().getName() + " not found");
            }

            if (c != null) {
                final GridFSDBFile imageForOutput = MongoConnectionHelper.getGridFS().findOne(new ObjectId(c.getGridId()));
                if (imageForOutput != null) {
                    ds.save(c);

                    try {
                        response.setHeader("Content-Length", "" + imageForOutput.getLength());
                        response.setHeader("Content-Disposition", "inline; filename=\"" + imageForOutput.getFilename() + "\"");
                        final OutputStream out = response.getOutputStream();
                        final InputStream in = imageForOutput.getInputStream();
                        final byte[] content = new byte[(int) imageForOutput.getLength()];
                        in.read(content);
                        out.write(content);
                        in.close();
                        out.close();
                        return;
                    } catch (Exception e) {
                        log.error(e);
                    }

                } else {
                    log.error("file not in db");
                }
            }
        }


        final Query q = ds.createQuery(MapReduceMinMax.class).disableValidation();
        final Date dFrom = DateTimeUtil.getDate("yyyy-MM-dd hh:mm:ss", from);
        final Date dTo = DateTimeUtil.getDate("yyyy-MM-dd hh:mm:ss", to);

        final String formatIn = getFormatIn(period);
        final String formatOut = getFormatOut(period);

        final List<Date> datesIn = getRangeDate(dFrom, dTo);
        final HashSet<String> datesInString = new HashSet<String>();

        for (Date d : datesIn) {
            datesInString.add(DateTimeUtil.dateFormat(formatIn, d));
        }

        if (datesIn != null && !datesIn.isEmpty()) {
            q.filter("_id in", datesInString);
        }
        q.order("_id");

        final List<MapReduceMinMax> mapReduceResult = q.asList();

        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        ChartEnumMinMaxHelper chartEnum = ChartEnumMinMaxHelper.getByFieldAndType(field, type);

        for (MapReduceMinMax m : mapReduceResult) {
            try {
                final Date tmpDate = DateTimeUtil.getDate(formatIn, m.getId().toString());

                if (tmpDate == null) {
                    continue;
                }

                final Method method = m.getClass().getMethod(chartEnum.getMethod());
                final Number n = (Number) method.invoke(m);
                dataset.addValue(n, chartEnum.getType(), DateTimeUtil.dateFormat(formatOut, tmpDate));


            } catch (IllegalAccessException ex) {
                log.error(ex);
            } catch (IllegalArgumentException ex) {
                log.error(ex);
            } catch (InvocationTargetException ex) {
                log.error(ex);
            } catch (NoSuchMethodException ex) {
                log.error(ex);
            } catch (SecurityException ex) {
                log.error(ex);
            }
        }

        final JFreeChart chart = ChartFactory.createBarChart(
                chartEnum.getTitle(), // chart title
                "", // domain axis label
                chartEnum.getUm(), // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                false, // include legend
                true, // tooltips?
                false // URLs?
                );

        final CategoryPlot xyPlot = (CategoryPlot) chart.getPlot();
        final CategoryAxis domain = xyPlot.getDomainAxis();

         if (field.toUpperCase().indexOf("PRESSURE") != -1) {
              xyPlot.getRangeAxis().setRange(chartPressureMin, chartPressureMax);
        } else {
            xyPlot.getRangeAxis().setAutoRange(true);
        }

        domain.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);

        final File f = File.createTempFile("mbmeteo", ".jpg");
        ChartUtilities.saveChartAsJPEG(f, chart, dimx, dimy);

        try {

            if (cacheWriteEnable) {
                final GridFSInputFile gfsFile = MongoConnectionHelper.getGridFS().createFile(f);
                gfsFile.setFilename(f.getName());
                gfsFile.save();

                final Cache c = new Cache();
                c.setServletName(this.getClass().getName());
                c.setCacheKey(cacheKey);
                c.setGridId(gfsFile.getId().toString());

                ds.save(c);

            }
View Full Code Here

TOP

Related Classes of it.marcoberri.mbmeteo.model.Cache

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.