Search in sources :

Example 1 with IPentahoLocale

use of org.pentaho.platform.api.locale.IPentahoLocale in project pentaho-platform by pentaho.

the class DefaultUnifiedRepositoryContentIT method testLocalePropertiesMap.

@Test
public void testLocalePropertiesMap() throws Exception {
    loginAsSysTenantAdmin();
    ITenant tenantAcme = tenantManager.createTenant(systemTenant, TENANT_ID_ACME, tenantAdminRoleName, tenantAuthenticatedRoleName, ANONYMOUS_ROLE_NAME);
    userRoleDao.createUser(tenantAcme, USERNAME_SUZY, PASSWORD, "", new String[] { tenantAdminRoleName });
    logout();
    login(USERNAME_SUZY, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
    // Create file
    final String fileName = "locale.sample";
    RepositoryFile file = createSampleFile(ClientRepositoryPaths.getUserHomeFolderPath(USERNAME_SUZY), fileName, "test", false, 123);
    // Test filename title matches created file name
    assertEquals(fileName, file.getTitle());
    final String DEFAULT_LOCALE = "default";
    final IPentahoLocale SPANISH = new PentahoLocale(new Locale("es"));
    final IPentahoLocale US = new PentahoLocale(Locale.US);
    final String TITLE = "title";
    final String DESCRIPTION = "description";
    final String EN_US_TITLE = "Locale Sample";
    final String EN_US_DESCRIPTION = "This is a test for retrieving localized words";
    final String SP_TITLE = "Muestra de Localizacion";
    final String SP_DESCRIPTION = "Esta es una prueba para buscar palabras localizadas";
    RepositoryFile.Builder builder = new RepositoryFile.Builder(file);
    Map<String, Properties> localeMap = new HashMap<String, Properties>();
    // Set English locale values
    final Properties enProperties = new Properties();
    enProperties.setProperty(TITLE, EN_US_TITLE);
    enProperties.setProperty(DESCRIPTION, EN_US_DESCRIPTION);
    localeMap.put(US.toString(), enProperties);
    // Set Spanish locale values
    final Properties esProperties = new Properties();
    esProperties.setProperty(TITLE, SP_TITLE);
    esProperties.setProperty(DESCRIPTION, SP_DESCRIPTION);
    localeMap.put(SPANISH.toString(), esProperties);
    builder.localePropertiesMap(localeMap);
    // Update file data
    final SampleRepositoryFileData modContent = new SampleRepositoryFileData("blah", false, 123);
    repo.updateFile(builder.build(), modContent, null);
    // Retrieve file - gets full map
    final RepositoryFile updatedFile = repo.getFile(file.getPath(), true);
    // Assert messages are the same
    Properties ep = updatedFile.getLocalePropertiesMap().get(US.toString());
    assertEquals(EN_US_TITLE, ep.getProperty(TITLE));
    assertEquals(EN_US_DESCRIPTION, ep.getProperty(DESCRIPTION));
    Properties sp = updatedFile.getLocalePropertiesMap().get(SPANISH.toString());
    assertEquals(SP_TITLE, sp.getProperty(TITLE));
    assertEquals(SP_DESCRIPTION, sp.getProperty(DESCRIPTION));
    // Assert empty rootLocale
    Properties rootLocale = updatedFile.getLocalePropertiesMap().get(DEFAULT_LOCALE);
    assertNotNull(rootLocale);
    final String NEW_TITLE = "new title";
    final String NEW_DESCRIPTION = "new description";
    // overwrite title
    enProperties.setProperty(TITLE, NEW_TITLE);
    // overwrite title
    enProperties.setProperty(DESCRIPTION, NEW_DESCRIPTION);
    txnTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            // assert available locales
            List<Locale> locales = repositoryFileDao.getAvailableLocalesForFile(updatedFile);
            // includes rootLocale
            assertEquals(3, locales.size());
            // assert correct locale properties
            Properties properties = repositoryFileDao.getLocalePropertiesForFile(updatedFile, "es");
            assertEquals(SP_TITLE, properties.getProperty(TITLE));
            assertEquals(SP_DESCRIPTION, properties.getProperty(DESCRIPTION));
            repositoryFileDao.setLocalePropertiesForFile(updatedFile, Locale.US.getLanguage(), enProperties);
        }
    });
    // Assert updated properties
    RepositoryFile updatedRepoFile = repo.getFile(file.getPath(), true);
    Properties updated_en = updatedRepoFile.getLocalePropertiesMap().get(US.toString());
    assertEquals(NEW_TITLE, updated_en.getProperty(TITLE));
    assertEquals(NEW_DESCRIPTION, updated_en.getProperty(DESCRIPTION));
    // test successful delete locale properties
    final RepositoryFile repoFile1 = updatedRepoFile.clone();
    txnTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            repositoryFileDao.deleteLocalePropertiesForFile(repoFile1, "es");
        }
    });
    // assert deleted locale
    updatedRepoFile = repo.getFile(file.getPath(), true);
    List<Locale> locales = repositoryFileDao.getAvailableLocalesForFile(updatedRepoFile);
    assertEquals(2, locales.size());
    // test successful delete locale properties
    final RepositoryFile repoFile2 = updatedRepoFile.clone();
    txnTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            repositoryFileDao.deleteLocalePropertiesForFile(repoFile2, "xx");
        }
    });
    // locale properties do not exist, no change in available locales
    updatedRepoFile = repo.getFile(file.getPath(), true);
    locales = repositoryFileDao.getAvailableLocalesForFile(updatedRepoFile);
    assertEquals(2, locales.size());
    logout();
}
Also used : Locale(java.util.Locale) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) HashMap(java.util.HashMap) TransactionStatus(org.springframework.transaction.TransactionStatus) Matchers.anyString(org.mockito.Matchers.anyString) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) Properties(java.util.Properties) SampleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.sample.SampleRepositoryFileData) ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) List(java.util.List) ArrayList(java.util.ArrayList) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 2 with IPentahoLocale

