Woonsan Ko pushed to branch feature/HSTTWO-3623 at cms-community / 
hippo-site-toolkit


Commits:
ba03ce4e by Woonsan Ko at 2016-03-21T15:34:45-04:00
HSTTWO-3623: adding skeletal CachingHippoTranslationBeanServiceImpl

- - - - -


2 changed files:

- + 
content-beans/src/main/java/org/hippoecm/hst/content/service/CachingHippoTranslationBeanServiceImpl.java
- 
content-beans/src/main/java/org/hippoecm/hst/content/service/DefaultHippoTranslationBeanServiceImpl.java


Changes:

=====================================
content-beans/src/main/java/org/hippoecm/hst/content/service/CachingHippoTranslationBeanServiceImpl.java
=====================================
--- /dev/null
+++ 
b/content-beans/src/main/java/org/hippoecm/hst/content/service/CachingHippoTranslationBeanServiceImpl.java
@@ -0,0 +1,89 @@
+/**
+ * Copyright 2016-2016 Hippo B.V. (http://www.onehippo.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.hippoecm.hst.content.service;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.commons.lang.StringUtils;
+import org.hippoecm.hst.cache.CacheElement;
+import org.hippoecm.hst.cache.HstCache;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Default Hippo Translation Content Bean service implementation by executing 
queries simply to find all the
+ * Hippo Translation beans.
+ */
+public class CachingHippoTranslationBeanServiceImpl extends 
DefaultHippoTranslationBeanServiceImpl {
+
+    private static Logger log = 
LoggerFactory.getLogger(CachingHippoTranslationBeanServiceImpl.class);
+
+    private final HstCache handleIdsOfTranslationIdCache;
+
+    public CachingHippoTranslationBeanServiceImpl(final HstCache 
handleIdsOfTranslationIdCache) {
+        this.handleIdsOfTranslationIdCache = handleIdsOfTranslationIdCache;
+    }
+
+    @Override
+    public List<Node> getTranslationNodes(final Session session, final String 
translationId)
+            throws RepositoryException {
+        if (StringUtils.isBlank(translationId)) {
+            throw new IllegalArgumentException("Blank translation ID.");
+        }
+
+        List<Node> translationNodes = null;
+
+        CacheElement cacheElem = 
handleIdsOfTranslationIdCache.get(translationId);
+
+        if (cacheElem != null) {
+            List<String> handleNodeIds = (List<String>) cacheElem.getContent();
+
+            if (handleNodeIds != null) {
+                translationNodes = new ArrayList<>();
+                Node handleNode;
+
+                for (String handleNodeId : handleNodeIds) {
+                    handleNode = session.getNodeByIdentifier(handleNodeId);
+
+                    if (handleNode.hasNode(handleNode.getName())) {
+                        
translationNodes.add(handleNode.getNode(handleNode.getName()));
+                    }
+                }
+            }
+        } else {
+            translationNodes = super.getTranslationNodes(session, 
translationId);
+
+            List<String> handleNodeIds = new ArrayList<String>();
+            Node handleNode;
+            for (Node translationNode : translationNodes) {
+                handleNode = translationNode.getParent();
+                handleNodeIds.add(handleNode.getIdentifier());
+            }
+
+            cacheElem = 
handleIdsOfTranslationIdCache.createElement(translationId, handleNodeIds);
+            handleIdsOfTranslationIdCache.put(cacheElem);
+        }
+
+        return translationNodes == null ? Collections.emptyList() : 
translationNodes;
+    }
+
+}


=====================================
content-beans/src/main/java/org/hippoecm/hst/content/service/DefaultHippoTranslationBeanServiceImpl.java
=====================================
--- 
a/content-beans/src/main/java/org/hippoecm/hst/content/service/DefaultHippoTranslationBeanServiceImpl.java
+++ 
b/content-beans/src/main/java/org/hippoecm/hst/content/service/DefaultHippoTranslationBeanServiceImpl.java
@@ -15,8 +15,8 @@
  */
 package org.hippoecm.hst.content.service;
 
+import java.util.ArrayList;
 import java.util.LinkedHashMap;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
@@ -45,6 +45,9 @@ public class DefaultHippoTranslationBeanServiceImpl 
implements HippoTranslationB
 
     private static Logger log = 
LoggerFactory.getLogger(DefaultHippoTranslationBeanServiceImpl.class);
 
+    public DefaultHippoTranslationBeanServiceImpl() {
+    }
+
     @Override
     public <T extends HippoBean> Map<String, T> getTranslationBeans(Session 
session, String translationId,
             Class<T> beanMappingClass) throws RepositoryException {
@@ -87,7 +90,7 @@ public class DefaultHippoTranslationBeanServiceImpl 
implements HippoTranslationB
             throw new IllegalArgumentException("Blank translation ID.");
         }
 
-        List<Node> translationNodes = new LinkedList<>();
+        List<Node> translationNodes = new ArrayList<>();
 
         String xpath = "//element(*," + HippoTranslationNodeType.NT_TRANSLATED 
+ ")[" + HippoTranslationNodeType.ID
                 + " = '" + translationId + "']";



View it on GitLab: 
https://code.onehippo.org/cms-community/hippo-site-toolkit/commit/ba03ce4eeb1ae54841157d57b432104c7320ba6d
_______________________________________________
Hippocms-svn mailing list
Hippocms-svn@lists.onehippo.org
https://lists.onehippo.org/mailman/listinfo/hippocms-svn

Reply via email to