|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (C) 2023, 1C-Soft LLC and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made |
| 5 | + * available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * 1C-Soft LLC - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package com.e1c.v8codestyle.md.check; |
| 14 | + |
| 15 | +import static com._1c.g5.v8.dt.mcore.McorePackage.Literals.NUMBER_QUALIFIERS; |
| 16 | +import static com._1c.g5.v8.dt.mcore.McorePackage.Literals.NUMBER_QUALIFIERS__PRECISION; |
| 17 | +import static com._1c.g5.v8.dt.mcore.McorePackage.Literals.TYPE_DESCRIPTION; |
| 18 | +import static com._1c.g5.v8.dt.mcore.McorePackage.Literals.TYPE_DESCRIPTION__NUMBER_QUALIFIERS; |
| 19 | +import static com._1c.g5.v8.dt.mcore.McorePackage.Literals.TYPE_DESCRIPTION__TYPES; |
| 20 | +import static com._1c.g5.v8.dt.metadata.mdclass.MdClassPackage.Literals.BASIC_DB_OBJECT; |
| 21 | + |
| 22 | +import java.text.MessageFormat; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.function.Supplier; |
| 25 | + |
| 26 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 27 | +import org.eclipse.emf.ecore.EObject; |
| 28 | +import org.eclipse.emf.ecore.EStructuralFeature; |
| 29 | + |
| 30 | +import com._1c.g5.v8.dt.mcore.NumberQualifiers; |
| 31 | +import com._1c.g5.v8.dt.mcore.TypeDescription; |
| 32 | +import com._1c.g5.v8.dt.mcore.util.McoreUtil; |
| 33 | +import com._1c.g5.v8.dt.metadata.mdclass.BasicFeature; |
| 34 | +import com._1c.g5.v8.dt.metadata.mdclass.DefinedType; |
| 35 | +import com._1c.g5.v8.dt.platform.IEObjectTypeNames; |
| 36 | +import com.e1c.g5.v8.dt.check.CheckComplexity; |
| 37 | +import com.e1c.g5.v8.dt.check.ICheckParameters; |
| 38 | +import com.e1c.g5.v8.dt.check.components.BasicCheck; |
| 39 | +import com.e1c.g5.v8.dt.check.settings.IssueSeverity; |
| 40 | +import com.e1c.g5.v8.dt.check.settings.IssueType; |
| 41 | +import com.e1c.v8codestyle.check.StandardCheckExtension; |
| 42 | +import com.e1c.v8codestyle.internal.md.CorePlugin; |
| 43 | + |
| 44 | +/** |
| 45 | + * The check that the DB attribute has number type and it is not more then 31. |
| 46 | + * |
| 47 | + * @author Dmitriy Marmyshev |
| 48 | + * |
| 49 | + */ |
| 50 | +public final class DbObjectMaxNumberLengthCheck |
| 51 | + extends BasicCheck |
| 52 | +{ |
| 53 | + |
| 54 | + private static final String CHECK_ID = "db-object-max-number-length"; //$NON-NLS-1$ |
| 55 | + |
| 56 | + public static final String MAX_LENGTH = "maxNumberLength"; //$NON-NLS-1$ |
| 57 | + |
| 58 | + public static final String MAX_LENGTH_DEFAULT = "31"; //$NON-NLS-1$ |
| 59 | + |
| 60 | + @Override |
| 61 | + public String getCheckId() |
| 62 | + { |
| 63 | + return CHECK_ID; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void configureCheck(CheckConfigurer builder) |
| 68 | + { |
| 69 | + builder.title(Messages.DbObjectMaxNumberLengthCheck_title) |
| 70 | + .description(Messages.DbObjectMaxNumberLengthCheck_description) |
| 71 | + .complexity(CheckComplexity.NORMAL) |
| 72 | + .severity(IssueSeverity.MAJOR) |
| 73 | + .issueType(IssueType.PORTABILITY) |
| 74 | + .extension(new StandardCheckExtension(467, getCheckId(), CorePlugin.PLUGIN_ID)) |
| 75 | + .extension(new SkipAdoptedInExtensionMdObjectExtension()) |
| 76 | + .parameter(MAX_LENGTH, Integer.class, MAX_LENGTH_DEFAULT, Messages.DbObjectMaxNumberLengthCheck_parameter); |
| 77 | + |
| 78 | + builder.topObject(BASIC_DB_OBJECT) |
| 79 | + .containment(TYPE_DESCRIPTION) |
| 80 | + .features(TYPE_DESCRIPTION__TYPES, TYPE_DESCRIPTION__NUMBER_QUALIFIERS); |
| 81 | + |
| 82 | + builder.topObject(BASIC_DB_OBJECT).containment(NUMBER_QUALIFIERS).features(NUMBER_QUALIFIERS__PRECISION); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected void check(Object object, ResultAcceptor resultAceptor, ICheckParameters parameters, |
| 87 | + IProgressMonitor monitor) |
| 88 | + { |
| 89 | + if (object instanceof TypeDescription) |
| 90 | + { |
| 91 | + check((TypeDescription)object, resultAceptor, parameters, monitor); |
| 92 | + } |
| 93 | + else if (object instanceof NumberQualifiers) |
| 94 | + { |
| 95 | + check((NumberQualifiers)object, resultAceptor, parameters, monitor); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void check(TypeDescription object, ResultAcceptor resultAceptor, ICheckParameters parameters, |
| 100 | + IProgressMonitor monitor) |
| 101 | + { |
| 102 | + |
| 103 | + if (!(object.eContainer() instanceof BasicFeature) || object.getTypes().size() != 1 |
| 104 | + || !IEObjectTypeNames.DEFINED_TYPE.equals(McoreUtil.getTypeCategory(object.getTypes().get(0)))) |
| 105 | + { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + TypeDescription typeDescription = object; |
| 110 | + EObject definedType = typeDescription.getTypes().get(0).eContainer(); |
| 111 | + while (definedType != null && !(definedType instanceof DefinedType)) |
| 112 | + { |
| 113 | + definedType = definedType.eContainer(); |
| 114 | + } |
| 115 | + if (definedType instanceof DefinedType) |
| 116 | + { |
| 117 | + typeDescription = ((DefinedType)definedType).getType(); |
| 118 | + } |
| 119 | + |
| 120 | + checkAndAddIssue(typeDescription, () -> { |
| 121 | + BasicFeature basicFeature = (BasicFeature)object.eContainer(); |
| 122 | + return basicFeature.getName(); |
| 123 | + }, resultAceptor, object, TYPE_DESCRIPTION__TYPES, parameters, monitor); |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + private void check(NumberQualifiers object, ResultAcceptor resultAceptor, ICheckParameters parameters, |
| 128 | + IProgressMonitor monitor) |
| 129 | + { |
| 130 | + |
| 131 | + if (!(object.eContainer() instanceof TypeDescription) |
| 132 | + || !(object.eContainer().eContainer() instanceof BasicFeature)) |
| 133 | + { |
| 134 | + return; |
| 135 | + } |
| 136 | + TypeDescription typeDescription = (TypeDescription)object.eContainer(); |
| 137 | + |
| 138 | + checkAndAddIssue(typeDescription, () -> { |
| 139 | + BasicFeature basicFeature = (BasicFeature)typeDescription.eContainer(); |
| 140 | + return basicFeature.getName(); |
| 141 | + }, resultAceptor, object, NUMBER_QUALIFIERS__PRECISION, parameters, monitor); |
| 142 | + } |
| 143 | + |
| 144 | + private void checkAndAddIssue(TypeDescription typeDescription, Supplier<String> basicFeatureName, |
| 145 | + ResultAcceptor resultAceptor, EObject object, EStructuralFeature feature, ICheckParameters parameters, |
| 146 | + IProgressMonitor monitor) |
| 147 | + { |
| 148 | + if (monitor.isCanceled() || typeDescription.getNumberQualifiers() == null |
| 149 | + || typeDescription.getTypes() |
| 150 | + .stream() |
| 151 | + .map(McoreUtil::getTypeName) |
| 152 | + .filter(Objects::nonNull) |
| 153 | + .noneMatch(IEObjectTypeNames.NUMBER::equals)) |
| 154 | + { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + int maxPrecision = parameters.getInt(MAX_LENGTH); |
| 159 | + int precision = typeDescription.getNumberQualifiers().getPrecision(); |
| 160 | + |
| 161 | + if (precision > maxPrecision) |
| 162 | + { |
| 163 | + resultAceptor.addIssue(MessageFormat.format(Messages.DbObjectMaxNumberLengthCheck_message, |
| 164 | + basicFeatureName.get(), maxPrecision), object, feature); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments