Example usage for com.mongodb.gridfs GridFSFile getId

List of usage examples for com.mongodb.gridfs GridFSFile getId

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFSFile getId.

Prototype

public Object getId() 

Source Link

Document

Gets the id.

Usage

From source file:com.kurento.kmf.repository.internal.repoimpl.mongo.MongoRepositoryItem.java

License:Open Source License

private MongoRepositoryItem(MongoRepository repository, GridFSFile dbFile, State state) {

    super(dbFile.getId().toString(), state, loadAttributes(dbFile), repository);

    this.dbFile = dbFile;
    this.setMetadata(new HashMap<String, String>());
}

From source file:org.craftercms.commons.mongo.FileInfo.java

License:Open Source License

FileInfo(final GridFSFile savedFile, final boolean withInputStream) {
    this.md5 = savedFile.getMD5();
    this.fileId = (ObjectId) savedFile.getId();
    this.contentType = savedFile.getContentType();
    this.fileSize = FileUtils.readableFileSize(savedFile.getLength());
    this.storeName = savedFile.getFilename();
    this.savedDate = savedFile.getUploadDate();
    this.fileSizeBytes = savedFile.getLength();
    if (withInputStream && savedFile instanceof GridFSDBFile) {
        this.inputStream = ((GridFSDBFile) savedFile).getInputStream();
    }//from ww w  . j a v  a  2  s  .c om
    attributes = new HashMap<>();
}

From source file:org.craftercms.studio.impl.repository.mongodb.services.impl.NodeServiceImpl.java

License:Open Source License

private CoreMetadata createNodeMetadata(final String fileName, final String creatorName,
        final InputStream content, final String folderLabel) throws MongoRepositoryException {
    CoreMetadata coreMetadata = createBasicMetadata(fileName, creatorName, folderLabel);
    try {/*from  ww  w . j  a  v  a  2s  .  c  o m*/
        GridFSFile savedFile = gridFSService.saveFile(fileName, content);
        coreMetadata.setSize(savedFile.getLength());
        coreMetadata.setFileId(savedFile.getId().toString());
    } catch (DataAccessException ex) {
        log.error("Unable to save {} file due a DataAccessException", fileName);
        log.error("DataAccessException thrown ", ex);
        throw new MongoRepositoryException("Unable to save file due a DataAccessException", ex);
    }
    return coreMetadata;
}

From source file:org.eclipse.hawkbit.artifact.repository.ArtifactStore.java

License:Open Source License

/**
 * Maps a single {@link GridFSFile} to {@link DbArtifact}.
 *
 * @param tenant//from   w  ww  .j  a va2 s.  co  m
 *            the tenant
 * @param dbFile
 *            the mongoDB gridFs file.
 * @return a mapped artifact from the given dbFile
 */
private static GridFsArtifact map(final GridFSFile fsFile) {
    if (fsFile == null) {
        return null;
    }
    final GridFsArtifact artifact = new GridFsArtifact(fsFile);
    artifact.setArtifactId(fsFile.getId().toString());
    artifact.setSize(fsFile.getLength());
    artifact.setContentType(fsFile.getContentType());
    artifact.setHashes(new DbArtifactHash(fsFile.getFilename(), fsFile.getMD5()));
    return artifact;
}

From source file:org.kurento.repository.internal.repoimpl.mongo.MongoRepositoryItem.java

License:Apache License

private MongoRepositoryItem(MongoRepository repository, GridFSFile dbFile, State state) {

    super(dbFile.getId().toString(), state, loadAttributes(dbFile), repository);

    this.dbFile = dbFile;
    // don't call ours setMetadata(...)
    super.setMetadata(new HashMap<String, String>());
}

From source file:org.opentestsystem.authoring.testitembank.service.impl.ImportSetServiceImpl.java

License:Open Source License

/**
 * processes import file.// w  w w.j  ava 2s  .  c  o m
 * 
 * @param importFile to process.
 * @param importSetId import set it is a part of.
 */
