Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MARP-947 re structure product module content in product #108

Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class EntityConstants {
public static final String FEEDBACK = "Feedback";
public static final String PRODUCT_CUSTOM_SORT = "ProductCustomSort";
public static final String PRODUCT_JSON_CONTENT = "ProductJsonContent";
public static final String PRODUCT_MODULE_CONTENT = "ProductModuleContent";
}
ntqdinh-axonivy marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.axonivy.market.constants;

public class MongoDBConstants {

private MongoDBConstants() {
}

public static final String ID ="_id";
public static final String ADD_FIELD ="$addFields";
public static final String PRODUCT_MODULE_CONTENTS ="productModuleContents";
public static final String PRODUCT_MODULE_CONTENTS_KEY_FIELD ="productModuleContents.$id";
public static final String PRODUCT_MODULE_CONTENT ="productModuleContent";
public static final String PRODUCT_MODULE_CONTENT_QUERY ="$productModuleContents";
public static final String FILTER ="$filter";
Expand All @@ -15,6 +17,14 @@ private MongoDBConstants() {
public static final String CONDITION ="cond";
public static final String EQUAL ="$eq";
public static final String PRODUCT_MODULE_CONTENT_TAG ="$$productModuleContent.tag";
public static final String PRODUCT_MODULE_CONTENT_FILTER_TAG ="productModuleContent.tag";
public static final String PRODUCT_COLLECTION ="Product";
public static final String NEWEST_RELEASED_VERSION_QUERY = "$newestReleaseVersion";
public static final String PRODUCT_MODULE_CONTENT_DOCUMENT ="ProductModuleContent";
public static final String UNWIND = "$unwind";
public static final String LOOKUP = "$lookup";
public static final String MATCH = "$match";
public static final String FROM = "from";
public static final String LOCAL_FIELD = "localField";
public static final String FOREIGN_FIELD = "foreignField";
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serial;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class Product implements Serializable {
private int installationCount;
private Date newestPublishedDate;
private String newestReleaseVersion;
@DBRef
private List<ProductModuleContent> productModuleContents;
private List<MavenArtifact> artifacts;
private Boolean synchronizedInstallationCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serial;
import java.io.Serializable;
import java.util.Map;

import static com.axonivy.market.constants.EntityConstants.PRODUCT_MODULE_CONTENT;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Document(PRODUCT_MODULE_CONTENT)
public class ProductModuleContent implements Serializable {
@Id
private String id;
@Serial
private static final long serialVersionUID = 1L;
@Schema(description = "Target release tag", example = "v10.0.25")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.axonivy.market.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.axonivy.market.entity.ProductModuleContent;

@Repository
public interface ProductModuleContentRepository extends MongoRepository<ProductModuleContent, String> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ public Document createDocumentFilterProductModuleContentByTag(String tag) {
return loopOverProductModuleContents.append(MongoDBConstants.CONDITION, isProductModuleContentOfCurrentTag);
}

private AggregationOperation createReturnFirstModuleContentOperation() {
return context -> new Document(MongoDBConstants.ADD_FIELD,
new Document(MongoDBConstants.PRODUCT_MODULE_CONTENTS,
public AggregationOperation createLookupProductModuleContentOperation() {
return Aggregation.lookup()
.from(MongoDBConstants.PRODUCT_MODULE_CONTENT_DOCUMENT)
.localField(MongoDBConstants.PRODUCT_MODULE_CONTENTS_KEY_FIELD)
.foreignField(MongoDBConstants.ID)
.as(MongoDBConstants.PRODUCT_MODULE_CONTENTS);
}

private AggregationOperation createAddFieldProductModuleContentOperation() {
return context ->
new Document(MongoDBConstants.ADD_FIELD,
new Document(MongoDBConstants.PRODUCT_MODULE_CONTENTS,
new Document(MongoDBConstants.FILTER, createDocumentFilterProductModuleContentByTag(MongoDBConstants.NEWEST_RELEASED_VERSION_QUERY))));
}

Expand All @@ -60,13 +69,20 @@ public Product queryProductByAggregation(Aggregation aggregation) {
@Override
public Product getProductByIdAndTag(String id, String tag) {
// Create the aggregation pipeline
Aggregation aggregation = Aggregation.newAggregation(createIdMatchOperation(id), createReturnFirstMatchTagModuleContentOperation(tag));
Aggregation aggregation = Aggregation.newAggregation(
createIdMatchOperation(id),
createLookupProductModuleContentOperation(),
createReturnFirstMatchTagModuleContentOperation(tag));
return queryProductByAggregation(aggregation);
}

@Override
public Product getProductById(String id) {
Aggregation aggregation = Aggregation.newAggregation(createIdMatchOperation(id), createReturnFirstModuleContentOperation());
Aggregation aggregation = Aggregation.newAggregation(
createIdMatchOperation(id),
createLookupProductModuleContentOperation(),
createAddFieldProductModuleContentOperation()
);
return queryProductByAggregation(aggregation);
}

Expand All @@ -78,7 +94,6 @@ public List<String> getReleasedVersionsById(String id) {
return Collections.emptyList();
}
return product.getReleasedVersions();

}

public int updateInitialCount(String productId, int initialCount) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@
package com.axonivy.market.service.impl;

import static com.axonivy.market.enums.DocumentField.MARKET_DIRECTORY;
import static com.axonivy.market.enums.DocumentField.SHORT_DESCRIPTIONS;

import static java.util.Optional.ofNullable;
import static org.apache.commons.lang3.StringUtils.EMPTY;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import com.axonivy.market.util.VersionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.kohsuke.github.GHCommit;
import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHTag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.axonivy.market.constants.CommonConstants;
import com.axonivy.market.constants.GitHubConstants;
import com.axonivy.market.constants.ProductJsonConstants;
Expand All @@ -47,11 +8,7 @@
import com.axonivy.market.entity.Product;
import com.axonivy.market.entity.ProductCustomSort;
import com.axonivy.market.entity.ProductModuleContent;
import com.axonivy.market.enums.ErrorCode;
import com.axonivy.market.enums.FileType;
import com.axonivy.market.enums.Language;
import com.axonivy.market.enums.SortOption;
import com.axonivy.market.enums.TypeOption;
import com.axonivy.market.enums.*;
import com.axonivy.market.exceptions.model.InvalidParamException;
import com.axonivy.market.factory.ProductFactory;
import com.axonivy.market.github.model.GitHubFile;
Expand All @@ -62,18 +19,51 @@
import com.axonivy.market.model.ProductCustomSortRequest;
import com.axonivy.market.repository.GitHubRepoMetaRepository;
import com.axonivy.market.repository.ProductCustomSortRepository;
import com.axonivy.market.repository.ProductModuleContentRepository;
import com.axonivy.market.repository.ProductRepository;
import com.axonivy.market.service.ProductService;
import com.axonivy.market.util.VersionUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.kohsuke.github.GHCommit;
import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHTag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.*;

import static com.axonivy.market.enums.DocumentField.MARKET_DIRECTORY;
import static com.axonivy.market.enums.DocumentField.SHORT_DESCRIPTIONS;
import static java.util.Optional.ofNullable;
import static org.apache.commons.lang3.StringUtils.EMPTY;

@Log4j2
@Service
public class ProductServiceImpl implements ProductService {

private final ProductRepository productRepository;
private final ProductModuleContentRepository productModuleContentRepository;
private final GHAxonIvyMarketRepoService axonIvyMarketRepoService;
private final GHAxonIvyProductRepoService axonIvyProductRepoService;
private final GitHubRepoMetaRepository gitHubRepoMetaRepository;
Expand All @@ -95,11 +85,14 @@ public class ProductServiceImpl implements ProductService {
public static final String NON_NUMERIC_CHAR = "[^0-9.]";
private final SecureRandom random = new SecureRandom();

public ProductServiceImpl(ProductRepository productRepository, GHAxonIvyMarketRepoService axonIvyMarketRepoService,
public ProductServiceImpl(ProductRepository productRepository,
ProductModuleContentRepository productModuleContentRepository,
GHAxonIvyMarketRepoService axonIvyMarketRepoService,
GHAxonIvyProductRepoService axonIvyProductRepoService, GitHubRepoMetaRepository gitHubRepoMetaRepository,
GitHubService gitHubService, ProductCustomSortRepository productCustomSortRepository,
MongoTemplate mongoTemplate) {
this.productRepository = productRepository;
this.productModuleContentRepository = productModuleContentRepository;
this.axonIvyMarketRepoService = axonIvyMarketRepoService;
this.axonIvyProductRepoService = axonIvyProductRepoService;
this.gitHubRepoMetaRepository = gitHubRepoMetaRepository;
Expand Down Expand Up @@ -368,16 +361,22 @@ private void updateProductFromReleaseTags(Product product, GHRepository productR
}

for (GHTag ghTag : tags) {
ntqdinh-axonivy marked this conversation as resolved.
Show resolved Hide resolved
ProductModuleContent productModuleContent =
axonIvyProductRepoService.getReadmeAndProductContentsFromTag(product, productRepo, ghTag.getName());
productModuleContents.add(productModuleContent);
ProductModuleContent productModuleContent = axonIvyProductRepoService.getReadmeAndProductContentsFromTag(product,
productRepo, ghTag.getName());
if (productModuleContent != null) {
productModuleContents.add(productModuleContent);
}
String versionFromTag = VersionUtils.convertTagToVersion(ghTag.getName());
if (Objects.isNull(product.getReleasedVersions())) {
product.setReleasedVersions(new ArrayList<>());
}
product.getReleasedVersions().add(versionFromTag);
}
product.setProductModuleContents(productModuleContents);
if (!CollectionUtils.isEmpty(productModuleContents)) {
List<ProductModuleContent> savedProductModuleContents = productModuleContentRepository
ntqdinh-axonivy marked this conversation as resolved.
Show resolved Hide resolved
.saveAll(productModuleContents);
product.setProductModuleContents(savedProductModuleContents);
}
}

private void getPublishedDateFromLatestTag(Product product, GHTag lastTag) {
Expand Down Expand Up @@ -437,7 +436,6 @@ public Product fetchProductDetail(String id) {
}).orElse(null);
}


@Override
public Product fetchBestMatchProductDetail(String id, String version) {
List<String> releasedVersions = productRepository.getReleasedVersionsById(id);
Expand Down
Loading
Loading