001    /*
002     * Copyright 2010 the original author or authors.
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, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.imap.dao;
017    
018    import java.sql.ResultSet;
019    import java.sql.SQLException;
020    import java.util.List;
021    
022    import javax.mail.Flags;
023    
024    import org.apache.commons.lang.ArrayUtils;
025    import org.apache.commons.lang.StringUtils;
026    
027    /**
028     * Mapping between message's system flags and message table's column name
029     * 
030     * @author Won Chul Doh
031     * @since Apr 27, 2010
032     * 
033     */
034    public class FlagUtils {
035    
036            private static Flags.Flag[] flagArray = { Flags.Flag.SEEN,
037                            Flags.Flag.ANSWERED, Flags.Flag.DELETED, Flags.Flag.FLAGGED,
038                            Flags.Flag.RECENT, Flags.Flag.DRAFT };
039    
040            private static String[] attrArray = { "seen", "answered", "deleted",
041                            "flagged", "recent", "draft" };
042    
043            static String getParam(Flags flags, Flags.Flag flag) {
044                    return flags.contains(flag) ? "Y" : "N";
045            }
046    
047            static String getFlagColumnName(Flags.Flag flag) {
048                    for (int i = 0; i < flagArray.length; i++) {
049                            if (flag == flagArray[i]) {
050                                    return attrArray[i];
051                            }
052                    }
053                    return null;
054            }
055    
056            static Flags getFlags(ResultSet rs) throws SQLException {
057                    Flags flags = new Flags();
058                    for (int i = 0; i < attrArray.length; i++) {
059                            if ("Y".equals(rs.getString(attrArray[i]))) {
060                                    flags.add(flagArray[i]);
061                            }
062                    }
063                    return flags;
064            }
065            
066            @SuppressWarnings("unchecked")
067            static String buildParams(Flags.Flag[] flags, boolean replace, boolean set,
068                            List params) {
069                    StringBuilder sb = new StringBuilder();
070                    for (int i = 0; i < flagArray.length; i++) {
071                            boolean contains = ArrayUtils.contains(flags, flagArray[i]);
072                            if (contains || replace) {
073                                    sb.append(attrArray[i]).append("= ?,");
074                                    params.add(replace ? (contains ? "Y" : "N")
075                                                    : (set ? "Y" : "N"));
076                            }
077                    }
078                    return StringUtils.stripEnd(sb.toString(), ",");
079            }
080            
081    }