@@ -1501,16 +1501,10 @@ private EntityMutationResponse createOrUpdate(EntityStream entityStream, boolean
1501
1501
}
1502
1502
1503
1503
AtlasEntity diffEntity = reqContext .getDifferentialEntity (entity .getGuid ());
1504
-
1505
- if (diffEntity != null &&
1506
- MapUtils .isNotEmpty (diffEntity .getRelationshipAttributes ()) &&
1507
- diffEntity .getRelationshipAttributes ().containsKey ("meanings" ) &&
1508
- diffEntity .getRelationshipAttributes ().size () == 1 &&
1509
- MapUtils .isEmpty (diffEntity .getAttributes ()) &&
1510
- MapUtils .isEmpty (diffEntity .getCustomAttributes ()) &&
1511
- MapUtils .isEmpty (diffEntity .getBusinessAttributes ()) &&
1512
- CollectionUtils .isEmpty (diffEntity .getClassifications ()) &&
1513
- CollectionUtils .isEmpty (diffEntity .getLabels ())) {
1504
+ boolean skipAuthBaseConditions = diffEntity != null && MapUtils .isEmpty (diffEntity .getCustomAttributes ()) && MapUtils .isEmpty (diffEntity .getBusinessAttributes ()) && CollectionUtils .isEmpty (diffEntity .getClassifications ()) && CollectionUtils .isEmpty (diffEntity .getLabels ());
1505
+ boolean skipAuthMeaningsUpdate = diffEntity != null && MapUtils .isNotEmpty (diffEntity .getRelationshipAttributes ()) && diffEntity .getRelationshipAttributes ().containsKey ("meanings" ) && diffEntity .getRelationshipAttributes ().size () == 1 && MapUtils .isEmpty (diffEntity .getAttributes ());
1506
+ boolean skipAuthStarredDetailsUpdate = diffEntity != null && MapUtils .isEmpty (diffEntity .getRelationshipAttributes ()) && MapUtils .isNotEmpty (diffEntity .getAttributes ()) && diffEntity .getAttributes ().size () == 3 && diffEntity .getAttributes ().containsKey (ATTR_STARRED_BY ) && diffEntity .getAttributes ().containsKey (ATTR_STARRED_COUNT ) && diffEntity .getAttributes ().containsKey (ATTR_STARRED_DETAILS_LIST );
1507
+ if (skipAuthBaseConditions && (skipAuthMeaningsUpdate || skipAuthStarredDetailsUpdate )) {
1514
1508
//do nothing, only diff is relationshipAttributes.meanings, allow update
1515
1509
} else {
1516
1510
AtlasAuthorizationUtils .verifyUpdateEntityAccess (typeRegistry , entityHeader ,"update entity: type=" + entity .getTypeName ());
@@ -1596,6 +1590,8 @@ private EntityMutationContext preCreateOrUpdate(EntityStream entityStream, Entit
1596
1590
1597
1591
AtlasVertex vertex = getResolvedEntityVertex (discoveryContext , entity );
1598
1592
1593
+ autoUpdateStarredDetailsAttributes (entity , vertex );
1594
+
1599
1595
try {
1600
1596
if (vertex != null ) {
1601
1597
if (!isPartialUpdate ) {
@@ -1692,6 +1688,102 @@ private EntityMutationContext preCreateOrUpdate(EntityStream entityStream, Entit
1692
1688
return context ;
1693
1689
}
1694
1690
1691
+ private void autoUpdateStarredDetailsAttributes (AtlasEntity entity , AtlasVertex vertex ) {
1692
+
1693
+ MetricRecorder metric = RequestContext .get ().startMetricRecord ("autoUpdateStarredDetailsAttributes" );
1694
+
1695
+ Boolean starEntityForUser = entity .getStarred ();
1696
+
1697
+ if (starEntityForUser != null ) {
1698
+
1699
+ long requestTime = RequestContext .get ().getRequestTime ();
1700
+ String requestUser = RequestContext .get ().getUser ();
1701
+
1702
+ Set <String > starredBy = new HashSet <>();
1703
+ Set <AtlasStruct > starredDetailsList = new HashSet <>();
1704
+ int starredCount = 0 ;
1705
+
1706
+ if (vertex != null ) {
1707
+ Set <String > vertexStarredBy = vertex .getMultiValuedSetProperty (ATTR_STARRED_BY , String .class );
1708
+ if (vertexStarredBy != null ) {
1709
+ starredBy = vertexStarredBy ;
1710
+ }
1711
+
1712
+ Iterable <AtlasEdge > starredDetailsEdges = vertex .getEdges (AtlasEdgeDirection .OUT , "__" + ATTR_STARRED_DETAILS_LIST );
1713
+ for (AtlasEdge starredDetailsEdge : starredDetailsEdges ) {
1714
+ AtlasVertex starredDetailsVertex = starredDetailsEdge .getInVertex ();
1715
+ String assetStarredBy = starredDetailsVertex .getProperty (ATTR_ASSET_STARRED_BY , String .class );
1716
+ Long assetStarredAt = starredDetailsVertex .getProperty (ATTR_ASSET_STARRED_AT , Long .class );
1717
+ AtlasStruct starredDetails = getStarredDetailsStruct (assetStarredBy , assetStarredAt );
1718
+ starredDetailsList .add (starredDetails );
1719
+ }
1720
+
1721
+ starredCount = starredBy .size ();
1722
+ }
1723
+
1724
+ if (starEntityForUser ) {
1725
+ addUserToStarredAttributes (requestUser , requestTime , starredBy , starredDetailsList );
1726
+ } else {
1727
+ removeUserFromStarredAttributes (requestUser , starredBy , starredDetailsList );
1728
+ }
1729
+
1730
+ // Update entity attributes
1731
+ if (starredBy .size () != starredCount ) {
1732
+ entity .setAttribute (ATTR_STARRED_BY , starredBy );
1733
+ entity .setAttribute (ATTR_STARRED_DETAILS_LIST , starredDetailsList );
1734
+ entity .setAttribute (ATTR_STARRED_COUNT , starredBy .size ());
1735
+ }
1736
+
1737
+ }
1738
+
1739
+ RequestContext .get ().endMetricRecord (metric );
1740
+ }
1741
+
1742
+ private void addUserToStarredAttributes (String requestUser , long requestTime , Set <String > starredBy , Set <AtlasStruct > starredDetailsList ) {
1743
+ //Check and update starredBy Attribute
1744
+ if (!starredBy .contains (requestUser )){
1745
+ starredBy .add (requestUser );
1746
+ }
1747
+
1748
+ //Check and update starredDetailsList Attribute
1749
+ boolean isStarredDetailsListUpdated = false ;
1750
+ for (AtlasStruct starredDetails : starredDetailsList ) {
1751
+ String assetStarredBy = (String ) starredDetails .getAttribute (ATTR_ASSET_STARRED_BY );
1752
+ if (assetStarredBy .equals (requestUser )) {
1753
+ starredDetails .setAttribute (ATTR_ASSET_STARRED_AT , requestTime );
1754
+ isStarredDetailsListUpdated = true ;
1755
+ break ;
1756
+ }
1757
+ }
1758
+ if (!isStarredDetailsListUpdated ) {
1759
+ AtlasStruct starredDetails = getStarredDetailsStruct (requestUser , requestTime );
1760
+ starredDetailsList .add (starredDetails );
1761
+ }
1762
+ }
1763
+
1764
+ private void removeUserFromStarredAttributes (String requestUser , Set <String > starredBy , Set <AtlasStruct > starredDetailsList ) {
1765
+ //Check and update starredBy Attribute
1766
+ if (starredBy .contains (requestUser )){
1767
+ starredBy .remove (requestUser );
1768
+ }
1769
+
1770
+ for (AtlasStruct starredDetails : starredDetailsList ) {
1771
+ String assetStarredBy = (String ) starredDetails .getAttribute (ATTR_ASSET_STARRED_BY );
1772
+ if (assetStarredBy .equals (requestUser )) {
1773
+ starredDetailsList .remove (starredDetails );
1774
+ break ;
1775
+ }
1776
+ }
1777
+ }
1778
+
1779
+ private AtlasStruct getStarredDetailsStruct (String assetStarredBy , long assetStarredAt ) {
1780
+ AtlasStruct starredDetails = new AtlasStruct ();
1781
+ starredDetails .setTypeName (STRUCT_STARRED_DETAILS );
1782
+ starredDetails .setAttribute (ATTR_ASSET_STARRED_BY , assetStarredBy );
1783
+ starredDetails .setAttribute (ATTR_ASSET_STARRED_AT , assetStarredAt );
1784
+ return starredDetails ;
1785
+ }
1786
+
1695
1787
public PreProcessor getPreProcessor (String typeName ) {
1696
1788
PreProcessor preProcessor = null ;
1697
1789
@@ -1983,6 +2075,12 @@ private void flushAutoUpdateAttributes(AtlasEntity entity, AtlasEntityType entit
1983
2075
}
1984
2076
}
1985
2077
2078
+ // for (String attrName : entityType.getAllAttributes().keySet()) {
2079
+ // if (ATTR_STARRED_BY.equals(attrName) || ATTR_STARRED_COUNT.equals(attrName) || ATTR_STARRED_DETAILS_LIST.equals(attrName)) {
2080
+ // flushAttributes.add(attrName);
2081
+ // }
2082
+ // }
2083
+
1986
2084
flushAttributes .forEach (entity ::removeAttribute );
1987
2085
}
1988
2086
}
0 commit comments