001/**
002 * Copyright 2014 The Sculptor Project Team, including the original
003 * author or authors.
004 * 
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.sculptor.generator.cartridge.mongodb;
018
019import com.google.inject.Inject;
020import java.util.Collection;
021import org.eclipse.emf.common.util.EList;
022import org.eclipse.xtext.xbase.lib.Extension;
023import org.eclipse.xtext.xbase.lib.Functions.Function1;
024import org.eclipse.xtext.xbase.lib.IterableExtensions;
025import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
026import org.sculptor.generator.cartridge.mongodb.MongoDbMapperTmpl;
027import org.sculptor.generator.cartridge.mongodb.MongoDbProperties;
028import org.sculptor.generator.chain.ChainOverride;
029import org.sculptor.generator.ext.Helper;
030import org.sculptor.generator.ext.Properties;
031import org.sculptor.generator.template.RootTmpl;
032import sculptormetamodel.Application;
033import sculptormetamodel.BasicType;
034import sculptormetamodel.DomainObject;
035import sculptormetamodel.Module;
036
037@ChainOverride
038@SuppressWarnings("all")
039public class RootTmplExtension extends RootTmpl {
040  @Inject
041  private MongoDbMapperTmpl mongoDbMapperTmpl;
042  
043  @Inject
044  @Extension
045  private MongoDbProperties mongoDbProperties;
046  
047  @Inject
048  @Extension
049  private Properties properties;
050  
051  @Inject
052  @Extension
053  private Helper helper;
054  
055  @Override
056  public String root(final Application it) {
057    RootTmpl headObj = getMethodsDispatchHead()[org.sculptor.generator.template.RootTmplMethodIndexes.ROOT_APPLICATION];
058    return headObj._chained_root(it);
059  }
060  
061  public RootTmplExtension(final RootTmpl next, final RootTmpl[] methodsDispatchNext) {
062    super(next);
063  }
064  
065  public String _chained_root(final Application it) {
066    String _xblockexpression = null;
067    {
068      boolean _and = false;
069      EList<Module> _modules = it.getModules();
070      boolean _isEmpty = _modules.isEmpty();
071      boolean _not = (!_isEmpty);
072      if (!_not) {
073        _and = false;
074      } else {
075        boolean _isRepositoryToBeGenerated = this.properties.isRepositoryToBeGenerated();
076        _and = _isRepositoryToBeGenerated;
077      }
078      if (_and) {
079        boolean _mongoDb = this.mongoDbProperties.mongoDb();
080        if (_mongoDb) {
081          Collection<DomainObject> _allDomainObjects = this.helper.getAllDomainObjects(it, false);
082          final Function1<DomainObject, Boolean> _function = new Function1<DomainObject, Boolean>() {
083            @Override
084            public Boolean apply(final DomainObject e) {
085              boolean _or = false;
086              boolean _isPersistent = RootTmplExtension.this.helper.isPersistent(e);
087              if (_isPersistent) {
088                _or = true;
089              } else {
090                _or = (e instanceof BasicType);
091              }
092              return Boolean.valueOf(_or);
093            }
094          };
095          Iterable<DomainObject> _filter = IterableExtensions.<DomainObject>filter(_allDomainObjects, _function);
096          final Procedure1<DomainObject> _function_1 = new Procedure1<DomainObject>() {
097            @Override
098            public void apply(final DomainObject it) {
099              RootTmplExtension.this.mongoDbMapperTmpl.mongoDbMapper(it);
100            }
101          };
102          IterableExtensions.<DomainObject>forEach(_filter, _function_1);
103        }
104      }
105      RootTmpl _next = this.getNext();
106      _xblockexpression = _next.root(it);
107    }
108    return _xblockexpression;
109  }
110  
111  public RootTmpl[] _getOverridesDispatchArray() {
112    org.sculptor.generator.template.RootTmpl[] result = new org.sculptor.generator.template.RootTmpl[org.sculptor.generator.template.RootTmplMethodIndexes.NUM_METHODS];
113    result[org.sculptor.generator.template.RootTmplMethodIndexes.ROOT_APPLICATION] = this; 
114    return result;
115  }
116}