001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Funsho David
018 */
019
020package org.nuxeo.directory.mongodb;
021
022import static org.nuxeo.mongodb.core.MongoDBSerializationHelper.MONGODB_ID;
023import static org.nuxeo.mongodb.core.MongoDBSerializationHelper.MONGODB_SEQ;
024
025import org.nuxeo.ecm.core.schema.SchemaManager;
026import org.nuxeo.ecm.core.schema.types.Field;
027import org.nuxeo.ecm.core.schema.types.Schema;
028import org.nuxeo.ecm.directory.AbstractDirectory;
029import org.nuxeo.ecm.directory.Directory;
030import org.nuxeo.ecm.directory.DirectoryCSVLoader;
031import org.nuxeo.ecm.directory.DirectoryException;
032import org.nuxeo.ecm.directory.Session;
033import org.nuxeo.mongodb.core.MongoDBSerializationHelper;
034import org.nuxeo.runtime.api.Framework;
035
036import java.util.HashMap;
037import java.util.LinkedHashMap;
038import java.util.Map;
039
040/**
041 * MongoDB implementation of a {@link Directory}
042 * 
043 * @since 9.1
044 */
045public class MongoDBDirectory extends AbstractDirectory {
046
047    protected Map<String, Field> schemaFieldMap;
048
049    protected String countersCollectionName;
050
051    protected boolean initialized;
052
053    public MongoDBDirectory(MongoDBDirectoryDescriptor descriptor) {
054        super(descriptor);
055
056        // register the references to other directories
057        addReferences(descriptor.getInverseReferences());
058        addReferences(descriptor.getMongoDBReferences());
059
060        // cache parameterization
061        cache.setEntryCacheName(descriptor.cacheEntryName);
062        cache.setEntryCacheWithoutReferencesName(descriptor.cacheEntryWithoutReferencesName);
063        cache.setNegativeCaching(descriptor.negativeCaching);
064
065        countersCollectionName = getName() + ".counters";
066
067    }
068
069    @Override
070    public MongoDBDirectoryDescriptor getDescriptor() {
071        return (MongoDBDirectoryDescriptor) descriptor;
072    }
073
074    @Override
075    public Session getSession() throws DirectoryException {
076
077        SchemaManager schemaManager = Framework.getService(SchemaManager.class);
078        Schema schema = schemaManager.getSchema(getSchema());
079        if (schema == null) {
080            throw new DirectoryException(getSchema() + " is not a registered schema");
081        }
082        schemaFieldMap = new LinkedHashMap<>();
083        schema.getFields().forEach(f -> schemaFieldMap.put(f.getName().getLocalName(), f));
084
085        MongoDBSession session = new MongoDBSession(this);
086        addSession(session);
087
088        // Initialize counters collection if autoincrement enabled
089        if (descriptor.isAutoincrementIdField() && !session.hasCollection(countersCollectionName)) {
090            Map<String, Object> seq = new HashMap<>();
091            seq.put(MONGODB_ID, getName());
092            seq.put(MONGODB_SEQ, 0L);
093            session.getCollection(countersCollectionName).insertOne(MongoDBSerializationHelper.fieldMapToBson(seq));
094        }
095
096        if (!initialized && descriptor.getDataFileName() != null && !session.hasCollection(getName())) {
097            DirectoryCSVLoader.loadData(descriptor.getDataFileName(), descriptor.getDataFileCharacterSeparator(),
098                    schema, session::createEntry);
099            initialized = true;
100        }
101        return session;
102    }
103
104    public Map<String, Field> getSchemaFieldMap() {
105        return schemaFieldMap;
106    }
107
108    public String getCountersCollectionName() {
109        return countersCollectionName;
110    }
111}