ISIS-903: allow the translations cache to be cleared (in prototype mode).

Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/a2aaf098
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/a2aaf098
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/a2aaf098

Branch: refs/heads/master
Commit: a2aaf098b9b9b0d08d9030594b8eeffe9344f8c0
Parents: 107cf3d
Author: Dan Haywood <[email protected]>
Authored: Wed Feb 18 14:01:34 2015 +0000
Committer: Dan Haywood <[email protected]>
Committed: Wed Feb 18 14:07:59 2015 +0000

----------------------------------------------------------------------
 .../runtime/services/i18n/po/PoAbstract.java    |  4 +-
 .../core/runtime/services/i18n/po/PoReader.java | 18 ++++++++-
 .../core/runtime/services/i18n/po/PoWriter.java |  3 --
 .../services/i18n/po/TranslationServicePo.java  | 19 +++++++--
 .../i18n/po/TranslationServicePoMenu.java       | 42 +++++++++++++++-----
 5 files changed, 67 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/a2aaf098/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoAbstract.java
----------------------------------------------------------------------
diff --git 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoAbstract.java
 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoAbstract.java
index 15777cc..95286f1 100644
--- 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoAbstract.java
+++ 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoAbstract.java
@@ -23,6 +23,7 @@ import 
org.apache.isis.applib.services.i18n.TranslationService;
 abstract class PoAbstract {
 
     protected final TranslationServicePo translationServicePo;
+
     private final TranslationService.Mode mode;
 
     PoAbstract(final TranslationServicePo translationServicePo, final 
TranslationService.Mode mode) {
@@ -37,7 +38,8 @@ abstract class PoAbstract {
 
     abstract String translate(final String context, final String msgId, final 
String msgIdPlural, int num);
 
-    public TranslationService.Mode getMode() {
+    TranslationService.Mode getMode() {
         return mode;
     }
+
 }

http://git-wip-us.apache.org/repos/asf/isis/blob/a2aaf098/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoReader.java
----------------------------------------------------------------------
diff --git 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoReader.java
 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoReader.java
index 6eb2c3f..435bb4e 100644
--- 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoReader.java
+++ 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoReader.java
@@ -42,7 +42,12 @@ class PoReader extends PoAbstract {
      *
      * <p>
      *     This means that the reader will search for 
<tt>translations_en-US.po</tt>, <tt>translations_en.po</tt>,
-     *     <tt>translations.po</tt>, according to the location that the 
provided {@link org.apache.isis.applib.services.i18n.TranslationsResolver} 
searches.  For example, if using the Wicket implementation, then will search 
for these files
+     *     <tt>translations.po</tt>, according to the location that the 
provided
+     *     {@link org.apache.isis.applib.services.i18n.TranslationsResolver} 
searches.
+     * </p>
+     *
+     * <p>
+     *     For example, if using the Wicket implementation, then will search 
for these files
      *     under <tt>/WEB-INF</tt> directory.
      * </p>
      */
@@ -55,7 +60,11 @@ class PoReader extends PoAbstract {
     }
 
     //region > init, shutdown
-    void init(final Map<String,String> config) {
+
+    /**
+     * Not API
+     */
+    void init() {
         fallback = readUrl(basename + ".po");
         if(fallback == null) {
             LOG.warn("No fallback translations found");
@@ -88,6 +97,11 @@ class PoReader extends PoAbstract {
         return translate(context, msgIdToUse, type);
     }
 
+    void clearCache() {
+        translationByKeyByLocale.clear();
+        init();
+    }
+
     private String translate(
             final String context, final String msgId, final 
ContextAndMsgId.Type type) {
 

http://git-wip-us.apache.org/repos/asf/isis/blob/a2aaf098/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoWriter.java
----------------------------------------------------------------------
diff --git 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoWriter.java
 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoWriter.java
index 5b850d6..d9962bd 100644
--- 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoWriter.java
+++ 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/PoWriter.java
@@ -115,9 +115,6 @@ class PoWriter extends PoAbstract {
         return block;
     }
 
-    /**
-     * Not API
-     */
     String toPot() {
         final StringBuilder buf = new StringBuilder();
         for (final String msgId : blocksByMsgId.keySet()) {

http://git-wip-us.apache.org/repos/asf/isis/blob/a2aaf098/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/TranslationServicePo.java
----------------------------------------------------------------------
diff --git 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/TranslationServicePo.java
 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/TranslationServicePo.java
index 3d92184..5ec4ccf 100644
--- 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/TranslationServicePo.java
+++ 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/TranslationServicePo.java
@@ -78,7 +78,7 @@ public class TranslationServicePo implements 
TranslationService {
 
         // switch to read mode
         final PoReader poReader = new PoReader(this);
-        poReader.init(config);
+        poReader.init();
         po = poReader;
     }
 
@@ -115,13 +115,25 @@ public class TranslationServicePo implements 
TranslationService {
      * Not API
      */
     @Programmatic
-    public String toPo() {
+    public String toPot() {
         if (!getMode().isWrite()) {
-            throw new IllegalStateException("Not in write mode");
+            return null;
         }
         return  ((PoWriter)po).toPot();
     }
 
+
+    /**
+     * Not API
+     */
+    @Programmatic
+    void clearCache() {
+        if (!getMode().isRead()) {
+            return;
+        }
+        ((PoReader)po).clearCache();
+    }
+
     // //////////////////////////////////////
 
     DeploymentType getDeploymentType() {
@@ -147,5 +159,4 @@ public class TranslationServicePo implements 
TranslationService {
         return localeProvider;
     }
 
-
 }

http://git-wip-us.apache.org/repos/asf/isis/blob/a2aaf098/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/TranslationServicePoMenu.java
----------------------------------------------------------------------
diff --git 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/TranslationServicePoMenu.java
 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/TranslationServicePoMenu.java
index e349668..9d5a236 100644
--- 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/TranslationServicePoMenu.java
+++ 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/i18n/po/TranslationServicePoMenu.java
@@ -26,18 +26,17 @@ import org.apache.isis.applib.annotation.Action;
 import org.apache.isis.applib.annotation.ActionLayout;
 import org.apache.isis.applib.annotation.DomainService;
 import org.apache.isis.applib.annotation.DomainServiceLayout;
-import org.apache.isis.applib.annotation.NatureOfService;
+import org.apache.isis.applib.annotation.MemberOrder;
 import org.apache.isis.applib.annotation.ParameterLayout;
 import org.apache.isis.applib.annotation.RestrictTo;
 import org.apache.isis.applib.annotation.SemanticsOf;
 import org.apache.isis.applib.value.Clob;
 
-@DomainService(
-        nature = NatureOfService.VIEW_MENU_ONLY
-)
+@DomainService()
 @DomainServiceLayout(
+        named = "Prototyping",
         menuBar = DomainServiceLayout.MenuBar.SECONDARY,
-        named = "Prototyping"
+        menuOrder = "500"
 )
 public class TranslationServicePoMenu {
 
@@ -71,22 +70,47 @@ public class TranslationServicePoMenu {
     @ActionLayout(
             cssClassFa = "fa-download"
     )
-    public Clob downloadPotFile(
+    @MemberOrder(sequence="500.10")
+    public Clob downloadTranslations(
             @ParameterLayout(named = ".pot file name")
             final String potFileName) {
-        final String chars = translationService.toPo();
+        final String chars = translationService.toPot();
         return new Clob(potFileName, "text/plain", chars);
     }
 
-    public String default0DownloadPotFile() {
+    public String default0DownloadTranslations() {
         return "translations.pot";
     }
-    public boolean hideDownloadPotFile() {
+    public boolean hideDownloadTranslations() {
         return translationService.getMode().isRead();
     }
 
     // //////////////////////////////////////
 
+    public static class ResetTranslationCacheDomainEvent extends 
ActionDomainEvent {
+        public ResetTranslationCacheDomainEvent(final TranslationServicePoMenu 
source, final Identifier identifier, final Object... arguments) {
+            super(source, identifier, arguments);
+        }
+    }
+
+    @Action(
+            domainEvent = ResetTranslationCacheDomainEvent.class,
+            semantics = SemanticsOf.SAFE,
+            restrictTo = RestrictTo.PROTOTYPING
+    )
+    @ActionLayout(
+            named="Clear translation cache",
+            cssClassFa = "fa-trash"
+    )
+    @MemberOrder(sequence="500.20")
+    public void resetTranslationCache() {
+        translationService.clearCache();
+    }
+    public boolean hideResetTranslationCache() {
+        return translationService.getMode().isWrite();
+    }
+
+    // //////////////////////////////////////
 
     @Inject
     private TranslationServicePo translationService;

Reply via email to