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.common.collect.Iterables; 020import java.util.Collection; 021import java.util.List; 022import javax.inject.Inject; 023import org.eclipse.xtend2.lib.StringConcatenation; 024import org.eclipse.xtext.xbase.lib.Extension; 025import org.eclipse.xtext.xbase.lib.Functions.Function1; 026import org.eclipse.xtext.xbase.lib.IterableExtensions; 027import org.sculptor.generator.cartridge.mongodb.MongoDbHelper; 028import org.sculptor.generator.ext.DbHelper; 029import org.sculptor.generator.ext.Helper; 030import org.sculptor.generator.ext.Properties; 031import org.sculptor.generator.util.HelperBase; 032import sculptormetamodel.DomainObject; 033import sculptormetamodel.Module; 034import sculptormetamodel.Reference; 035import sculptormetamodel.Repository; 036 037@SuppressWarnings("all") 038public class MongoDbAccessObjectFactoryTmpl { 039 @Inject 040 @Extension 041 private MongoDbHelper mongoDbHelper; 042 043 @Inject 044 @Extension 045 private DbHelper dbHelper; 046 047 @Inject 048 @Extension 049 private HelperBase helperBase; 050 051 @Inject 052 @Extension 053 private Helper helper; 054 055 @Inject 056 @Extension 057 private Properties properties; 058 059 public String getAdditionalDataMappers(final Repository it) { 060 String _xblockexpression = null; 061 { 062 DomainObject _aggregateRoot = it.getAggregateRoot(); 063 Collection<DomainObject> _aggregate = this.helper.getAggregate(_aggregateRoot); 064 final Function1<DomainObject, Collection<Reference>> _function = new Function1<DomainObject, Collection<Reference>>() { 065 @Override 066 public Collection<Reference> apply(final DomainObject ag) { 067 return MongoDbAccessObjectFactoryTmpl.this.helper.getAllReferences(ag); 068 } 069 }; 070 Iterable<Collection<Reference>> _map = IterableExtensions.<DomainObject, Collection<Reference>>map(_aggregate, _function); 071 Iterable<Reference> _flatten = Iterables.<Reference>concat(_map); 072 final Function1<Reference, Boolean> _function_1 = new Function1<Reference, Boolean>() { 073 @Override 074 public Boolean apply(final Reference e) { 075 return Boolean.valueOf(MongoDbAccessObjectFactoryTmpl.this.helper.isUnownedReference(e)); 076 } 077 }; 078 final Iterable<Reference> allUnownedReferences = IterableExtensions.<Reference>filter(_flatten, _function_1); 079 DomainObject _aggregateRoot_1 = it.getAggregateRoot(); 080 List<Reference> _naturalKeyReferences = this.helper.getNaturalKeyReferences(_aggregateRoot_1); 081 final Function1<Reference, Boolean> _function_2 = new Function1<Reference, Boolean>() { 082 @Override 083 public Boolean apply(final Reference e) { 084 boolean _isEnumReference = MongoDbAccessObjectFactoryTmpl.this.helper.isEnumReference(e); 085 return Boolean.valueOf((!_isEnumReference)); 086 } 087 }; 088 final Iterable<Reference> allNonEnumNaturalKeyReferences = IterableExtensions.<Reference>filter(_naturalKeyReferences, _function_2); 089 StringConcatenation _builder = new StringConcatenation(); 090 _builder.append("@SuppressWarnings(\"rawtypes\")"); 091 _builder.newLine(); 092 _builder.append("private "); 093 String _fw = this.properties.fw("accessimpl.mongodb.DataMapper"); 094 _builder.append(_fw, ""); 095 _builder.append("[] additionalDataMappers ="); 096 _builder.newLineIfNotEmpty(); 097 _builder.append("\t"); 098 _builder.append("new "); 099 String _fw_1 = this.properties.fw("accessimpl.mongodb.DataMapper"); 100 _builder.append(_fw_1, "\t"); 101 _builder.append("[] {"); 102 _builder.newLineIfNotEmpty(); 103 { 104 boolean _isJodaDateTimeLibrary = this.dbHelper.isJodaDateTimeLibrary(); 105 if (_isJodaDateTimeLibrary) { 106 _builder.append("\t\t"); 107 String _fw_2 = this.properties.fw("accessimpl.mongodb.JodaLocalDateMapper"); 108 _builder.append(_fw_2, "\t\t"); 109 _builder.append(".getInstance(),"); 110 _builder.newLineIfNotEmpty(); 111 _builder.append("\t\t"); 112 String _fw_3 = this.properties.fw("accessimpl.mongodb.JodaDateTimeMapper"); 113 _builder.append(_fw_3, "\t\t"); 114 _builder.append(".getInstance(),"); 115 _builder.newLineIfNotEmpty(); 116 } 117 } 118 _builder.append("\t\t"); 119 String _fw_4 = this.properties.fw("accessimpl.mongodb.EnumMapper"); 120 _builder.append(_fw_4, "\t\t"); 121 _builder.append(".getInstance()"); 122 { 123 boolean _isEmpty = IterableExtensions.isEmpty(allUnownedReferences); 124 boolean _not = (!_isEmpty); 125 if (_not) { 126 _builder.append(","); 127 } 128 } 129 _builder.newLineIfNotEmpty(); 130 { 131 boolean _hasElements = false; 132 for(final Reference ref : allUnownedReferences) { 133 if (!_hasElements) { 134 _hasElements = true; 135 } else { 136 _builder.appendImmediate(", ", "\t\t"); 137 } 138 _builder.append("\t\t"); 139 String _fw_5 = this.properties.fw("accessimpl.mongodb.IdMapper"); 140 _builder.append(_fw_5, "\t\t"); 141 _builder.append(".getInstance("); 142 DomainObject _to = ref.getTo(); 143 String _domainPackage = this.helperBase.getDomainPackage(_to); 144 _builder.append(_domainPackage, "\t\t"); 145 _builder.append("."); 146 DomainObject _to_1 = ref.getTo(); 147 String _name = _to_1.getName(); 148 _builder.append(_name, "\t\t"); 149 _builder.append(".class)"); 150 _builder.newLineIfNotEmpty(); 151 _builder.append("\t\t\t\t\t"); 152 } 153 } 154 { 155 boolean _isEmpty_1 = IterableExtensions.isEmpty(allNonEnumNaturalKeyReferences); 156 boolean _not_1 = (!_isEmpty_1); 157 if (_not_1) { 158 _builder.append(","); 159 } 160 } 161 _builder.newLineIfNotEmpty(); 162 { 163 boolean _hasElements_1 = false; 164 for(final Reference ref_1 : allNonEnumNaturalKeyReferences) { 165 if (!_hasElements_1) { 166 _hasElements_1 = true; 167 } else { 168 _builder.appendImmediate(", ", "\t\t"); 169 } 170 _builder.append("\t\t"); 171 DomainObject _to_2 = ref_1.getTo(); 172 Module _module = _to_2.getModule(); 173 String _mapperPackage = this.mongoDbHelper.getMapperPackage(_module); 174 _builder.append(_mapperPackage, "\t\t"); 175 _builder.append("."); 176 DomainObject _to_3 = ref_1.getTo(); 177 String _name_1 = _to_3.getName(); 178 _builder.append(_name_1, "\t\t"); 179 _builder.append("Mapper.getInstance()"); 180 _builder.newLineIfNotEmpty(); 181 } 182 } 183 _builder.append("\t\t"); 184 _builder.append("};"); 185 _builder.newLine(); 186 _builder.newLine(); 187 _builder.append("@SuppressWarnings(\"unchecked\")"); 188 _builder.newLine(); 189 _builder.append("protected "); 190 String _fw_6 = this.properties.fw("accessimpl.mongodb.DataMapper"); 191 _builder.append(_fw_6, ""); 192 _builder.append("<Object, com.mongodb.DBObject>[] getAdditionalDataMappers() {"); 193 _builder.newLineIfNotEmpty(); 194 _builder.append("\t"); 195 _builder.append("return additionalDataMappers;"); 196 _builder.newLine(); 197 _builder.append("}"); 198 _builder.newLine(); 199 _xblockexpression = _builder.toString(); 200 } 201 return _xblockexpression; 202 } 203 204 public String ensureIndex(final Repository it) { 205 StringConcatenation _builder = new StringConcatenation(); 206 _builder.append("@javax.annotation.PostConstruct"); 207 _builder.newLine(); 208 _builder.append("protected void ensureIndex() {"); 209 _builder.newLine(); 210 _builder.append("\t"); 211 _builder.append("com.mongodb.DBCollection dbCollection = dbManager.getDBCollection("); 212 DomainObject _aggregateRoot = it.getAggregateRoot(); 213 Module _module = _aggregateRoot.getModule(); 214 String _mapperPackage = this.mongoDbHelper.getMapperPackage(_module); 215 _builder.append(_mapperPackage, "\t"); 216 _builder.append("."); 217 DomainObject _aggregateRoot_1 = it.getAggregateRoot(); 218 String _name = _aggregateRoot_1.getName(); 219 _builder.append(_name, "\t"); 220 _builder.append("Mapper.getInstance().getDBCollectionName());"); 221 _builder.newLineIfNotEmpty(); 222 _builder.append("\t"); 223 _builder.append("for ("); 224 String _fw = this.properties.fw("accessimpl.mongodb.IndexSpecification"); 225 _builder.append(_fw, "\t"); 226 _builder.append(" each : "); 227 DomainObject _aggregateRoot_2 = it.getAggregateRoot(); 228 Module _module_1 = _aggregateRoot_2.getModule(); 229 String _mapperPackage_1 = this.mongoDbHelper.getMapperPackage(_module_1); 230 _builder.append(_mapperPackage_1, "\t"); 231 _builder.append("."); 232 DomainObject _aggregateRoot_3 = it.getAggregateRoot(); 233 String _name_1 = _aggregateRoot_3.getName(); 234 _builder.append(_name_1, "\t"); 235 _builder.append("Mapper.getInstance().indexes()) {"); 236 _builder.newLineIfNotEmpty(); 237 _builder.append("\t\t"); 238 _builder.append("dbCollection.ensureIndex(each.getKeys(), each.getName(), each.isUnique());"); 239 _builder.newLine(); 240 _builder.append("\t"); 241 _builder.append("}"); 242 _builder.newLine(); 243 _builder.append("}"); 244 _builder.newLine(); 245 return _builder.toString(); 246 } 247}