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.builder; 018 019import com.google.common.base.Objects; 020import java.util.Arrays; 021import java.util.Collection; 022import java.util.List; 023import javax.inject.Inject; 024import org.eclipse.xtext.xbase.lib.CollectionLiterals; 025import org.eclipse.xtext.xbase.lib.Extension; 026import org.eclipse.xtext.xbase.lib.Functions.Function1; 027import org.eclipse.xtext.xbase.lib.IterableExtensions; 028import org.sculptor.generator.cartridge.builder.BuilderProperties; 029import org.sculptor.generator.chain.ChainLink; 030import org.sculptor.generator.chain.ChainOverridable; 031import org.sculptor.generator.ext.Helper; 032import org.sculptor.generator.util.HelperBase; 033import sculptormetamodel.Attribute; 034import sculptormetamodel.DomainObject; 035import sculptormetamodel.Module; 036import sculptormetamodel.NamedElement; 037import sculptormetamodel.Reference; 038 039@ChainOverridable 040@SuppressWarnings("all") 041public class BuilderHelper extends ChainLink<BuilderHelper> { 042 @Inject 043 @Extension 044 private HelperBase helperBase; 045 046 @Inject 047 @Extension 048 private Helper helper; 049 050 @Inject 051 @Extension 052 private BuilderProperties properties; 053 054 public String getBuilderPackage(final Module module) { 055 String _basePackage = this.helperBase.getBasePackage(module); 056 String _builderPackage = this.properties.getBuilderPackage(); 057 return this.helperBase.concatPackage(_basePackage, _builderPackage); 058 } 059 060 public String getBuilderPackage(final DomainObject it) { 061 Module _module = it.getModule(); 062 return this.getBuilderPackage(_module); 063 } 064 065 public List<Attribute> getBuilderAttributes(final DomainObject domainObject) { 066 BuilderHelper headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERATTRIBUTES_DOMAINOBJECT]; 067 return headObj._chained_getBuilderAttributes(domainObject); 068 } 069 070 public List<Reference> getBuilderReferences(final DomainObject domainObject) { 071 BuilderHelper headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERREFERENCES_DOMAINOBJECT]; 072 return headObj._chained_getBuilderReferences(domainObject); 073 } 074 075 public List<NamedElement> getBuilderConstructorParameters(final DomainObject domainObject) { 076 BuilderHelper headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERCONSTRUCTORPARAMETERS_DOMAINOBJECT]; 077 return headObj._chained_getBuilderConstructorParameters(domainObject); 078 } 079 080 public List<NamedElement> getBuilderProperties(final DomainObject domainObject) { 081 BuilderHelper headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERPROPERTIES_DOMAINOBJECT]; 082 return headObj._chained_getBuilderProperties(domainObject); 083 } 084 085 public String getBuilderClassName(final DomainObject domainObject) { 086 BuilderHelper headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERCLASSNAME_DOMAINOBJECT]; 087 return headObj._chained_getBuilderClassName(domainObject); 088 } 089 090 public String getBuilderFqn(final DomainObject domainObject) { 091 BuilderHelper headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERFQN_DOMAINOBJECT]; 092 return headObj._chained_getBuilderFqn(domainObject); 093 } 094 095 public boolean _needsBuilder(final DomainObject domainObject) { 096 BuilderHelper headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes._NEEDSBUILDER_DOMAINOBJECT]; 097 return headObj._chained__needsBuilder(domainObject); 098 } 099 100 public boolean _needsBuilder(final sculptormetamodel.Enum domainObject) { 101 BuilderHelper headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes._NEEDSBUILDER_ENUM]; 102 return headObj._chained__needsBuilder(domainObject); 103 } 104 105 public boolean needsBuilder(final DomainObject domainObject) { 106 if (domainObject instanceof sculptormetamodel.Enum) { 107 return _needsBuilder((sculptormetamodel.Enum)domainObject); 108 } else if (domainObject != null) { 109 return _needsBuilder(domainObject); 110 } else { 111 throw new IllegalArgumentException("Unhandled parameter types: " + 112 Arrays.<Object>asList(domainObject).toString()); 113 } 114 } 115 116 public BuilderHelper(final BuilderHelper next) { 117 super(next); 118 } 119 120 public List<Attribute> _chained_getBuilderAttributes(final DomainObject domainObject) { 121 Collection<Attribute> _allAttributes = this.helper.getAllAttributes(domainObject); 122 final Function1<Attribute, Boolean> _function = new Function1<Attribute, Boolean>() { 123 public Boolean apply(final Attribute a) { 124 boolean _and = false; 125 boolean _and_1 = false; 126 boolean _isUuid = BuilderHelper.this.helper.isUuid(a); 127 boolean _not = (!_isUuid); 128 if (!_not) { 129 _and_1 = false; 130 } else { 131 Attribute _idAttribute = BuilderHelper.this.helper.getIdAttribute(domainObject); 132 boolean _notEquals = (!Objects.equal(a, _idAttribute)); 133 _and_1 = _notEquals; 134 } 135 if (!_and_1) { 136 _and = false; 137 } else { 138 String _name = a.getName(); 139 boolean _notEquals_1 = (!Objects.equal(_name, "version")); 140 _and = _notEquals_1; 141 } 142 return Boolean.valueOf(_and); 143 } 144 }; 145 Iterable<Attribute> _filter = IterableExtensions.<Attribute>filter(_allAttributes, _function); 146 return IterableExtensions.<Attribute>toList(_filter); 147 } 148 149 public List<Reference> _chained_getBuilderReferences(final DomainObject domainObject) { 150 Collection<Reference> _allReferences = this.helper.getAllReferences(domainObject); 151 return IterableExtensions.<Reference>toList(_allReferences); 152 } 153 154 public List<NamedElement> _chained_getBuilderConstructorParameters(final DomainObject domainObject) { 155 return this.helper.getConstructorParameters(domainObject); 156 } 157 158 public List<NamedElement> _chained_getBuilderProperties(final DomainObject domainObject) { 159 List<NamedElement> _xblockexpression = null; 160 { 161 final List<NamedElement> retVal = CollectionLiterals.<NamedElement>newArrayList(); 162 List<Attribute> _builderAttributes = this.getBuilderAttributes(domainObject); 163 retVal.addAll(_builderAttributes); 164 List<Reference> _builderReferences = this.getBuilderReferences(domainObject); 165 retVal.addAll(_builderReferences); 166 _xblockexpression = retVal; 167 } 168 return _xblockexpression; 169 } 170 171 public String _chained_getBuilderClassName(final DomainObject domainObject) { 172 String _name = domainObject.getName(); 173 return (_name + "Builder"); 174 } 175 176 public String _chained_getBuilderFqn(final DomainObject domainObject) { 177 String _builderPackage = this.getBuilderPackage(domainObject); 178 String _plus = (_builderPackage + "."); 179 String _builderClassName = this.getBuilderClassName(domainObject); 180 return (_plus + _builderClassName); 181 } 182 183 public boolean _chained__needsBuilder(final DomainObject domainObject) { 184 boolean _isAbstract = domainObject.isAbstract(); 185 return (_isAbstract == false); 186 } 187 188 public boolean _chained__needsBuilder(final sculptormetamodel.Enum domainObject) { 189 return false; 190 } 191 192 public BuilderHelper[] _getOverridesDispatchArray() { 193 org.sculptor.generator.cartridge.builder.BuilderHelper[] result = new org.sculptor.generator.cartridge.builder.BuilderHelper[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.NUM_METHODS]; 194 result[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERATTRIBUTES_DOMAINOBJECT] = this; 195 result[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERREFERENCES_DOMAINOBJECT] = this; 196 result[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERCONSTRUCTORPARAMETERS_DOMAINOBJECT] = this; 197 result[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERPROPERTIES_DOMAINOBJECT] = this; 198 result[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERCLASSNAME_DOMAINOBJECT] = this; 199 result[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes.GETBUILDERFQN_DOMAINOBJECT] = this; 200 result[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes._NEEDSBUILDER_DOMAINOBJECT] = this; 201 result[org.sculptor.generator.cartridge.builder.BuilderHelperMethodIndexes._NEEDSBUILDER_ENUM] = this; 202 return result; 203 } 204}