private void processImportFile(final ImportFile importFile, final String importSetId, final String tenantId,
        final String itemBank) {
    int itemsImported = 0;
    final File fileToImport = importFile.getFile();
    if (fileToImport == null || !fileToImport.exists()) {
        importFile.addMessage("file.not.found");
        importFile.setImportStatus(ImportStatus.FILE_NOT_FOUND);
    } else {
        try {
            // DO WE NEED THIS METADATA?
            final BasicDBObject metadata = new BasicDBObject();
            metadata.put("fileName", fileToImport.getName());
            metadata.put("importSetId", importSetId);
            final GridFSFile gridfsFile = this.gridFsRepository.save(fileToImport, fileToImport.getName(),
                    metadata);
            final String gridfsId = gridfsFile.getId().toString();

            // extract items in file

            final List<ApipItemContent> itemContents = this.apipZipInputFileExtractorService
                    .createItems(importFile.getFile(), gridfsId, tenantId, itemBank);

            // save/validate items
            for (final ApipItemContent itemContent : itemContents) {
                final ItemValidator itemValidator = new ItemValidator();
                final Item item = itemContent.getItem();
                final String itemIdentifier = item.getIdentifier();
                try {
                    final Errors errors = validate(itemValidator, itemContent);
                    if (errors.hasErrors()) {
                        importFile.setImportStatus(ImportStatus.FAILED);
                        for (final ObjectError objectError : errors.getAllErrors()) {
                            importFile.addMessage(item.getIdentifier(), objectError.getCode(),
                                    (String[]) objectError.getArguments());
                        }
                    } else {
                        String itemfsId = "";
                        if (itemContents.size() == 1) {
                            itemfsId = gridfsId;
                        } else {
                            final BasicDBObject itemGridFSMetadata = new BasicDBObject();
                            final String itemFileName = item.getIdentifier() + "_" + fileToImport.getName();
                            itemGridFSMetadata.put("fileName", itemFileName);
                            itemGridFSMetadata.put("importSetId", importSetId);
                            itemGridFSMetadata.put("itemIdentifier", item.getIdentifier());
                            itemGridFSMetadata.put("version", item.getVersion());
                            itemGridFSMetadata.put("originalFileId", gridfsId);
                            final GridFSFile itemfsFile = this.gridFsRepository.save(itemContent.getItemZip(),
                                    itemFileName, itemGridFSMetadata);
                            itemfsId = itemfsFile.getId().toString();
                        }
                        item.setItemZipGridId(itemfsId);
                        this.itemRepository.addItem(item);
                        itemsImported++;
                        importFile.addMessage(itemIdentifier, "item.successfully.added",
                                new String[] { item.getIdentifier(), item.getVersion() });

                        for (final Entry<String, Object> metadataEntry : item.getAllIncludedMetatdata()
                                .entrySet()) {
                            this.itemMetadataService.saveNewItemMetadata(tenantId, metadataEntry.getKey(),
                                    metadataEntry.getValue());
                        }
                    }
                } catch (final TestItemBankException tibE) {
                    importFile.setImportStatus(ImportStatus.FAILED);
                    importFile.addMessage(itemIdentifier, tibE.getMessageCode(), tibE.getMessageArgs());
                } catch (final DuplicateKeyException e) {
                    importFile.setImportStatus(ImportStatus.FAILED);
                    importFile.addMessage(itemIdentifier, "item.already.exists",
                            new String[] { item.getIdentifier(), item.getVersion() });
                }
            }

            if (importFile.getImportStatus() != ImportStatus.FAILED) {
                importFile.addMessage("Successful import");
                importFile.setImportStatus(ImportStatus.IMPORT_COMPLETE);
            }
            final String message = itemsImported + " items created from import file " + importFile.getPathName()
                    + " from import set:" + importSetId;
            this.alertBeacon.sendAlert(MnaSeverity.INFO, MnaAlertType.TIB_IMPORT.name(), message);
        } catch (final TestItemBankException tibE) {
            importFile.setImportStatus(ImportStatus.FAILED);
            importFile.addMessage("", tibE.getMessageCode(), tibE.getMessageArgs());
            sendZipFileMonitoringAndAlertingErrors(tibE.getMessageCode(),
                    new String[] { importFile.getPathName() });
        } catch (final Exception e) {
            LOGGER.error("unknown error importing file", e);
            importFile.addMessage("unexpected.error");
            importFile.setImportStatus(ImportStatus.FAILED);
        }
    }
}

From source file:org.opentestsystem.delivery.testreg.service.impl.FileUploadServiceImpl.java

License:Open Source License

@Override
public FileUploadResponse saveFile(final String fileName, final InputStream inputStream,
        final String formatType) {

    final DBObject metadata = new BasicDBObject("formatType", formatType);
    final FileUploadResponse response = new FileUploadResponse();
    try {//from ww  w .j av  a  2  s . co m
        final GridFSFile gridFile = this.fileUploadGridFsRepository.save(inputStream, fileName, metadata);
        inputStream.close();
        response.setFileName(fileName);
        response.setFileGridFsId(gridFile.getId().toString());
        response.setStatusCode(HttpStatus.CREATED.value());
        response.setMessage("File uploaded successfully");
    } catch (final IOException ioe) {
        response.setStatusCode(HttpStatus.UNPROCESSABLE_ENTITY.value());
        response.setMessage("File Failed to upload ");
        throw new LocalizedException("fileupload.save.error", ioe);
    }

    return response;
}

From source file:org.opentestsystem.shared.progman.service.impl.AssetPoolServiceImpl.java

License:Open Source License

@Override
public Asset saveAssetFile(final String assetPoolId, final String fileName, final byte[] inBytes,
        final String fileContentType) {

    final AssetType assetType = determineAssetType(fileContentType);
    if (assetType == null) {
        throw new LocalizedException("upload.type.not.supported", new String[] { fileContentType });
    }/*w  ww .  j  a  v  a  2s. c  o m*/

    final AssetPool assetPool = getAssetPool(assetPoolId);
    if (assetPool == null) {
        throw createAssetPoolNotFoundException(assetPoolId);
    }

    final DBObject metadata = null;
    final GridFSFile gridFile = this.assetGridFsRepository.save(inBytes, fileName, metadata);
    final Asset asset = new Asset();
    asset.setType(assetType);
    asset.setAssetFileGridId(gridFile.getId().toString());
    asset.setAssetFileName(gridFile.getFilename());
    asset.setBasePath(this.pmBaseUrl);
    asset.setFileContentType(fileContentType);
    asset.setProperty("/assetPool" + asset.calculatePropertyValueForFile());
    assetPool.addAsset(asset);

    final Set<String> duplicateFiles = findDuplicateFileNames(assetPool.getAssets());
    if (!duplicateFiles.isEmpty()) {
        throw new LocalizedException("assetPool.duplicate.assets", new String[] { duplicateFiles.toString() });
    }

    saveAssetPool(assetPool);
    return asset;
}

From source file:org.swarmcom.jsynapse.dao.ContentRepositoryImpl.java

License:Apache License

@Override
public String upload(InputStream content, String fileName, String contentType) {
    GridFSFile file = gridFsTemplate.store(content, fileName, contentType);
    return file.getId().toString();
}