Skip to content

Commit

Permalink
Fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
nntthuy-axonivy committed Jul 5, 2024
1 parent f41cc41 commit 72fdc20
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ public ProductModelAssembler() {
@Override
public ProductModel toModel(Product product) {
ProductModel resource = new ProductModel();
resource.add(
linkTo(methodOn(ProductDetailsController.class).findProductDetails(product.getId(), product.getType()))
.withSelfRel());
resource.add(linkTo(methodOn(ProductDetailsController.class).findProductDetails(product.getId(), product.getType()))
.withSelfRel());
return createResource(resource, product);
}

public ProductModel createResource(ProductModel model, Product product) {
model.setId(product.getId());
model.setName(product.getName());
model.setShortDescription(product.getShortDescription());
model.setNames(product.getNames());
model.setShortDescriptions(product.getShortDescriptions());
model.setType(product.getType());
model.setTags(product.getTags());
model.setLogoUrl(product.getLogoUrl());
return model;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public class GitHubConstants {
public static final String AXONIVY_MARKET_ORGANIZATION_NAME = "axonivy-market";
public static final String AXONIVY_MARKETPLACE_REPO_NAME = "market";
public static final String AXONIVY_MARKETPLACE_PATH = "market";
public static final String DEFAULT_BRANCH = "feature/MARP-463-Multilingualism-for-Website";
public static final String PRODUCT_JSON_FILE_PATH_FORMAT = "%s/product.json";
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ProductController {
private final PagedResourcesAssembler<Product> pagedResourcesAssembler;

public ProductController(ProductService service, ProductModelAssembler assembler,
PagedResourcesAssembler<Product> pagedResourcesAssembler) {
PagedResourcesAssembler<Product> pagedResourcesAssembler) {
this.service = service;
this.assembler = assembler;
this.pagedResourcesAssembler = pagedResourcesAssembler;
Expand All @@ -44,9 +44,9 @@ public ProductController(ProductService service, ProductModelAssembler assembler
@Operation(summary = "Find all products", description = "Be default system will finds product by type as 'all'")
@GetMapping()
public ResponseEntity<PagedModel<ProductModel>> findProducts(
@RequestParam(required = true, name = "type") String type,
@RequestParam(required = false, name = "keyword") String keyword,
@RequestParam(required = true, name = "language") String language, Pageable pageable) {
@RequestParam(required = true, name = "type") String type,
@RequestParam(required = false, name = "keyword") String keyword,
@RequestParam(required = true, name = "language") String language, Pageable pageable) {
Page<Product> results = service.findProducts(type, keyword, language, pageable);
if (results.isEmpty()) {
return generateEmptyPagedModel();
Expand Down Expand Up @@ -76,7 +76,7 @@ public ResponseEntity<Message> syncProducts() {
@SuppressWarnings("unchecked")
private ResponseEntity<PagedModel<ProductModel>> generateEmptyPagedModel() {
var emptyPagedModel = (PagedModel<ProductModel>) pagedResourcesAssembler
.toEmptyModel(Page.empty(), ProductModel.class);
.toEmptyModel(Page.empty(), ProductModel.class);
return new ResponseEntity<>(emptyPagedModel, HttpStatus.OK);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Date;
import java.util.List;

import com.axonivy.market.model.MultilingualismValue;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -30,9 +32,11 @@ public class Product implements Serializable {
@Id
private String id;
private String marketDirectory;
private String name;
@JsonProperty
private MultilingualismValue names;
private String version;
private String shortDescription;
@JsonProperty
private MultilingualismValue shortDescriptions;
private String logoUrl;
private Boolean listed;
private String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import static com.axonivy.market.constants.MetaConstants.*;
import static org.apache.commons.lang3.StringUtils.EMPTY;

import com.axonivy.market.enums.Language;
import com.axonivy.market.github.util.GitHubUtils;
import com.axonivy.market.model.DisplayValue;
import com.axonivy.market.model.MultilingualismValue;
import org.apache.commons.lang3.BooleanUtils;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.kohsuke.github.GHContent;
Expand All @@ -20,6 +24,7 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.CollectionUtils;

@Log4j2
@NoArgsConstructor(access = AccessLevel.PRIVATE)
Expand Down Expand Up @@ -51,13 +56,13 @@ public static Product mappingByMetaJSONFile(Product product, GHContent ghContent
}

product.setId(meta.getId());
product.setName(meta.getName());
product.setNames(mappingMultilingualismValueByMetaJSONFile(meta.getNames()));
product.setMarketDirectory(extractParentDirectory(ghContent));
product.setListed(meta.getListed());
product.setType(meta.getType());
product.setTags(meta.getTags());
product.setVersion(meta.getVersion());
product.setShortDescription(meta.getDescription());
product.setShortDescriptions(mappingMultilingualismValueByMetaJSONFile(meta.getDescriptions()));
product.setVendor(StringUtils.isBlank(meta.getVendor()) ? DEFAULT_VENDOR_NAME : meta.getVendor());
product.setVendorUrl(
StringUtils.isBlank(meta.getVendorUrl()) ? DEFAULT_VENDOR_URL : meta.getVendorUrl());
Expand All @@ -73,6 +78,21 @@ public static Product mappingByMetaJSONFile(Product product, GHContent ghContent
return product;
}

private static MultilingualismValue mappingMultilingualismValueByMetaJSONFile(List<DisplayValue> list) {
MultilingualismValue value = new MultilingualismValue();
if (!CollectionUtils.isEmpty(list)) {
for (DisplayValue name : list) {
if (Language.EN.getValue().equalsIgnoreCase(name.getLocale())) {
value.setEn(name.getValue());
} else if (Language.DE.getValue().equalsIgnoreCase(name.getLocale())) {
value.setDe(name.getValue());
}
}
}

return value;
}

private static String extractParentDirectory(GHContent ghContent) {
var path = StringUtils.defaultIfEmpty(ghContent.getPath(), EMPTY);
return path.replace(ghContent.getName(), EMPTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.axonivy.market.model.DisplayValue;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -19,8 +20,8 @@ public class Meta {
@JsonProperty("$schema")
private String schema;
private String id;
private String name;
private String description;
private List<DisplayValue> names;
private List<DisplayValue> descriptions;
private String type;
private String platformReview;
private String sourceUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.List;
import java.util.Map;

import com.axonivy.market.github.util.GitHubUtils;
import org.kohsuke.github.GHCommit;
import org.kohsuke.github.GHCommitQueryBuilder;
import org.kohsuke.github.GHCompare;
Expand All @@ -23,13 +22,13 @@
import com.axonivy.market.github.model.GitHubFile;
import com.axonivy.market.github.service.GHAxonIvyMarketRepoService;
import com.axonivy.market.github.service.GitHubService;
import com.axonivy.market.github.util.GitHubUtils;

import lombok.extern.log4j.Log4j2;

@Log4j2
@Service
public class GHAxonIvyMarketRepoServiceImpl implements GHAxonIvyMarketRepoService {
private static final String DEFAULT_BRANCH = "master";
private static final LocalDateTime INITIAL_COMMIT_DATE = LocalDateTime.of(2020, 10, 30, 0, 0);
private GHOrganization organization;
private GHRepository repository;
Expand All @@ -45,7 +44,7 @@ public Map<String, List<GHContent>> fetchAllMarketItems() {
Map<String, List<GHContent>> ghContentMap = new HashMap<>();
try {
List<GHContent> directoryContent = gitHubService.getDirectoryContent(getRepository(),
GitHubConstants.AXONIVY_MARKETPLACE_PATH);
GitHubConstants.AXONIVY_MARKETPLACE_PATH, GitHubConstants.DEFAULT_BRANCH);
for (var content : directoryContent) {
extractFileInDirectoryContent(content, ghContentMap);
}
Expand Down Expand Up @@ -85,7 +84,7 @@ public GHCommit getLastCommit(long lastCommitTime) {
}

private GHCommitQueryBuilder createQueryCommitsBuilder(long lastCommitTime) {
return getRepository().queryCommits().since(lastCommitTime).from(DEFAULT_BRANCH);
return getRepository().queryCommits().since(lastCommitTime).from(GitHubConstants.DEFAULT_BRANCH);
}

@Override
Expand Down Expand Up @@ -136,4 +135,4 @@ public GHRepository getRepository() {
return repository;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public interface ProductRepository extends MongoRepository<Product, String> {
@Query("{'marketDirectory': {$regex : ?0, $options: 'i'}}")
Product findByMarketDirectoryRegex(String search);

@Query("{ $and: [ { $or: [ { 'name': { $regex: ?0, $options: 'i' } }, { 'shortDescription': { $regex: ?0, $options: 'i' } } ] }, { 'type': ?1 } ] }")
Page<Product> searchByKeywordAndType(String keyword, String type, Pageable unifiedPageabe);
@Query("{ $and: [ { $or: [ { 'names.?': { $regex: ?0, $options: 'i' } }, { 'shortDescriptions.?': { $regex: ?0, $options: 'i' } } ] }, { 'type': ?1 } ] }")
Page<Product> searchByKeywordAndType(String keyword, String type, String language, Pageable unifiedPageabe);

@Query("{ $or: [ { 'name': { $regex: ?0, $options: 'i' } }, { 'shortDescription': { $regex: ?0, $options: 'i' } } ] }")
Page<Product> searchByNameOrShortDescriptionRegex(String keyword, Pageable unifiedPageabe);
@Query("{ $or: [ { 'names.?1': { $regex: ?0, $options: 'i' } }, { 'shortDescriptions.?1': { $regex: ?0, $options: 'i' } } ] }")
Page<Product> searchByNameOrShortDescriptionRegex(String keyword, String language, Pageable unifiedPageabe);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.axonivy.market.entity.Product;

public interface ProductService {
Page<Product> findProducts(String type, String keyword, Pageable pageable);
Page<Product> findProducts(String type, String keyword, String language, Pageable pageable);

boolean syncLatestDataFromMarketRepo();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,27 @@ public ProductServiceImpl(ProductRepository productRepository, GHAxonIvyMarketRe
}

@Override
public Page<Product> findProducts(String type, String keyword, Pageable pageable) {
public Page<Product> findProducts(String type, String keyword, String language, Pageable pageable) {
final var typeOption = TypeOption.of(type);
final var searchPageable = refinePagination(pageable);
final var searchPageable = refinePagination(language, pageable);
Page<Product> result = Page.empty();
switch (typeOption) {
case ALL:
if (StringUtils.isBlank(keyword)) {
result = productRepository.findAll(searchPageable);
} else {
result = productRepository.searchByNameOrShortDescriptionRegex(keyword, searchPageable);
}
break;
case CONNECTORS, UTILITIES, SOLUTIONS:
if (StringUtils.isBlank(keyword)) {
result = productRepository.findByType(typeOption.getCode(), searchPageable);
} else {
result = productRepository.searchByKeywordAndType(keyword, typeOption.getCode(), searchPageable);
}
break;
default:
break;
case ALL:
if (StringUtils.isBlank(keyword)) {
result = productRepository.findAll(searchPageable);
} else {
result = productRepository.searchByNameOrShortDescriptionRegex(keyword, language, searchPageable);
}
break;
case CONNECTORS, UTILITIES, SOLUTIONS:
if (StringUtils.isBlank(keyword)) {
result = productRepository.findByType(typeOption.getCode(), searchPageable);
} else {
result = productRepository.searchByKeywordAndType(keyword, typeOption.getCode(), language, searchPageable);
}
break;
default:
break;
}
return result;
}
Expand Down Expand Up @@ -132,8 +132,7 @@ private void updateLatestChangeToProductsFromGithubRepo() {
Map<String, List<GitHubFile>> groupGitHubFiles = new HashMap<>();
for (var file : gitHubFileChanges) {
String filePath = file.getFileName();
var parentPath = filePath.replace(FileType.META.getFileName(), EMPTY).replace(FileType.LOGO.getFileName(),
EMPTY);
var parentPath = filePath.replace(FileType.META.getFileName(), EMPTY).replace(FileType.LOGO.getFileName(), EMPTY);
var files = groupGitHubFiles.getOrDefault(parentPath, new ArrayList<>());
files.add(file);
groupGitHubFiles.putIfAbsent(parentPath, files);
Expand All @@ -144,8 +143,8 @@ private void updateLatestChangeToProductsFromGithubRepo() {
Product product = new Product();
GHContent fileContent;
try {
fileContent = gitHubService.getGHContent(axonIvyMarketRepoService.getRepository(),
file.getFileName());
fileContent = gitHubService.getGHContent(axonIvyMarketRepoService.getRepository(), file.getFileName(),
GitHubConstants.DEFAULT_BRANCH);
} catch (IOException e) {
log.error("Get GHContent failed: ", e);
continue;
Expand All @@ -165,44 +164,44 @@ private void updateLatestChangeToProductsFromGithubRepo() {
private void modifyProductLogo(String parentPath, GitHubFile file, Product product, GHContent fileContent) {
Product result = null;
switch (file.getStatus()) {
case MODIFIED, ADDED:
result = productRepository.findByMarketDirectoryRegex(parentPath);
if (result != null) {
result.setLogoUrl(GitHubUtils.getDownloadUrl(fileContent));
productRepository.save(result);
}
break;
case REMOVED:
result = productRepository.findByLogoUrl(product.getLogoUrl());
if (result != null) {
productRepository.deleteById(result.getId());
}
break;
default:
break;
case MODIFIED, ADDED:
result = productRepository.findByMarketDirectoryRegex(parentPath);
if (result != null) {
result.setLogoUrl(GitHubUtils.getDownloadUrl(fileContent));
productRepository.save(result);
}
break;
case REMOVED:
result = productRepository.findByLogoUrl(product.getLogoUrl());
if (result != null) {
productRepository.deleteById(result.getId());
}
break;
default:
break;
}
}

private void modifyProductByMetaContent(GitHubFile file, Product product) {
switch (file.getStatus()) {
case MODIFIED, ADDED:
productRepository.save(product);
break;
case REMOVED:
productRepository.deleteById(product.getId());
break;
default:
break;
case MODIFIED, ADDED:
productRepository.save(product);
break;
case REMOVED:
productRepository.deleteById(product.getId());
break;
default:
break;
}
}

private Pageable refinePagination(Pageable pageable) {
private Pageable refinePagination(String language, Pageable pageable) {
PageRequest pageRequest = (PageRequest) pageable;
if (pageable != null && pageable.getSort() != null) {
if (pageable != null) {
List<Order> orders = new ArrayList<>();
for (var sort : pageable.getSort()) {
final var sortOption = SortOption.of(sort.getProperty());
Order order = new Order(sort.getDirection(), sortOption.getCode());
Order order = new Order(sort.getDirection(), sortOption.getCode(language));
orders.add(order);
}
pageRequest = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(orders));
Expand Down Expand Up @@ -236,9 +235,7 @@ private Page<Product> syncProductsFromGitHubRepo() {
updateProductFromReleaseTags(product);
}
products.add(product);

});

if (!products.isEmpty()) {
productRepository.saveAll(products);
}
Expand Down

0 comments on commit 72fdc20

Please sign in to comment.