use of org.pentaho.platform.api.locale.IPentahoLocale in project pentaho-platform by pentaho.

the class DefaultUnifiedRepositoryContentIT method testLocales.

@Test
public void testLocales() throws Exception {
    loginAsSysTenantAdmin();
    ITenant tenantAcme = tenantManager.createTenant(systemTenant, TENANT_ID_ACME, tenantAdminRoleName, tenantAuthenticatedRoleName, ANONYMOUS_ROLE_NAME);
    userRoleDao.createUser(tenantAcme, USERNAME_SUZY, PASSWORD, "", new String[] { tenantAdminRoleName });
    logout();
    login(USERNAME_SUZY, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
    // Create file
    final String fileName = "locale.sample";
    RepositoryFile file = createSampleFile(ClientRepositoryPaths.getUserHomeFolderPath(USERNAME_SUZY), fileName, "test", false, 123);
    // Test filename title matches created file name
    assertEquals(fileName, file.getTitle());
    final IPentahoLocale SPANISH = new PentahoLocale(new Locale("es"));
    final IPentahoLocale US = new PentahoLocale(Locale.US);
    final String EN_US_TITLE = "Locale Sample";
    final String EN_US_DESCRIPTION = "This is a test for retrieving localized words";
    final String SP_TITLE = "Muestra de Localizacion";
    final String SP_DESCRIPTION = "Esta es una prueba para buscar palabras localizadas";
    RepositoryFile.Builder builder = new RepositoryFile.Builder(file);
    // Set English locale values
    builder.title(US.toString(), EN_US_TITLE);
    builder.description(US.toString(), EN_US_DESCRIPTION);
    // Set Spanish locale values
    builder.title(SPANISH.toString(), SP_TITLE);
    builder.description(SPANISH.toString(), SP_DESCRIPTION);
    // Update file data
    final SampleRepositoryFileData modContent = new SampleRepositoryFileData("blah", false, 123);
    repo.updateFile(builder.build(), modContent, null);
    // Retrieve file - gets full map
    RepositoryFile updatedFile = repo.getFile(file.getPath(), true);
    assertNotNull(updatedFile.getLocalePropertiesMap());
    assertEquals(3, updatedFile.getLocalePropertiesMap().size());
    /*
     * Retrieve single result with locale
     */
    // SPANISH
    updatedFile = repo.getFile(file.getPath(), SPANISH);
    assertEquals(SP_TITLE, updatedFile.getTitle());
    assertEquals(SP_DESCRIPTION, updatedFile.getDescription());
    // US ENGLISH
    updatedFile = repo.getFile(file.getPath(), US);
    assertEquals(EN_US_TITLE, updatedFile.getTitle());
    assertEquals(EN_US_DESCRIPTION, updatedFile.getDescription());
    // ROOT Locale
    LocaleHelper.setLocale(US.getLocale());
    updatedFile = repo.getFile(file.getPath(), null);
    assertEquals(EN_US_TITLE, updatedFile.getTitle());
    assertEquals(EN_US_DESCRIPTION, updatedFile.getDescription());
    logout();
}
Also used : Locale(java.util.Locale) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) SampleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.sample.SampleRepositoryFileData) ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Matchers.anyString(org.mockito.Matchers.anyString) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) Test(org.junit.Test)

