use of org.pentaho.platform.repository2.locale.PentahoLocale in project pentaho-platform by pentaho.
the class FileServiceTest method testDoGetFileLocales.
@Test
public void testDoGetFileLocales() {
String param = "file1";
RepositoryFileDto repositoryFileDto = mock(RepositoryFileDto.class);
List<PentahoLocale> locales = new ArrayList<PentahoLocale>();
PentahoLocale mockedLocale = mock(PentahoLocale.class);
locales.add(mockedLocale);
doReturn(param).when(repositoryFileDto).getId();
doReturn(repositoryFileDto).when(fileService.defaultUnifiedRepositoryWebService).getFile("/" + param);
when(fileService.defaultUnifiedRepositoryWebService.getAvailableLocalesForFileById(repositoryFileDto.getId())).thenReturn(locales);
try {
fileService.doGetFileLocales(param);
verify(fileService.getRepository(), times(0)).getAvailableLocalesForFileById(repositoryFileDto.getId());
} catch (FileNotFoundException e) {
fail();
} catch (InternalError e) {
fail();
}
}
use of org.pentaho.platform.repository2.locale.PentahoLocale in project pentaho-platform by pentaho.
the class FileService method doGetFileLocales.
/**
* Retrieves the list of locale map for the selected repository file. The list will be empty if a problem occurs.
*
* @param pathId colon separated path for the repository file
* <pre function="syntax.xml">
* :path:to:file:id
* </pre>
* @return <code>List<LocaleMapDto></code> the list of locales
* <pre function="syntax.xml">
* <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
* <localePropertiesMapEntries>
* <localeMapDto>
* <locale>default</locale>
* <properties>
* <stringKeyStringValueDto>
* <key>file.title</key>
* <value>myFile</value>
* </stringKeyStringValueDto>
* <stringKeyStringValueDto>
* <key>jcr:primaryType</key>
* <value>nt:unstructured</value>
* </stringKeyStringValueDto>
* <stringKeyStringValueDto>
* <key>title</key>
* <value>myFile</value>
* </stringKeyStringValueDto>
* <stringKeyStringValueDto>
* <key>file.description</key>
* <value>myFile Description</value>
* </stringKeyStringValueDto>
* </properties>
* </localeMapDto>
* </localePropertiesMapEntries>
* </pre>
* @throws FileNotFoundException
*/
public List<LocaleMapDto> doGetFileLocales(String pathId) throws FileNotFoundException {
List<LocaleMapDto> availableLocales = new ArrayList<LocaleMapDto>();
RepositoryFileDto file = getRepoWs().getFile(idToPath(pathId));
if (file == null) {
throw new FileNotFoundException();
}
try {
List<PentahoLocale> locales = getRepoWs().getAvailableLocalesForFileById(file.getId());
if (locales != null && !locales.isEmpty()) {
for (PentahoLocale locale : locales) {
availableLocales.add(new LocaleMapDto(locale.toString(), null));
}
}
} catch (Exception e) {
throw new InternalError();
}
return availableLocales;
}
use of org.pentaho.platform.repository2.locale.PentahoLocale 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();
}
use of org.pentaho.platform.repository2.locale.PentahoLocale 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();
}
use of org.pentaho.platform.repository2.locale.PentahoLocale 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();
}
Aggregations