Search in sources :

Example 76 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class DirectoryRecordStore method getRecords.

@Override
public RecordReader getRecords(final PathName path) {
    final RecordDefinition recordDefinition = getRecordDefinition(path);
    final Resource resource = getResource(path.toString(), recordDefinition);
    final RecordReader reader = RecordReader.newRecordReader(resource);
    if (reader == null) {
        throw new IllegalArgumentException("Cannot find reader for: " + path);
    } else {
        final String typePath = this.typePathByResource.get(resource);
        reader.setProperty("schema", recordDefinition.getSchema());
        reader.setProperty("typePath", typePath);
        return reader;
    }
}
Also used : RecordReader(com.revolsys.record.io.RecordReader) PathResource(com.revolsys.spring.resource.PathResource) Resource(com.revolsys.spring.resource.Resource) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 77 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class DirectoryRecordStore method insertRecord.

@Override
public synchronized void insertRecord(final Record record) {
    final RecordDefinition recordDefinition = record.getRecordDefinition();
    final String typePath = recordDefinition.getPath();
    RecordWriter writer = this.writers.get(typePath);
    if (writer == null) {
        final String schemaName = PathUtil.getPath(typePath);
        final File subDirectory = FileUtil.getDirectory(getDirectory(), schemaName);
        final String fileExtension = getFileExtension();
        final File file = new File(subDirectory, recordDefinition.getName() + "." + fileExtension);
        final Resource resource = new PathResource(file);
        writer = RecordWriter.newRecordWriter(recordDefinition, resource);
        if (writer == null) {
            throw new RuntimeException("Cannot create writer for: " + typePath);
        } else if (writer instanceof ObjectWithProperties) {
            final ObjectWithProperties properties = writer;
            properties.setProperties(getProperties());
        }
        this.writers.put(typePath, writer);
    }
    writer.write(record);
    addStatistic("Insert", record);
}
Also used : RecordWriter(com.revolsys.record.io.RecordWriter) PathResource(com.revolsys.spring.resource.PathResource) ObjectWithProperties(com.revolsys.properties.ObjectWithProperties) PathResource(com.revolsys.spring.resource.PathResource) Resource(com.revolsys.spring.resource.Resource) File(java.io.File) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 78 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class PdfImage method newBufferedImage.

protected BufferedImage newBufferedImage() {
    final Resource imageResource = getImageResource();
    try {
        final File file = Resource.getOrDownloadFile(imageResource);
        // TODO password support
        final PDDocument document = PDDocument.loadNonSeq(file, null, null);
        @SuppressWarnings("unchecked") final List<PDPage> pages = document.getDocumentCatalog().getAllPages();
        if (pages.isEmpty()) {
            throw new RuntimeException("PDF file " + imageResource + " doesn't contain any pages");
        } else {
            if (pages.size() > 1) {
                Logs.warn(this, "PDF file " + imageResource + " doesn't contais more than 1 page");
            }
            final PDPage page = pages.get(0);
            final COSDictionary pageDictionary = page.getCOSDictionary();
            final Rectangle2D mediaBox = PdfUtil.findRectangle(pageDictionary, COSName.MEDIA_BOX);
            final int resolution = 72;
            BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_ARGB, resolution);
            final COSDictionary viewport = PdfUtil.getPageViewport(pageDictionary);
            if (viewport != null) {
                final Rectangle2D bbox = PdfUtil.findRectangle(viewport, COSName.BBOX);
                if (bbox != null) {
                    final double boxX = bbox.getX();
                    final double boxY = bbox.getY();
                    final int boxWidth = (int) bbox.getWidth();
                    final int boxHeight = (int) bbox.getHeight();
                    final BufferedImage viewportImage = new BufferedImage(boxWidth, boxHeight, BufferedImage.TYPE_INT_ARGB);
                    final Graphics2D graphics = (Graphics2D) viewportImage.getGraphics();
                    final double translateY = -(mediaBox.getHeight() - (boxHeight + boxY));
                    graphics.translate(-boxX, translateY);
                    graphics.scale(1, 1);
                    graphics.drawImage(image, 0, 0, null);
                    graphics.dispose();
                    image = viewportImage;
                }
                final BoundingBox boundingBox = PdfUtil.getViewportBoundingBox(viewport);
                setBoundingBox(boundingBox);
                setResolution(boundingBox.getWidth() / image.getWidth());
            }
            return image;
        }
    } catch (final IOException e) {
        throw new RuntimeException("Error loading PDF file " + imageResource, e);
    }
}
Also used : PDPage(org.apache.pdfbox.pdmodel.PDPage) COSDictionary(org.apache.pdfbox.cos.COSDictionary) Resource(com.revolsys.spring.resource.Resource) Rectangle2D(java.awt.geom.Rectangle2D) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) BoundingBox(com.revolsys.geometry.model.BoundingBox) File(java.io.File)

Aggregations

Resource (com.revolsys.spring.resource.Resource)78 PathResource (com.revolsys.spring.resource.PathResource)23 MapEx (com.revolsys.collection.map.MapEx)9 File (java.io.File)9 IOException (java.io.IOException)8 InputStream (java.io.InputStream)6 LinkedHashMapEx (com.revolsys.collection.map.LinkedHashMapEx)5 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)5 UrlResource (com.revolsys.spring.resource.UrlResource)5 Writer (java.io.Writer)5 Record (com.revolsys.record.Record)4 RecordDefinition (com.revolsys.record.schema.RecordDefinition)4 DataType (com.revolsys.datatype.DataType)3 BoundingBox (com.revolsys.geometry.model.BoundingBox)3 Geometry (com.revolsys.geometry.model.Geometry)3 AbstractRecordWriter (com.revolsys.io.AbstractRecordWriter)3 ArrayRecord (com.revolsys.record.ArrayRecord)3 RecordReader (com.revolsys.record.io.RecordReader)3 RecordWriter (com.revolsys.record.io.RecordWriter)3 Blob (java.sql.Blob)3