Example 3 with IPentahoLocale

use of org.pentaho.platform.api.locale.IPentahoLocale in project pentaho-platform by pentaho.

the class JcrRepositoryFileUtils method getLocalizedString.

public static String getLocalizedString(final Session session, final PentahoJcrConstants pentahoJcrConstants, final Node localizedStringNode, IPentahoLocale pentahoLocale) throws RepositoryException {
    Assert.isTrue(isLocalizedString(session, pentahoJcrConstants, localizedStringNode));
    boolean isLocaleNull = pentahoLocale == null;
    if (pentahoLocale == null) {
        pentahoLocale = new PentahoLocale();
    }
    Locale locale = pentahoLocale.getLocale();
    // $NON-NLS-1$
    final String UNDERSCORE = "_";
    // $NON-NLS-1$
    final String COLON = ":";
    boolean hasLanguage = StringUtils.hasText(locale.getLanguage());
    boolean hasCountry = StringUtils.hasText(locale.getCountry());
    boolean hasVariant = StringUtils.hasText(locale.getVariant());
    List<String> candidatePropertyNames = new ArrayList<String>(3);
    if (hasVariant) {
        candidatePropertyNames.add(locale.getLanguage() + UNDERSCORE + locale.getCountry() + UNDERSCORE + locale.getVariant());
    }
    if (hasCountry) {
        candidatePropertyNames.add(locale.getLanguage() + UNDERSCORE + locale.getCountry());
    }
    if (hasLanguage) {
        candidatePropertyNames.add(locale.getLanguage());
    }
    for (String propertyName : candidatePropertyNames) {
        if (localizedStringNode.hasProperty(propertyName)) {
            return localizedStringNode.getProperty(propertyName).getString();
        }
    }
    String prefix = session.getNamespacePrefix(PentahoJcrConstants.PHO_NS);
    Assert.hasText(prefix);
    String propertyStr = isLocaleNull ? pentahoJcrConstants.getPHO_ROOTLOCALE() : prefix + COLON + locale.getLanguage();
    return localizedStringNode.getProperty(propertyStr).getString();
}
Also used : Locale(java.util.Locale) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) ArrayList(java.util.ArrayList) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale)

Example 4 with IPentahoLocale

use of org.pentaho.platform.api.locale.IPentahoLocale in project pentaho-platform by pentaho.

the class JcrRepositoryFileUtils method nodeToFileOld.

