001    /*
002     *  Copyright 2001-2013 Stephen Colebourne
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 org.joda.beans.integrate.mongo;
017    
018    import java.util.Iterator;
019    import java.util.Map;
020    import java.util.Set;
021    
022    import org.joda.beans.Bean;
023    
024    import com.mongodb.DBObject;
025    
026    /**
027     * Allows a Joda-Bean to be passed directly to MongoDB.
028     *
029     * @author Stephen Colebourne
030     */
031    public class BeanMongoDBObject implements DBObject {
032    
033        /**
034         * The underlying bean.
035         */
036        private final Bean bean;
037        /**
038         * The Mongo partial flag.
039         */
040        private boolean partial;
041    
042        /**
043         * Creates an instance wrapping a bean.
044         * @param bean  the bean to wrap, not null
045         */
046        public BeanMongoDBObject(Bean bean) {
047            this.bean = bean;
048        }
049    
050        //-----------------------------------------------------------------------
051        @Override
052        public boolean containsField(String name) {
053            return bean.propertyNames().contains(name);
054        }
055    
056        /** 
057         * @deprecated Use containsField()
058         */
059        @Override
060        @Deprecated
061        public boolean containsKey(String name) {
062            return containsField(name);
063        }
064    
065        @Override
066        public Object get(String name) {
067            return bean.property(name).get();
068        }
069    
070        @Override
071        public Object put(String name, Object value) {
072            return bean.property(name).put(value);
073        }
074    
075        @Override
076        public void putAll(DBObject object) {
077            for (String name : object.keySet()){
078                put(name, object.get(name));
079            }
080        }
081    
082        @Override
083        @SuppressWarnings("rawtypes")
084        public void putAll(Map map) {
085            for (Iterator it = map.keySet().iterator(); it.hasNext(); ) {
086                Object key = (Object) it.next();
087                put(key.toString(), map.get(key));
088            }
089        }
090    
091        @Override
092        public Object removeField(String name) {
093            throw new UnsupportedOperationException("Remove unsupported");
094        }
095    
096        @Override
097        public Set<String> keySet() {
098            return bean.propertyNames();
099        }
100    
101        @Override
102        @SuppressWarnings("rawtypes")
103        public Map toMap() {
104            return bean.metaBean().createPropertyMap(bean).flatten();
105        }
106    
107        //-----------------------------------------------------------------------
108        @Override
109        public boolean isPartialObject() {
110            return partial;
111        }
112    
113        @Override
114        public void markAsPartialObject() {
115            partial = true;
116        }
117    
118    }