001    /*
002     * Copyright (c) 2009 The openGion Project.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013     * either express or implied. See the License for the specific language
014     * governing permissions and limitations under the License.
015     */
016    package org.opengion.hayabusa.db;
017    
018    import java.util.Arrays;
019    import java.util.Comparator;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    /**
024     * ユーザー単位?エ??設定情報を管?るため?クラスです?
025     *
026     * 画面ID+エ??名をキーとしてエ??設定オブジェクト?
027     * 追??削除、参照を行います?
028     *
029     * @og.rev 5.3.6.0 (2011/06/01) 新規追?
030     *
031     * @version  5.0
032     * @author   Hiroki Nakamura
033     * @since    JDK6.0,
034     */
035    public class DBEditConfigManager  {
036    
037            // エ??設定情報の管?ブジェク?画面ID+エ??名単位で管?
038            private final Map<String,Map<String,DBEditConfig>> editConfigMap = new HashMap<String,Map<String,DBEditConfig>>();
039    
040            /**
041             * ?ォルトコンストラクターです?
042             */
043    //      public DBEditConfigManager() {
044    //      }
045    
046            /**
047             * エ??設定オブジェクトを追?ます?
048             *
049             * ここでの追??あくまでメモリ上での登録になります?
050             * 登録した?を永続的に登録する場合?、別途DBへの登録が?になります?
051             *
052             * @param guikey 画面ID
053             * @param editName エ???
054             * @param config エ??設定オブジェク?
055             */
056            public void addEditConfig( final String guikey, final String editName, final DBEditConfig config ) {
057                    if( guikey == null || guikey.length() == 0 ) { return; }
058                    if( editName == null || editName.length() == 0 ) { return; }
059                    if( config == null ) { return; }
060    
061                    synchronized( editConfigMap ) {
062                            Map<String,DBEditConfig> map = editConfigMap.get( guikey );
063                            if( map == null ) {
064                                    map = new HashMap<String, DBEditConfig>();
065                            }
066                            map.put( editName, config );
067    
068                            editConfigMap.put( guikey, map );
069                    }
070            }
071    
072            /**
073             * エ??設定オブジェクトを削除します?
074             *
075             * ここでの追??あくまでメモリ上での削除になります?
076             * 登録した?を永続的に削除する場合?、別途DBへの登録が?になります?
077             *
078             * @param guikey 画面ID
079             * @param editName エ???
080             *
081             * @return エ??設定オブジェク?
082             */
083            public DBEditConfig deleteEditConfig( final String guikey, final String editName ) {
084                    if( guikey == null || guikey.length() == 0 ) { return null; }
085                    if( editName == null || editName.length() == 0 ) { return null; }
086    
087                    DBEditConfig config = null;
088                    synchronized( editConfigMap ) {
089                            Map<String,DBEditConfig> map = editConfigMap.get( guikey );
090                            if( map == null ) { return null; }
091                            config = map.remove( editName );
092                            editConfigMap.put( guikey, map );
093                    }
094                    return config;
095            }
096    
097            /**
098             * エ??設定オブジェクトを取得します?
099             *
100             * @param guikey 画面ID
101             * @param editName エ???
102             *
103             * @return エ??設定オブジェク?
104             */
105            public DBEditConfig getEditConfig( final String guikey, final String editName ) {
106                    if( guikey == null || guikey.length() == 0 ) { return null; }
107                    if( editName == null || editName.length() == 0 ) { return null; }
108    
109                    synchronized( editConfigMap ) {
110                            Map<String,DBEditConfig> map = editConfigMap.get( guikey );
111                            if( map == null ) { return null; }
112                            return map.get( editName );
113                    }
114            }
115    
116            /**
117             * 画面IDをキーにエ??設定??(配?)を返します?
118             * 返される配?は、エ??名?にソートされた状態で返されます?
119             *
120             * @param guikey 画面ID
121             *
122             * @return エ??設定?(配?)
123             */
124            public DBEditConfig[] getEditConfigs( final String guikey ) {
125                    if( guikey == null || guikey.length() == 0 ) { return null; }
126    
127                    Map<String,DBEditConfig> map = editConfigMap.get( guikey );
128                    if( map == null ) { return null; }
129    
130                    DBEditConfig[] configs = map.values().toArray( new DBEditConfig[0] );
131                    Arrays.sort( configs
132                                    , new Comparator<DBEditConfig> (){
133                                            public int compare( final DBEditConfig c1, final DBEditConfig c2 ) {
134                                                    return ( c1 == null ? -1 : c1.getEditName().compareTo( c2.getEditName() ) );
135                                            }
136                                    }
137                    );
138    
139                    return configs;
140            }
141    }