public static RepositoryFile nodeToFileOld(final Session session, final PentahoJcrConstants pentahoJcrConstants, final IPathConversionHelper pathConversionHelper, final ILockHelper lockHelper, final Node node, final boolean loadMaps, IPentahoLocale pentahoLocale) throws RepositoryException {
    if (session.getRootNode().isSame(node)) {
        return getRootFolder(session);
    }
    Serializable id = null;
    String name = null;
    String path = null;
    long fileSize = 0;
    Date created = null;
    String creatorId = null;
    Boolean hidden = RepositoryFile.HIDDEN_BY_DEFAULT;
    Boolean schedulable = RepositoryFile.SCHEDULABLE_BY_DEFAULT;
    Date lastModified = null;
    boolean folder = false;
    boolean versioned = false;
    Serializable versionId = null;
    boolean locked = false;
    String lockOwner = null;
    Date lockDate = null;
    String lockMessage = null;
    String title = null;
    String description = null;
    Boolean aclNode = false;
    Map<String, Properties> localePropertiesMap = null;
    id = getNodeId(session, pentahoJcrConstants, node);
    if (logger.isDebugEnabled()) {
        // $NON-NLS-1$
        logger.debug(String.format("reading file with id '%s' and path '%s'", id, node.getPath()));
    }
    path = pathConversionHelper.absToRel((getAbsolutePath(session, pentahoJcrConstants, node)));
    // if the rel path is / then name the folder empty string instead of its true name (this hides the tenant name)
    // $NON-NLS-1$
    name = RepositoryFile.SEPARATOR.equals(path) ? "" : getNodeName(session, pentahoJcrConstants, node);
    if (isPentahoFolder(pentahoJcrConstants, node)) {
        folder = true;
    }
    // jcr:created nodes have OnParentVersion values of INITIALIZE
    if (node.hasProperty(pentahoJcrConstants.getJCR_CREATED())) {
        Calendar tmpCal = node.getProperty(pentahoJcrConstants.getJCR_CREATED()).getDate();
        if (tmpCal != null) {
            created = tmpCal.getTime();
        }
    }
    // Expensive
    Map<String, Serializable> metadata = getFileMetadata(session, id);
    if (metadata != null) {
        creatorId = (String) metadata.get(PentahoJcrConstants.PHO_CONTENTCREATOR);
        Serializable schedulableValue = metadata.get(RepositoryFile.SCHEDULABLE_KEY);
        if (schedulableValue instanceof String) {
            schedulable = BooleanUtils.toBoolean((String) schedulableValue);
        }
    }
    if (node.hasProperty(pentahoJcrConstants.getPHO_HIDDEN())) {
        hidden = node.getProperty(pentahoJcrConstants.getPHO_HIDDEN()).getBoolean();
    }
    if (node.hasProperty(pentahoJcrConstants.getPHO_FILESIZE())) {
        fileSize = node.getProperty(pentahoJcrConstants.getPHO_FILESIZE()).getLong();
    }
    if (node.hasProperty(pentahoJcrConstants.getPHO_ACLNODE())) {
        aclNode = node.getProperty(pentahoJcrConstants.getPHO_ACLNODE()).getBoolean();
    }
    if (isPentahoFile(pentahoJcrConstants, node)) {
        // pho:lastModified nodes have OnParentVersion values of IGNORE; i.e. they don't exist in frozen nodes
        if (!node.isNodeType(pentahoJcrConstants.getNT_FROZENNODE())) {
            Calendar tmpCal = node.getProperty(pentahoJcrConstants.getPHO_LASTMODIFIED()).getDate();
            if (tmpCal != null) {
                lastModified = tmpCal.getTime();
            }
        }
    }
    // Get default locale if null
    if (pentahoLocale == null) {
        Locale currentLocale = LocaleHelper.getLocale();
        if (currentLocale != null) {
            pentahoLocale = new PentahoLocale(currentLocale);
        } else {
            pentahoLocale = new PentahoLocale();
        }
    }
    // Not needed for content generators and the like
    if (isPentahoHierarchyNode(session, pentahoJcrConstants, node)) {
        if (node.hasNode(pentahoJcrConstants.getPHO_LOCALES())) {
            // Expensive
            localePropertiesMap = getLocalePropertiesMap(session, pentahoJcrConstants, node.getNode(pentahoJcrConstants.getPHO_LOCALES()));
            // [BISERVER-8337] localize title and description
            LocalePropertyResolver lpr = new LocalePropertyResolver(name);
            LocalizationUtil localizationUtil = new LocalizationUtil(localePropertiesMap, pentahoLocale.getLocale());
            title = localizationUtil.resolveLocalizedString(lpr.resolveDefaultTitleKey(), null);
            if (org.apache.commons.lang.StringUtils.isBlank(title)) {
                title = localizationUtil.resolveLocalizedString(lpr.resolveTitleKey(), null);
                if (org.apache.commons.lang.StringUtils.isBlank(title)) {
                    title = localizationUtil.resolveLocalizedString(lpr.resolveNameKey(), title);
                }
            }
            description = localizationUtil.resolveLocalizedString(lpr.resolveDefaultDescriptionKey(), null);
            if (org.apache.commons.lang.StringUtils.isBlank(description)) {
                description = localizationUtil.resolveLocalizedString(lpr.resolveDescriptionKey(), description);
            }
        }
        // found
        if (title == null && node.hasNode(pentahoJcrConstants.getPHO_TITLE())) {
            title = getLocalizedString(session, pentahoJcrConstants, node.getNode(pentahoJcrConstants.getPHO_TITLE()), pentahoLocale);
        }
        if (description == null && node.hasNode(pentahoJcrConstants.getPHO_DESCRIPTION())) {
            description = getLocalizedString(session, pentahoJcrConstants, node.getNode(pentahoJcrConstants.getPHO_DESCRIPTION()), pentahoLocale);
        }
    }
    if (!loadMaps) {
        // remove reference, allow garbage collection
        localePropertiesMap = null;
    }
    versioned = isVersioned(session, pentahoJcrConstants, node);
    if (versioned) {
        versionId = getVersionId(session, pentahoJcrConstants, node);
    }
    locked = isLocked(pentahoJcrConstants, node);
    if (locked) {
        Lock lock = session.getWorkspace().getLockManager().getLock(node.getPath());
        lockOwner = lockHelper.getLockOwner(session, pentahoJcrConstants, lock);
        lockDate = lockHelper.getLockDate(session, pentahoJcrConstants, lock);
        lockMessage = lockHelper.getLockMessage(session, pentahoJcrConstants, lock);
    }
    RepositoryFile file = new RepositoryFile.Builder(id, name).createdDate(created).creatorId(creatorId).lastModificationDate(lastModified).folder(folder).versioned(versioned).path(path).versionId(versionId).fileSize(fileSize).locked(locked).lockDate(lockDate).hidden(hidden).schedulable(schedulable).lockMessage(lockMessage).lockOwner(lockOwner).title(title).description(description).locale(pentahoLocale.toString()).localePropertiesMap(localePropertiesMap).aclNode(aclNode).build();
    return file;
}
Also used : Locale(java.util.Locale) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) Serializable(java.io.Serializable) Calendar(java.util.Calendar) Properties(java.util.Properties) PentahoLocale(org.pentaho.platform.repository2.locale.PentahoLocale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) Date(java.util.Date) Lock(javax.jcr.lock.Lock) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean)

Aggregations

Locale (java.util.Locale)4 IPentahoLocale (org.pentaho.platform.api.locale.IPentahoLocale)4 PentahoLocale (org.pentaho.platform.repository2.locale.PentahoLocale)4 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)3 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ITenant (org.pentaho.platform.api.mt.ITenant)2 SampleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.sample.SampleRepositoryFileData)2 Serializable (java.io.Serializable)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Lock (javax.jcr.lock.Lock)1 MutableBoolean (org.apache.commons.lang.mutable.MutableBoolean)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1