Skip to content

Commit

Permalink
#905 | Disable Unique attribute without an index for pk attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
mlytvyn authored Jan 3, 2024
1 parent 4eff407 commit 564ba78
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
- Inspect a value set for `translator` modifier [#901](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/901)
- Inspect a value set for `cellDecorator` modifier [#902](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/902)
- Inspect a value set for `mode` modifier [#903](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/903)
- Inspect a value set for modifier of boolean type [#904](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/904)
- Inspect a value set for modifier of a boolean type [#904](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/904)
- Disable `Unique attribute without an index` for `pk` attribute [#905](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/905)

### `FlexibleSearch` enhancements
- Improved `words case` and `table alias` actions performance [#874](https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/874)
Expand Down
4 changes: 2 additions & 2 deletions resources/i18n/HybrisBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ hybris.inspections.impex.ImpExOrphanValueGroupInspection.key=[y] Orphan value gr
hybris.inspections.impex.ImpExMultilineMacroNameInspection.key=[y] Multi-line macro name usage should be omitted.
hybris.inspections.impex.ImpExUnresolvedClassReferenceInspection.unresolved=[y] Modifier ''{0}'' ''{1}'' points to non-existing class.
hybris.inspections.impex.ImpExUnresolvedClassReferenceInspection.wrongImplementation=[y] Modifier ''{0}'' ''{1}'' should extend ''{2}''.
hybris.inspections.impex.ImpExInvalidModeValueInspection.key=[y] Invalid mode modifier value ''{0}'', only 'append', 'remove' or 'merge' can be used.
hybris.inspections.impex.ImpExInvalidBooleanModifierValueInspection.key=[y] Invalid ''{0}'' modifier value ''{1}'', only 'true' and 'false' can be used.
hybris.inspections.impex.ImpExInvalidModeValueInspection.key=[y] Invalid mode modifier value ''{0}'', only ''append'', ''remove'' or ''merge'' can be used.
hybris.inspections.impex.ImpExInvalidBooleanModifierValueInspection.key=[y] Invalid ''{0}'' modifier value ''{1}'', only ''true'' and ''false'' can be used.

hybris.inspections.fix.impex.DeleteModifier=Delete modifier
hybris.inspections.fix.impex.DeleteModifier.text=Delete modifier ''{0}''
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of "SAP Commerce Developers Toolset" plugin for Intellij IDEA.
* Copyright (C) 2019-2023 EPAM Systems <[email protected]> and contributors
* This file is part of "SAP Commerce Developers Toolset" plugin for IntelliJ IDEA.
* Copyright (C) 2019-2024 EPAM Systems <[email protected]> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand All @@ -21,6 +21,7 @@ package com.intellij.idea.plugin.hybris.codeInspection.rule.impex
import com.intellij.codeHighlighting.HighlightDisplayLevel
import com.intellij.codeInspection.LocalInspectionTool
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.idea.plugin.hybris.common.HybrisConstants
import com.intellij.idea.plugin.hybris.common.utils.HybrisI18NBundleUtils.message
import com.intellij.idea.plugin.hybris.impex.psi.ImpexFullHeaderParameter
import com.intellij.idea.plugin.hybris.impex.psi.ImpexHeaderLine
Expand All @@ -32,39 +33,38 @@ import com.intellij.psi.util.PsiTreeUtil
class ImpexUniqueAttributeWithoutIndexInspection : LocalInspectionTool() {

override fun getDefaultLevel(): HighlightDisplayLevel = HighlightDisplayLevel.ERROR
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = ParameterChecker(holder)
}
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = object : ImpexVisitor() {

private class ParameterChecker(val holder: ProblemsHolder) : ImpexVisitor() {
override fun visitFullHeaderParameter(param: ImpexFullHeaderParameter) {
val attribute = param.anyHeaderParameterName.text

override fun visitFullHeaderParameter(param: ImpexFullHeaderParameter) {
param.modifiersList
.flatMap { it.attributeList }
.asSequence()
.filter { it.anyAttributeValue?.text == "true" }
.filter { it.anyAttributeName.stringList.isEmpty() }
.filter { it.anyAttributeName.firstChild == it.anyAttributeName.lastChild }
.filter { it.anyAttributeName.text == "unique" }
.firstOrNull()
?: return
val attribute = param.anyHeaderParameterName.text
// no need to validate special parameters
if (attribute.startsWith('@') || HybrisConstants.ATTRIBUTE_PK.equals(attribute, true)) return

// no need to validate special parameters
if (attribute.startsWith('@')) return
param.modifiersList
.flatMap { it.attributeList }
.asSequence()
.filter { it.anyAttributeValue?.text == "true" }
.filter { it.anyAttributeName.stringList.isEmpty() }
.filter { it.anyAttributeName.firstChild == it.anyAttributeName.lastChild }
.filter { it.anyAttributeName.text == "unique" }
.firstOrNull()
?: return

val typeName = PsiTreeUtil.getParentOfType(param, ImpexHeaderLine::class.java)
?.fullHeaderType
?.headerTypeName
?.text
?: return
val typeName = PsiTreeUtil.getParentOfType(param, ImpexHeaderLine::class.java)
?.fullHeaderType
?.headerTypeName
?.text
?: return

val hasIndex = TSMetaModelAccess.getInstance(param.project).findMetaItemByName(typeName)
?.allIndexes
?.flatMap { it.keys }
?.any { it.equals(attribute, true) }
?: true
val hasIndex = TSMetaModelAccess.getInstance(param.project).findMetaItemByName(typeName)
?.allIndexes
?.flatMap { it.keys }
?.any { it.equals(attribute, true) }
?: true

if (hasIndex) return

if (!hasIndex) {
holder.registerProblem(
param,
message("hybris.inspections.impex.ImpexUniqueAttributeWithoutIndexInspection.key", attribute, typeName)
Expand Down

0 comments on commit 564ba78

Please sign in to comment.