3535import org .eclipse .sw360 .datahandler .permissions .PermissionUtils ;
3636import org .eclipse .sw360 .datahandler .permissions .ProjectPermissions ;
3737import org .eclipse .sw360 .datahandler .thrift .*;
38- import org .eclipse .sw360 .datahandler .thrift .attachments .Attachment ;
39- import org .eclipse .sw360 .datahandler .thrift .attachments .AttachmentContent ;
38+ import org .eclipse .sw360 .datahandler .thrift .attachments .*;
4039import org .eclipse .sw360 .datahandler .thrift .changelogs .ChangeLogs ;
4140import org .eclipse .sw360 .datahandler .thrift .changelogs .Operation ;
4241import org .eclipse .sw360 .datahandler .thrift .components .*;
8079import static org .eclipse .sw360 .datahandler .common .SW360Assert .assertId ;
8180import static org .eclipse .sw360 .datahandler .common .SW360Assert .assertNotNull ;
8281import static org .eclipse .sw360 .datahandler .common .SW360Assert .fail ;
83- import static org .eclipse .sw360 .datahandler .common .SW360ConfigKeys .IS_PACKAGE_PORTLET_ENABLED ;
84- import static org .eclipse .sw360 .datahandler .common .SW360ConfigKeys .MAINLINE_STATE_ENABLED_FOR_USER ;
82+ import static org .eclipse .sw360 .datahandler .common .SW360ConfigKeys .*;
8583import static org .eclipse .sw360 .datahandler .common .SW360Utils .getBUFromOrganisation ;
8684import static org .eclipse .sw360 .datahandler .common .SW360Utils .getCreatedOn ;
8785import static org .eclipse .sw360 .datahandler .common .SW360Utils .printName ;
@@ -154,6 +152,7 @@ public class ProjectDatabaseHandler extends AttachmentAwareDatabaseHandler {
154152 Project ._Fields .LICENSE_INFO_HEADER_TEXT );
155153 private Map <String , Project > cachedAllProjectsIdMap ;
156154 private Instant cachedAllProjectsIdMapLoadingInstant ;
155+ private final ThriftClients thriftClients ;
157156
158157 public ProjectDatabaseHandler (Cloudant client , String dbName , String attachmentDbName ) throws MalformedURLException {
159158 this (client , dbName , attachmentDbName , new ProjectModerator (),
@@ -204,6 +203,8 @@ public ProjectDatabaseHandler(Cloudant client, String dbName, String attachmentD
204203 this .packageDatabaseHandler = packageDatabaseHandler ;
205204 DatabaseConnectorCloudant dbChangelogs = new DatabaseConnectorCloudant (client , DatabaseSettings .COUCH_DB_CHANGE_LOGS );
206205 this .dbHandlerUtil = new DatabaseHandlerUtil (dbChangelogs );
206+
207+ thriftClients = new ThriftClients ();
207208 }
208209
209210 /////////////////////
@@ -425,6 +426,10 @@ public AddDocumentRequestSummary addProject(Project project, User user) throws S
425426 // Add project to database and return ID
426427 repository .add (project );
427428
429+ if (SW360Utils .readConfig (INHERIT_ATTACHMENT_USAGES , false ) && !CommonUtils .isNullOrEmptyMap (project .getLinkedProjects ())) {
430+ saveAttachmentUsages (project );
431+ }
432+
428433 dbHandlerUtil .addChangeLogs (project , null , user .getEmail (), Operation .CREATE , null , Lists .newArrayList (),
429434 null , null );
430435 sendMailNotificationsForNewProject (project , user .getEmail ());
@@ -480,6 +485,16 @@ public RequestStatus updateProject(Project project, User user, boolean forceUpda
480485 updateProjectDependentLinkedFields (project , actual );
481486 project .unsetVendor ();
482487 updateModifiedFields (project , user .getEmail ());
488+
489+ if (SW360Utils .readConfig (INHERIT_ATTACHMENT_USAGES , false )) {
490+ Set <String > newLinkedProjects = CommonUtils .isNullOrEmptyMap (project .getLinkedProjects ()) ? new HashSet <>() : new HashSet <>(project .getLinkedProjects ().keySet ());
491+ Set <String > actualLinkedProjects = CommonUtils .isNullOrEmptyMap (actual .getLinkedProjects ()) ? new HashSet <>() : actual .getLinkedProjects ().keySet ();
492+
493+ newLinkedProjects .removeAll (actualLinkedProjects );
494+ if (!newLinkedProjects .isEmpty ()) {
495+ saveAttachmentUsages (project );
496+ }
497+ }
483498 repository .update (project );
484499
485500 List <ChangeLogs > referenceDocLogList =new LinkedList <>();
@@ -504,6 +519,62 @@ public RequestStatus updateProject(Project project, User user, boolean forceUpda
504519 }
505520 }
506521
522+ private void saveAttachmentUsages (Project project ) {
523+ AttachmentService .Iface attachmentClient = thriftClients .makeAttachmentClient ();
524+ String projectId = project .getId ();
525+ List <String > projectPaths = new ArrayList <>();
526+
527+ buildProjectPaths (project ,null ,projectPaths );
528+ projectPaths .remove (project .getId ());
529+ try {
530+ if (!projectPaths .isEmpty ()) {
531+ List <AttachmentUsage > newAttachmentUsages = parseAttachmentUsages (projectPaths ,projectId );
532+ attachmentClient .makeAttachmentUsages (newAttachmentUsages );
533+ }
534+ } catch (TException e ) {
535+ log .error ("Saving attachment usages for project " + projectId + " failed" , e );
536+ }
537+ }
538+
539+ void buildProjectPaths (Project project , String parentPath , List <String > results ) {
540+ String currentPath = parentPath == null ? project .getId () : parentPath + ":" + project .getId ();
541+ if (CommonUtils .isNullOrEmptyMap (project .getLinkedProjects ())) {
542+ results .add (currentPath );
543+ }
544+ else {
545+ for (Map .Entry <String ,ProjectProjectRelationship > entry : project .getLinkedProjects ().entrySet ()) {
546+ buildProjectPaths (repository .get (entry .getKey ()), currentPath , results );
547+ }
548+ results .add (currentPath );
549+ }
550+ }
551+
552+ private List <AttachmentUsage > parseAttachmentUsages (List <String > projectPaths , String projectId ) {
553+ List <AttachmentUsage > result = new ArrayList <>();
554+ try {
555+ for (String projectPath : projectPaths ) {
556+ String [] pathArray = projectPath .split (":" );
557+ String subProjectId = pathArray [pathArray .length -1 ];
558+ List <AttachmentUsage > subProjectAttachmentUsages = thriftClients .makeAttachmentClient ().getUsedAttachments (Source .projectId (subProjectId ), null );
559+
560+ for (AttachmentUsage usage : subProjectAttachmentUsages ) {
561+ String releaseId = usage .getOwner ().getReleaseId ();
562+ String attachmentContentId = usage .getAttachmentContentId ();
563+ AttachmentUsage newUsage = new AttachmentUsage (Source .releaseId (releaseId ), attachmentContentId , Source .projectId (projectId ));
564+ final UsageData usageData ;
565+ LicenseInfoUsage licenseInfoUsage = new LicenseInfoUsage (Collections .emptySet ());
566+ licenseInfoUsage .setProjectPath (projectPath );
567+ usageData = UsageData .licenseInfo (licenseInfoUsage );
568+ newUsage .setUsageData (usageData );
569+ result .add (newUsage );
570+ }
571+ }
572+ } catch (TException e ) {
573+ log .error ("Saving attachment usages for project " + projectId + " failed" , e );
574+ }
575+ return result ;
576+ }
577+
507578 private void setRequestedDateAndTrimComment (Project project , Project actual , User user ) {
508579 Set <String > actualReleaseIds = null ;
509580 if (Objects .nonNull (actual ) && Objects .nonNull (actual .getReleaseIdToUsage ())) {
@@ -862,7 +933,7 @@ private boolean isLinkedReleasesUpdateFromLinkedPackagesFailed(Project updatedPr
862933 final ProjectReleaseRelationship releaseRelation = new ProjectReleaseRelationship (ReleaseRelationship .UNKNOWN , MainlineState .OPEN );
863934 if (CommonUtils .isNotEmpty (linkedPacakgeIds )) {
864935 try {
865- PackageService .Iface packageClient = new ThriftClients () .makePackageClient ();
936+ PackageService .Iface packageClient = thriftClients .makePackageClient ();
866937 List <Package > addedPackages = packageClient .getPackageByIds (linkedPacakgeIds );
867938
868939 Map <String , ProjectReleaseRelationship > releaseIdToUsageMap = addedPackages .stream ().map (Package ::getReleaseId )
@@ -894,7 +965,7 @@ private boolean isLinkedReleasesUpdateFromLinkedPackagesFailed(Project updatedPr
894965
895966 if (CommonUtils .isNotEmpty (unlinkedPacakgeIds )) {
896967 try {
897- PackageService .Iface packageClient = new ThriftClients () .makePackageClient ();
968+ PackageService .Iface packageClient = thriftClients .makePackageClient ();
898969 List <Package > removedPackages = packageClient .getPackageWithReleaseByPackageIds (unlinkedPacakgeIds );
899970
900971 Map <String , Set <String >> releaseIdToPackageIdsMap = removedPackages .stream ()
@@ -2064,7 +2135,6 @@ private void propagateSecurityResponsiblesToLinkedProjects(Set<String> responsib
20642135 }
20652136
20662137 private Map <String , String > getGidsByEmail () throws TException {
2067- ThriftClients thriftClients = new ThriftClients ();
20682138 UserService .Iface userClient = thriftClients .makeUserClient ();
20692139 Map <String , String > gidByEmail = new HashMap <>();
20702140 userClient
@@ -2386,7 +2456,6 @@ public ByteBuffer getReportDataStream(User user, boolean extendedByReleases, Str
23862456 }
23872457
23882458 private ProjectExporter getProjectExporterObject (List <Project > documents , User user , boolean extendedByReleases ) throws SW360Exception {
2389- ThriftClients thriftClients = new ThriftClients ();
23902459 return new ProjectExporter (thriftClients .makeComponentClient (),
23912460 thriftClients .makeProjectClient (), user , documents , extendedByReleases );
23922461 }
@@ -2409,7 +2478,7 @@ public String getReportInEmail(User user,
24092478
24102479 private List <Project > getProjectDetailsBasedOnId (User user , String projectId ) throws TException {
24112480 final Collection <ProjectLink > projectLinks = SW360Utils .getLinkedProjectsAsFlatList (projectId , true ,
2412- new ThriftClients () , log , user );
2481+ thriftClients , log , user );
24132482 if (projectLinks .isEmpty ()) {
24142483 throw new TException ("For the projectId : " + projectId
24152484 + ", No data available. Please check the projectId and try again." );
@@ -2419,7 +2488,6 @@ private List<Project> getProjectDetailsBasedOnId(User user, String projectId) th
24192488 }
24202489
24212490 public ByteBuffer downloadExcel (User user , boolean extendedByReleases , String token ) throws SW360Exception {
2422- ThriftClients thriftClients = new ThriftClients ();
24232491 ProjectExporter exporter = new ProjectExporter (thriftClients .makeComponentClient (),
24242492 thriftClients .makeProjectClient (), user , extendedByReleases );
24252493 try {
0 commit comments