001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *
010     *        http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License.
018     */
019    
020    package org.apache.isis.core.progmodel.facetdecorators.i18n.resourcebundle.internal;
021    
022    import java.util.List;
023    import java.util.MissingResourceException;
024    import java.util.ResourceBundle;
025    
026    import org.apache.isis.applib.Identifier;
027    import org.apache.isis.core.commons.config.IsisConfiguration;
028    import org.apache.isis.core.commons.lang.StringUtils;
029    import org.apache.isis.core.progmodel.facetdecorators.i18n.I18nManager;
030    import org.apache.log4j.Logger;
031    
032    import com.google.common.collect.Lists;
033    
034    /**
035     * REVIEW: why isn't there a type for collections also?
036     */
037    public class I18nManagerUsingResourceBundle implements I18nManager {
038    
039        private static final Logger LOG = Logger.getLogger(I18nManagerUsingResourceBundle.class);
040    
041        private static final String BASE_FILE_NAME = "i18n";
042    
043        private static final String MEMBER_TYPE_PROPERTY = "property";
044        private static final String MEMBER_TYPE_COLLECTION = "collection";
045        private static final String MEMBER_TYPE_ACTION = "action";
046        private static final String MEMBER_TYPE_PARAMETER = "parameter";
047    
048        private static final String TEXT_TYPE_NAME = "name";
049        private static final String TEXT_TYPE_DESCRIPTION = "description";
050        private static final String TEXT_TYPE_HELP = "help";
051    
052        private ResourceBundle bundle;
053    
054        @SuppressWarnings("unused")
055        private final IsisConfiguration configuration;
056    
057        // //////////////////////////////////////////////////////////////
058        // Contructor, init, shutdown
059        // //////////////////////////////////////////////////////////////
060    
061        public I18nManagerUsingResourceBundle(final IsisConfiguration configuration) {
062            this.configuration = configuration;
063        }
064    
065        @Override
066        public void init() {
067            try {
068                bundle = ResourceBundle.getBundle(BASE_FILE_NAME);
069            } catch (final MissingResourceException e) {
070                LOG.warn("Missing resource bundle: " + e.getMessage());
071            }
072    
073        }
074    
075        @Override
076        public void shutdown() {
077        }
078    
079        // //////////////////////////////////////////////////////////////
080        // Members
081        // //////////////////////////////////////////////////////////////
082    
083        @Override
084        public String getName(final Identifier identifier) {
085            return internalizedTextForClassMember(identifier, TEXT_TYPE_NAME);
086        }
087    
088        @Override
089        public String getDescription(final Identifier identifier) {
090            return internalizedTextForClassMember(identifier, TEXT_TYPE_DESCRIPTION);
091        }
092    
093        @Override
094        public String getHelp(final Identifier identifier) {
095            return internalizedTextForClassMember(identifier, TEXT_TYPE_HELP);
096        }
097    
098        private String internalizedTextForClassMember(final Identifier identifier, final String textType) {
099            if (bundle == null) {
100                return null;
101            }
102            final List<String> key = buildMemberTypeKey(identifier, textType);
103            return lookupTextFromBundle(key);
104        }
105    
106        private static List<String> buildMemberTypeKey(final Identifier identifier, final String textType) {
107            final List<String> keys = Lists.newArrayList();
108    
109            if (identifier.isPropertyOrCollection()) {
110                keys.add(buildMemberTypeKey(identifier, textType, MEMBER_TYPE_PROPERTY));
111                keys.add(buildMemberTypeKey(identifier, textType, MEMBER_TYPE_COLLECTION));
112            } else {
113                keys.add(buildMemberTypeKey(identifier, textType, MEMBER_TYPE_ACTION));
114            }
115            return keys;
116        }
117    
118        private static String buildMemberTypeKey(final Identifier identifier, final String textType, final String memberType) {
119            final StringBuilder sb = new StringBuilder();
120            sb.append(identifier.getClassName()).append(".");
121            sb.append(memberType);
122            final String memberName = identifier.getMemberName();
123            if (!StringUtils.isNullOrEmpty(memberName)) {
124                sb.append(".").append(memberName);
125            }
126            sb.append(".").append(textType);
127            return sb.toString();
128        }
129    
130        // //////////////////////////////////////////////////////////////
131        // Parameters
132        // //////////////////////////////////////////////////////////////
133    
134        @Override
135        public List<String> getParameterNames(final Identifier identifier) {
136            return internalizedTextForParameter(identifier, TEXT_TYPE_NAME);
137        }
138    
139        private List<String> internalizedTextForParameter(final Identifier identifier, final String textType) {
140            if (bundle == null) {
141                return null;
142            }
143            final List<String> internalizedText = Lists.newArrayList();
144            final List<String> memberParameterNames = identifier.getMemberParameterNames();
145            int paramNum = 0;
146            for (@SuppressWarnings("unused")
147            final String dummy : memberParameterNames) {
148                final String key = buildParameterTypeKey(identifier, textType, paramNum);
149                internalizedText.add(lookupTextFromBundle(key));
150                paramNum++;
151            }
152            return internalizedText;
153        }
154    
155        private static String buildParameterTypeKey(final Identifier identifier, final String textType, final int paramNum) {
156            return identifier.getClassName() + "." + MEMBER_TYPE_ACTION + "." + identifier.getMemberName() + "."
157                + MEMBER_TYPE_PARAMETER + (paramNum + 1) + "." + textType;
158        }
159    
160        // //////////////////////////////////////////////////////////////
161        // Helpers
162        // //////////////////////////////////////////////////////////////
163    
164        private String lookupTextFromBundle(final List<String> keys) {
165            for (final String key : keys) {
166                final String text = lookupTextFromBundle(key);
167                if (text != null) {
168                    return text;
169                }
170            }
171            return null;
172        }
173    
174        private String lookupTextFromBundle(final String key) {
175            try {
176                return bundle.getString(key);
177            } catch (final MissingResourceException e) {
178                return null;
179            }
180        }
181    
182    }