Search in sources :

Example 1 with CsvUtils

use of org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils in project data-access by pentaho.

the class CsvDatasourceServiceImpl method getPreviewRows.

public List<String> getPreviewRows(String filename, boolean isFirstRowHeader, int rows, String encoding) throws Exception {
    checkPermissions();
    List<String> previewRows = null;
    if (!StringUtils.isEmpty(filename)) {
        CsvUtils service = new CsvUtils();
        ModelInfo mi = service.getFileContents("", filename, ",", "\"", rows, isFirstRowHeader, // $NON-NLS-1$  //$NON-NLS-2$  //$NON-NLS-3$
        encoding);
        previewRows = mi.getFileInfo().getContents();
    }
    return previewRows;
}
Also used : CsvUtils(org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils) ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)

Example 2 with CsvUtils

use of org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils in project data-access by pentaho.

the class CsvDatasourceServiceImpl method stageFile.

public ModelInfo stageFile(String fileName, String delimiter, String enclosure, boolean isFirstRowHeader, String encoding) throws Exception {
    checkPermissions();
    ModelInfo modelInfo;
    fileName = FilenameUtils.getName(fileName);
    try {
        int headerRows = isFirstRowHeader ? 1 : 0;
        modelInfo = new CsvUtils().generateFields("", fileName, AgileHelper.getCsvSampleRowSize(), delimiter, enclosure, headerRows, true, true, // $NON-NLS-1$
        encoding);
    } catch (FileNotFoundException e) {
        logger.error(e);
        throw new Exception("File was not found: " + fileName);
    } catch (Exception e) {
        logger.error(e);
        throw e;
    }
    return modelInfo;
}
Also used : CsvUtils(org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils) ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) FileNotFoundException(java.io.FileNotFoundException) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)

Example 3 with CsvUtils

use of org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils in project data-access by pentaho.

the class MetadataDatasourceService method uploadServletImportMetadataDatasource.

/**
 * @param localizeBundleEntries
 * @param domainId
 *          Unique identifier for the metadata datasource
 * @param metadataFile
 *          Input stream for the metadata.xmi
 *
 * @return Response containing the success of the method
 *
 * @throws PentahoAccessControlException
 *           Thrown when validation of access fails
 */
@PUT
@Path("/uploadServletImport")
@Consumes({ TEXT_PLAIN })
@Produces("text/plain")
@Deprecated
@Facet(name = "Unsupported")
public Response uploadServletImportMetadataDatasource(String localizeBundleEntries, @QueryParam("domainId") String domainId, @QueryParam("metadataFile") String metadataFile) throws PentahoAccessControlException {
    try {
        DatasourceService.validateAccess();
    } catch (PentahoAccessControlException e) {
        return Response.serverError().entity(e.toString()).build();
    }
    IMetadataDomainRepository metadataDomainRepository = PentahoSystem.get(IMetadataDomainRepository.class, PentahoSessionHolder.getSession());
    PentahoMetadataDomainRepository metadataImporter = new PentahoMetadataDomainRepository(PentahoSystem.get(IUnifiedRepository.class));
    CsvUtils csvUtils = new CsvUtils();
    boolean validPropertyFiles = true;
    StringBuffer invalidFiles = new StringBuffer();
    try {
        String TMP_FILE_PATH = File.separatorChar + "system" + File.separatorChar + "tmp" + File.separatorChar;
        String sysTmpDir = PentahoSystem.getApplicationContext().getSolutionPath(TMP_FILE_PATH);
        FileInputStream metadataInputStream = new FileInputStream(sysTmpDir + File.separatorChar + metadataFile);
        metadataImporter.storeDomain(metadataInputStream, domainId, true);
        metadataDomainRepository.getDomain(domainId);
        StringTokenizer bundleEntriesParam = new StringTokenizer(localizeBundleEntries, ";");
        while (bundleEntriesParam.hasMoreTokens()) {
            String localizationBundleElement = bundleEntriesParam.nextToken();
            StringTokenizer localizationBundle = new StringTokenizer(localizationBundleElement, "=");
            String localizationFileName = localizationBundle.nextToken();
            String localizationFile = localizationBundle.nextToken();
            if (localizationFileName.endsWith(".properties")) {
                String encoding = csvUtils.getEncoding(localizationFile);
                if (ENCODINGS.contains(encoding)) {
                    for (final Pattern propertyBundlePattern : patterns) {
                        final Matcher propertyBundleMatcher = propertyBundlePattern.matcher(localizationFileName);
                        if (propertyBundleMatcher.matches()) {
                            FileInputStream bundleFileInputStream = new FileInputStream(sysTmpDir + File.separatorChar + localizationFile);
                            metadataImporter.addLocalizationFile(domainId, propertyBundleMatcher.group(2), bundleFileInputStream, true);
                            break;
                        }
                    }
                } else {
                    validPropertyFiles = false;
                    invalidFiles.append(localizationFileName);
                }
            } else {
                validPropertyFiles = false;
                invalidFiles.append(localizationFileName);
            }
        }
        if (!validPropertyFiles) {
            return Response.serverError().entity(Messages.getString("MetadataDatasourceService.ERROR_002_PROPERTY_FILES_ERROR") + invalidFiles.toString()).build();
        }
        return Response.ok("SUCCESS").type(MediaType.TEXT_PLAIN).build();
    } catch (Exception e) {
        metadataImporter.removeDomain(domainId);
        return Response.serverError().entity(Messages.getString("MetadataDatasourceService.ERROR_001_METADATA_DATASOURCE_ERROR")).build();
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) PentahoMetadataDomainRepository(org.pentaho.platform.plugin.services.metadata.PentahoMetadataDomainRepository) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) FileInputStream(java.io.FileInputStream) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) CsvUtils(org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils) StringTokenizer(java.util.StringTokenizer) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) Facet(org.codehaus.enunciate.Facet)

Example 4 with CsvUtils

use of org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils in project data-access by pentaho.

the class CsvDatasourceServiceImpl method getEncoding.

public String getEncoding(String fileName) {
    checkPermissions();
    String encoding = null;
    try {
        CsvUtils csvModelService = new CsvUtils();
        encoding = csvModelService.getEncoding(fileName);
    } catch (Exception e) {
        logger.error(e);
    }
    return encoding;
}
Also used : CsvUtils(org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)

Aggregations

CsvUtils (org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils)4 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 CsvTransformGeneratorException (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)2 ModelInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)2 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)2 FileInputStream (java.io.FileInputStream)1 StringTokenizer (java.util.StringTokenizer)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Facet (org.codehaus.enunciate.Facet)1 IMetadataDomainRepository (org.pentaho.metadata.repository.IMetadataDomainRepository)1 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)1 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)1 PentahoMetadataDomainRepository (org.pentaho.platform.plugin.services.metadata.PentahoMetadataDomainRepository)1