Author: sascharodekamp
Date: Fri Dec 9 13:26:46 2011 New Revision: 1212400 URL: http://svn.apache.org/viewvc?rev=1212400&view=rev Log: A bug fix in the language selection, a few new tests and some code cleanup. Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/JcrRepositoryAccessor.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/JackrabbitRepositoryAccessor.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/jackrabbit/JackrabbitArticleHelper.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/jackrabbit/JackrabbitFileHelper.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JackrabbitTests.java ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/jackrabbit/JcrUtilJackrabbit.java Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/JcrRepositoryAccessor.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/JcrRepositoryAccessor.java?rev=1212400&r1=1212399&r2=1212400&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/JcrRepositoryAccessor.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/JcrRepositoryAccessor.java Fri Dec 9 13:26:46 2011 @@ -129,4 +129,13 @@ public interface JcrRepositoryAccessor { * @return */ public Session getSession(); + + /** + * Returns true if the node with the passed node path exist, if not or if an + * exception occurs false will be returned. + * + * @param nodePathToCheck + * @return + */ + public boolean checkIfNodeExist(String nodePathToCheck); } Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/JackrabbitRepositoryAccessor.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/JackrabbitRepositoryAccessor.java?rev=1212400&r1=1212399&r2=1212400&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/JackrabbitRepositoryAccessor.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/access/jackrabbit/JackrabbitRepositoryAccessor.java Fri Dec 9 13:26:46 2011 @@ -51,7 +51,7 @@ public class JackrabbitRepositoryAccesso this.session = session; - this.ocm = new ObjectContentManagerImpl(session, JCRFactoryImpl.getMapper()); + this.ocm = new ObjectContentManagerImpl(session, JCRFactoryImpl.getMapper()); return; } @@ -217,11 +217,31 @@ public class JackrabbitRepositoryAccesso /* * (non-Javadoc) - * @see org.ofbiz.jcr.access.RepositoryAccess#queryForRepositoryData(java.lang.String) + * + * @see + * org.ofbiz.jcr.access.RepositoryAccess#queryForRepositoryData(java.lang + * .String) */ @Override - public QueryResult queryForRepositoryData(String query) throws RepositoryException{ + public QueryResult queryForRepositoryData(String query) throws RepositoryException { ContentReader contentReader = new ContentReaderJackrabbit(this.ocm); return contentReader.queryRepositoryData(query); } + + /* + * (non-Javadoc) + * + * @see + * org.ofbiz.jcr.access.JcrRepositoryAccessor#checkIfNodeExist(java.lang + * .String) + */ + @Override + public boolean checkIfNodeExist(String nodePathToCheck) { + try { + return getSession().itemExists(nodePathToCheck); + } catch (RepositoryException e) { + Debug.logError(e, module); + return false; + } + } } Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/jackrabbit/JackrabbitArticleHelper.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/jackrabbit/JackrabbitArticleHelper.java?rev=1212400&r1=1212399&r2=1212400&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/jackrabbit/JackrabbitArticleHelper.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/jackrabbit/JackrabbitArticleHelper.java Fri Dec 9 13:26:46 2011 @@ -15,7 +15,6 @@ import javax.jcr.ValueFormatException; import org.apache.jackrabbit.ocm.exception.ObjectContentManagerException; import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.UtilProperties; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.GenericValue; import org.ofbiz.jcr.access.jackrabbit.ConstantsJackrabbit; @@ -23,6 +22,7 @@ import org.ofbiz.jcr.access.jackrabbit.J import org.ofbiz.jcr.api.JcrDataHelper; import org.ofbiz.jcr.orm.OfbizRepositoryMapping; import org.ofbiz.jcr.orm.jackrabbit.JackrabbitArticle; +import org.ofbiz.jcr.util.jackrabbit.JcrUtilJackrabbit; /** * This Helper class encapsulate the jcr article content bean. it provide all @@ -127,7 +127,7 @@ public class JackrabbitArticleHelper ext @Override public void storeContentInRepository(String contentPath, String language, String title, String content, Calendar publicationDate) throws ObjectContentManagerException, ItemExistsException { if (UtilValidate.isEmpty(language)) { - language = determindeTheDefaultLanguage(); + language = JcrUtilJackrabbit.determindeTheDefaultLanguage(); } // construct the content article object @@ -146,7 +146,7 @@ public class JackrabbitArticleHelper ext @Override public void updateContentInRepository(JackrabbitArticle updatedArticle) throws RepositoryException, ObjectContentManagerException { // if the item not already exist create it. - if (!super.access.getSession().itemExists(updatedArticle.getPath())) { + if (!super.access.checkIfNodeExist(updatedArticle.getPath())) { Debug.logWarning("This content object with the path: " + updatedArticle.getPath() + " doesn't exist in the repository. It will now created.", module); this.storeContentInRepository(updatedArticle.getPath(), updatedArticle.getLanguage(), updatedArticle.getTitle(), updatedArticle.getContent(), updatedArticle.getPubDate()); return; @@ -231,38 +231,56 @@ public class JackrabbitArticleHelper ext return contentPath; } - String canonicalizedContentPath = canonicalizeContentPath(contentPath); + // contentLanaguage should never be null, because the concatenation + // looks really bad if the String have a null value. + if (contentLanguage == null) { + contentLanguage = ""; + } + + String canonicalizedContentPath = canonicalizeContentPath(contentPath, contentLanguage); + + // Step 1.) Check if the requested node language combination exist and + // if it have a valid localize flag + // Step 2.) If the first condition is not true, check the combination + // from node path and default language have a valid node result + // Step 3.) If condition one and two are false, determine the first node + // which have a valid language flag + + if (super.access.checkIfNodeExist(canonicalizedContentPath + contentLanguage) && checkIfNodeHaveValidLanguageMixIn(canonicalizedContentPath + contentLanguage)) { + contentPath = canonicalizedContentPath + contentLanguage; + } else if (super.access.checkIfNodeExist(canonicalizedContentPath + JcrUtilJackrabbit.determindeTheDefaultLanguage())) { + contentPath = canonicalizedContentPath + JcrUtilJackrabbit.determindeTheDefaultLanguage(); + } else { + contentPath = determineFirstAvailableLanguageNode(canonicalizedContentPath); + } + + return contentPath; + } + + /** + * Iterate over all child nodes and returns the first with a valid language + * flag. + * + * @param canonicalizedContentPath + * @return + */ + private String determineFirstAvailableLanguageNode(String canonicalizedContentPath) { + String contentPath = ""; - // check if this language already exist in the repository - Session session = super.access.getSession(); try { - // check if the node exist OR if the node has NO localized flag OR - // the localized flag is set to false - if (!session.itemExists(canonicalizedContentPath + contentLanguage) - || (!session.getNode(canonicalizedContentPath + contentLanguage).hasProperty("localized") - || !session.getNode(canonicalizedContentPath + contentLanguage).getProperty("localized").getBoolean())) { - // check for default language - if (!session.itemExists(canonicalizedContentPath + determindeTheDefaultLanguage())) { - // return the first available language - NodeIterator childNodes = session.getNode(canonicalizedContentPath).getNodes(); - while (childNodes.hasNext()) { - Node child = childNodes.nextNode(); - if (possibleLocales.contains(child.getName())) { - contentPath = child.getPath(); - break; - } - } - childNodes = null; - } else { - contentPath = canonicalizedContentPath + determindeTheDefaultLanguage(); + // return the first available language + NodeIterator childNodes = super.access.getSession().getNode(canonicalizedContentPath).getNodes(); + while (childNodes.hasNext()) { + Node child = childNodes.nextNode(); + if (possibleLocales.contains(child.getName()) && checkIfNodeHaveValidLanguageMixIn(child)) { + contentPath = child.getPath(); + break; } - } else { - contentPath = canonicalizedContentPath + contentLanguage; } + childNodes = null; } catch (RepositoryException e) { Debug.logError(e, module); } - return contentPath; } @@ -273,14 +291,18 @@ public class JackrabbitArticleHelper ext * @param contentPath * @return */ - private String canonicalizeContentPath(String contentPath) { + private String canonicalizeContentPath(String contentPath, String contentLanguage) { // we split the path string in chunks String[] splitContentPath = contentPath.split(ConstantsJackrabbit.NODEPATHDELIMITER); String canonicalizedCotnentPath = ""; // check if the last chunk contains a language which is part of our - // locale list - if (possibleLocales.contains(splitContentPath[splitContentPath.length - 1])) { + // locale list, but this should only be called if the language should be + // changed. Because it is possible to request a node directly with the + // language flag. That means if the node path contains a language and + // the language should not be changed (contentLanaguage is empty), the + // language flag should be stay in the content path. + if (UtilValidate.isNotEmpty(contentLanguage) && possibleLocales.contains(splitContentPath[splitContentPath.length - 1])) { // this local field should not be part of our path string canonicalizedCotnentPath = buildCanonicalizeContentPath(splitContentPath, splitContentPath.length - 1); } else { @@ -311,16 +333,7 @@ public class JackrabbitArticleHelper ext canonicalizedContentPath.append(splitContentPath[i]).append(ConstantsJackrabbit.NODEPATHDELIMITER); } } - return null; - } - - /** - * Return default language from property file. - * - * @return - */ - private String determindeTheDefaultLanguage() { - return UtilProperties.getPropertyValue("general", "locale.properties.fallback"); + return canonicalizedContentPath.toString(); } /** @@ -338,16 +351,34 @@ public class JackrabbitArticleHelper ext /** * Checks if a node have the property <code>localized</code> and if this + * property is be <code>true</code>. If an exception occurs false will be + * returned. + * + * @param nodePathWhichShouldHaveAValidLanguageFlag + * @return + */ + private boolean checkIfNodeHaveValidLanguageMixIn(String nodePathWhichShouldHaveAValidLanguageFlag) { + try { + Node node = super.access.getSession().getNode(nodePathWhichShouldHaveAValidLanguageFlag); + return checkIfNodeHaveValidLanguageMixIn(node); + } catch (RepositoryException e) { + Debug.logError(e, module); + return false; + } + } + + /** + * Checks if a node have the property <code>localized</code> and if this * property is be <code>true</code> * - * @param tmpNode + * @param nodeWhichShouldHaveAValidLanguageFlag * @return * @throws RepositoryException * @throws ValueFormatException * @throws PathNotFoundException */ - private boolean checkIfNodeHaveValidLanguageMixIn(Node tmpNode) throws RepositoryException, ValueFormatException, PathNotFoundException { - return tmpNode.hasProperty("localized") && tmpNode.getProperty("localized").getBoolean(); + private boolean checkIfNodeHaveValidLanguageMixIn(Node nodeWhichShouldHaveAValidLanguageFlag) throws RepositoryException, ValueFormatException, PathNotFoundException { + return nodeWhichShouldHaveAValidLanguageFlag.hasProperty("localized") && nodeWhichShouldHaveAValidLanguageFlag.getProperty("localized").getBoolean(); } /** Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/jackrabbit/JackrabbitFileHelper.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/jackrabbit/JackrabbitFileHelper.java?rev=1212400&r1=1212399&r2=1212400&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/jackrabbit/JackrabbitFileHelper.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/api/jackrabbit/JackrabbitFileHelper.java Fri Dec 9 13:26:46 2011 @@ -45,16 +45,24 @@ public class JackrabbitFileHelper extend super(new JackrabbitRepositoryAccessor(userLogin)); } - /* (non-Javadoc) - * @see org.ofbiz.jcr.api.jackrabbit.FileHelper#getRepositoryContent(java.lang.String) + /* + * (non-Javadoc) + * + * @see + * org.ofbiz.jcr.api.jackrabbit.FileHelper#getRepositoryContent(java.lang + * .String) */ @Override public JackrabbitHierarchyNode getRepositoryContent(String contentPath) throws ClassCastException { return getRepositoryContent(contentPath, null); } - /* (non-Javadoc) - * @see org.ofbiz.jcr.api.jackrabbit.FileHelper#getRepositoryContent(java.lang.String, java.lang.String) + /* + * (non-Javadoc) + * + * @see + * org.ofbiz.jcr.api.jackrabbit.FileHelper#getRepositoryContent(java.lang + * .String, java.lang.String) */ @Override public JackrabbitHierarchyNode getRepositoryContent(String contentPath, String version) throws ClassCastException { @@ -78,16 +86,24 @@ public class JackrabbitFileHelper extend throw new ClassCastException("The content object for the path: " + contentPath + " is not a file content object. This Helper can only handle content objects with the type: " + JackrabbitFile.class.getName()); } - /* (non-Javadoc) - * @see org.ofbiz.jcr.api.jackrabbit.FileHelper#storeContentInRepository(byte[], java.lang.String, java.lang.String) + /* + * (non-Javadoc) + * + * @see + * org.ofbiz.jcr.api.jackrabbit.FileHelper#storeContentInRepository(byte[], + * java.lang.String, java.lang.String) */ @Override public void storeContentInRepository(byte[] fileData, String fileName, String folderPath) throws ObjectContentManagerException, RepositoryException { storeContentInRepository(new ByteArrayInputStream(fileData), fileName, folderPath); } - /* (non-Javadoc) - * @see org.ofbiz.jcr.api.jackrabbit.FileHelper#storeContentInRepository(java.io.InputStream, java.lang.String, java.lang.String) + /* + * (non-Javadoc) + * + * @see + * org.ofbiz.jcr.api.jackrabbit.FileHelper#storeContentInRepository(java + * .io.InputStream, java.lang.String, java.lang.String) */ @Override public void storeContentInRepository(InputStream fileData, String fileName, String folderPath) throws ObjectContentManagerException, RepositoryException { @@ -97,22 +113,14 @@ public class JackrabbitFileHelper extend throw new ObjectContentManagerException("Please specify a folder, a file content can't be stored directly under root."); } - // create an ORM Resource Object - JackrabbitResource ormResource = new JackrabbitResource(); - ormResource.setData(fileData); - ormResource.setMimeType(getMimeTypeFromInputStream(fileData)); - ormResource.setLastModified(new GregorianCalendar()); + JackrabbitResource ormResource = createResource(fileData); - // create an ORM File Object - JackrabbitFile ormFile = new JackrabbitFile(); - ormFile.setCreationDate(new GregorianCalendar()); - ormFile.setResource(ormResource); - ormFile.setPath(fileName); + JackrabbitFile ormFile = createFile(fileName, ormResource); // Create the folder if necessary, otherwise we just update the folder // content folderPath = JcrUtilJackrabbit.createAbsoluteNodePath(folderPath); - if (super.access.getSession().itemExists(folderPath)) { + if (super.access.checkIfNodeExist(folderPath)) { OfbizRepositoryMapping orm = super.access.getContentObject(folderPath); if (orm instanceof JackrabbitFolder) { JackrabbitFolder ormFolder = (JackrabbitFolder) orm; @@ -120,17 +128,15 @@ public class JackrabbitFileHelper extend super.access.updateContentObject(ormFolder); } } else { - // create the ORM folder Object - JackrabbitFolder ormFolder = new JackrabbitFolder(); - ormFolder.addChild(ormFile); - ormFolder.setPath(folderPath); - + JackrabbitFolder ormFolder = createFolder(folderPath, ormFile); super.access.storeContentObject(ormFolder); } } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.ofbiz.jcr.api.jackrabbit.FileHelper#isFileContent() */ @Override @@ -138,7 +144,9 @@ public class JackrabbitFileHelper extend return (hierarchy instanceof JackrabbitFile); } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.ofbiz.jcr.api.jackrabbit.FileHelper#isFolderContent() */ @Override @@ -159,4 +167,53 @@ public class JackrabbitFileHelper extend } } + /** + * Creates a Jackrabbit Folder Object which should be stored in the + * repository. + * + * @param folderPath + * @param ormFile + * @return + */ + private JackrabbitFolder createFolder(String folderPath, JackrabbitFile ormFile) { + // create the ORM folder Object + JackrabbitFolder ormFolder = new JackrabbitFolder(); + ormFolder.addChild(ormFile); + ormFolder.setPath(folderPath); + return ormFolder; + } + + /** + * Creates a Jackrabbit File Object which is needed for a Jackrabbit Folder + * Object. + * + * @param fileName + * @param ormResource + * @return + */ + private JackrabbitFile createFile(String fileName, JackrabbitResource ormResource) { + // create an ORM File Object + JackrabbitFile ormFile = new JackrabbitFile(); + ormFile.setCreationDate(new GregorianCalendar()); + ormFile.setResource(ormResource); + ormFile.setPath(fileName); + return ormFile; + } + + /** + * Creates a Jackrabbit Resource Object which is needed for a Jackrabbit + * File Object. + * + * @param fileData + * @return + */ + private JackrabbitResource createResource(InputStream fileData) { + // create an ORM Resource Object + JackrabbitResource ormResource = new JackrabbitResource(); + ormResource.setData(fileData); + ormResource.setMimeType(getMimeTypeFromInputStream(fileData)); + ormResource.setLastModified(new GregorianCalendar()); + return ormResource; + } + } Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JackrabbitTests.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JackrabbitTests.java?rev=1212400&r1=1212399&r2=1212400&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JackrabbitTests.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/test/JackrabbitTests.java Fri Dec 9 13:26:46 2011 @@ -36,6 +36,7 @@ import org.apache.jackrabbit.ocm.manager import org.apache.jackrabbit.ocm.manager.impl.ObjectContentManagerImpl; import org.apache.jackrabbit.ocm.mapper.Mapper; import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilProperties; import org.ofbiz.entity.GenericValue; import org.ofbiz.jcr.access.ContentWriter; import org.ofbiz.jcr.access.JcrRepositoryAccessor; @@ -117,6 +118,10 @@ public class JackrabbitTests extends OFB assertNotNull(JcrUtilJackrabbit.getRepositoryNodes(userLogin, null)); } + public void testDefaultLanguage() { + assertEquals(UtilProperties.getPropertyValue("general", "locale.properties.fallback"), JcrUtilJackrabbit.determindeTheDefaultLanguage()); + } + // // Jackrabbit Accessor tests // @@ -159,6 +164,14 @@ public class JackrabbitTests extends OFB accessor.closeAccess(); } + public void testAccessorNodeExist() throws RepositoryException { + JcrRepositoryAccessor accessor = new JackrabbitRepositoryAccessor(userLogin); + assertTrue(accessor.checkIfNodeExist("/")); + assertFalse(accessor.checkIfNodeExist("/test")); + + accessor.closeAccess(); + } + // // Content Writer // Modified: ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/jackrabbit/JcrUtilJackrabbit.java URL: http://svn.apache.org/viewvc/ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/jackrabbit/JcrUtilJackrabbit.java?rev=1212400&r1=1212399&r2=1212400&view=diff ============================================================================== --- ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/jackrabbit/JcrUtilJackrabbit.java (original) +++ ofbiz/branches/jackrabbit20100709/framework/jcr/src/org/ofbiz/jcr/util/jackrabbit/JcrUtilJackrabbit.java Fri Dec 9 13:26:46 2011 @@ -11,6 +11,7 @@ import javax.jcr.Session; import javolution.util.FastList; import javolution.util.FastMap; +import org.ofbiz.base.util.UtilProperties; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.GenericValue; import org.ofbiz.jcr.access.jackrabbit.ConstantsJackrabbit; @@ -115,4 +116,13 @@ public class JcrUtilJackrabbit { public static boolean checkIfNodePathIsAbsolute(String nodePath) { return nodePath.startsWith(ConstantsJackrabbit.ROOTPATH); } + + /** + * Return default language from property file. + * + * @return + */ + public static String determindeTheDefaultLanguage() { + return UtilProperties.getPropertyValue("general", "locale.properties.fallback"); + } } |
Free forum by Nabble | Edit this page |