Skip to content

Commit a5b0a84

Browse files
authored
Merge pull request #167 from jerry-goodman/develop
Improve the material api and fix some bugs
2 parents 0c7c917 + 34663fc commit a5b0a84

File tree

22 files changed

+205
-115
lines changed

22 files changed

+205
-115
lines changed

server/openblocks-domain/src/main/java/com/openblocks/domain/material/model/MaterialMeta.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ public class MaterialMeta extends HasIdAndAuditing {
1515
private String filename;
1616
private String orgId;
1717
private long size;// in bytes
18+
private MaterialType type;
1819
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.openblocks.domain.material.model;
2+
3+
public enum MaterialType {
4+
5+
COMMON, LOGO, FAVICON
6+
}

server/openblocks-domain/src/main/java/com/openblocks/domain/material/repository/MaterialMateRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.stereotype.Repository;
55

66
import com.openblocks.domain.material.model.MaterialMeta;
7+
import com.openblocks.domain.material.model.MaterialType;
78

89
import reactor.core.publisher.Flux;
910
import reactor.core.publisher.Mono;
@@ -13,5 +14,9 @@ public interface MaterialMateRepository extends ReactiveMongoRepository<Material
1314

1415
Flux<MaterialMeta> findByOrgId(String orgId);
1516

17+
Flux<MaterialMeta> findByOrgIdAndType(String orgId, MaterialType type);
18+
19+
Flux<MaterialMeta> findByOrgIdAndFilenameAndType(String orgId, String filename, MaterialType type);
20+
1621
Mono<Boolean> existsByOrgIdAndFilename(String orgId, String filename);
1722
}

server/openblocks-domain/src/main/java/com/openblocks/domain/organization/model/Organization.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public OrganizationCommonSettings getCommonSettings() {
8484
public static class OrganizationCommonSettings extends HashMap<String, Object> {
8585
public static final String USER_EXTRA_TRANSFORMER = "userExtraTransformer";
8686
public static final String USER_EXTRA_TRANSFORMER_UPDATE_TIME = "userExtraTransformer_updateTime";
87+
88+
// custom branding configs
89+
public static final String CUSTOM_BRANDING_KEY = "branding";
8790
}
8891

8992
public long getCreateTime() {

server/openblocks-domain/src/main/java/com/openblocks/domain/organization/service/OrganizationService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
public interface OrganizationService {
1717

18+
@PossibleEmptyMono
19+
Mono<Organization> getOrganizationInEnterpriseMode();
20+
1821
Mono<Organization> create(Organization organization, String creatorUserId);
1922

2023
Mono<Organization> createDefault(User user);

server/openblocks-domain/src/main/java/com/openblocks/domain/organization/service/OrganizationServiceImpl.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.openblocks.domain.organization.model.QOrganization;
3030
import com.openblocks.domain.organization.repository.OrganizationRepository;
3131
import com.openblocks.domain.user.model.User;
32+
import com.openblocks.infra.annotation.PossibleEmptyMono;
3233
import com.openblocks.infra.mongo.MongoUpsertHelper;
3334
import com.openblocks.sdk.config.CommonConfig;
3435
import com.openblocks.sdk.config.dynamic.Conf;
@@ -91,7 +92,7 @@ public Mono<Organization> createDefault(User user) {
9192
return create(organization, user.getId());
9293
}
9394
// enterprise mode
94-
return joinOrgForEnterpriseMode(user.getId())
95+
return joinOrganizationInEnterpriseMode(user.getId())
9596
.flatMap(join -> {
9697
if (Boolean.TRUE.equals(join)) {
9798
return Mono.empty();
@@ -101,17 +102,26 @@ public Mono<Organization> createDefault(User user) {
101102
});
102103
}
103104

104-
private Mono<Boolean> joinOrgForEnterpriseMode(String userId) {
105+
private Mono<Boolean> joinOrganizationInEnterpriseMode(String userId) {
106+
return getOrganizationInEnterpriseMode()
107+
.flatMap(organization -> orgMemberService.addMember(organization.getId(), userId, MemberRole.MEMBER))
108+
.defaultIfEmpty(false);
109+
}
110+
111+
@Override
112+
@PossibleEmptyMono
113+
public Mono<Organization> getOrganizationInEnterpriseMode() {
114+
if (commonConfig.getWorkspace().getMode() == WorkspaceMode.SAAS) {
115+
return Mono.empty();
116+
}
105117
return Mono.defer(() -> {
106118
String enterpriseOrgId = commonConfig.getWorkspace().getEnterpriseOrgId();
107119
if (StringUtils.isNotBlank(enterpriseOrgId)) {
108120
return repository.findById(enterpriseOrgId);
109121
}
110122
return Mono.empty();
111123
})
112-
.switchIfEmpty(repository.findAll().next())
113-
.flatMap(organization -> orgMemberService.addMember(organization.getId(), userId, MemberRole.MEMBER))
114-
.defaultIfEmpty(false);
124+
.switchIfEmpty(repository.findAll().next());
115125
}
116126

117127
@Override

server/openblocks-infra/src/main/java/com/openblocks/infra/birelation/BiRelationService.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public Mono<Boolean> upsert(BiRelation biRelation) {
7777
Preconditions.checkArgument(isNotBlank(biRelation.getTargetId()));
7878
Preconditions.checkArgument(biRelation.getBizType() != null);
7979

80-
Query query = buildQuery(biRelation.getSourceId(), biRelation.getTargetId(), biRelation.getBizType());
81-
return mongoUpsertHelper.upsert(biRelation, query);
80+
Criteria criteria = buildCriteria(biRelation.getSourceId(), biRelation.getTargetId(), biRelation.getBizType());
81+
return mongoUpsertHelper.upsert(biRelation, criteria);
8282
}
8383

8484
public Flux<BiRelation> getBySourceIds(BiRelationBizType bizType, Collection<String> sourceIds) {
@@ -169,11 +169,14 @@ public Mono<Long> countByTargetId(BiRelationBizType bizType, String targetId) {
169169
return biRelationRepository.countByBizTypeAndTargetId(bizType, targetId);
170170
}
171171

172-
private Query buildQuery(String sourceId, String targetId, BiRelationBizType bizType) {
173-
Criteria criteria = where(BIZ_TYPE).is(bizType)
172+
private Criteria buildCriteria(String sourceId, String targetId, BiRelationBizType bizType) {
173+
return where(BIZ_TYPE).is(bizType)
174174
.and(SOURCE_ID).is(sourceId)
175175
.and(TARGET_ID).is(targetId);
176-
return new Query(criteria);
176+
}
177+
178+
private Query buildQuery(String sourceId, String targetId, BiRelationBizType bizType) {
179+
return new Query(buildCriteria(sourceId, targetId, bizType));
177180
}
178181

179182
public Mono<BiRelation> getById(String id) {

server/openblocks-infra/src/main/java/com/openblocks/infra/constant/NewUrl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.openblocks.infra.constant;
22

33
public final class NewUrl {
4+
5+
private NewUrl() {
6+
}
7+
48
public static final String PREFIX = "/api";
59
public static final String ORGANIZATION_URL = PREFIX + "/organizations";
610
public static final String DATASOURCE_URL = PREFIX + "/datasources";
@@ -21,5 +25,7 @@ public final class NewUrl {
2125
public static final String LIBRARY_QUERY_RECORD_URL = PREFIX + "/library-query-records";
2226

2327
public static final String FOLDER_URL = PREFIX + "/folders";
28+
29+
public static final String GITHUB_STAR = PREFIX + "/misc/github-star";
2430
public static final String MATERIAL_URL = PREFIX + "/materials";
2531
}

server/openblocks-infra/src/main/java/com/openblocks/infra/mongo/MongoUpsertHelper.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,39 @@ public <T extends HasIdAndAuditing> Mono<T> upsertWithAuditingParams(T newResour
8989
.switchIfEmpty(Mono.defer(() -> reactiveMongoTemplate.save(newResource)));
9090
}
9191

92+
/**
93+
* reactiveMongoTemplate#upsert is not used because createdAt/createdBy/updatedAt/updatedBy params cannot be set here
94+
*/
95+
@SuppressWarnings("unchecked")
96+
public <T extends HasIdAndAuditing> Mono<T> upsertWithAuditingParams(T newResource, Criteria criteria) {
97+
return reactiveMongoTemplate.findOne(new Query(criteria), (Class<T>) newResource.getClass())
98+
.flatMap(existingResource -> {
99+
newResource.setId(existingResource.getId());
100+
newResource.setCreatedAt(existingResource.getCreatedAt());
101+
newResource.setCreatedBy(existingResource.getCreatedBy());
102+
return reactiveMongoTemplate.save(newResource);
103+
})
104+
.switchIfEmpty(Mono.defer(() -> reactiveMongoTemplate.save(newResource)));
105+
}
106+
92107
/**
93108
* used for createdAt/createdBy/updatedAt/updatedBy is not required
94109
*/
95110
public <T> Mono<Boolean> upsert(T newResource, String uniqueKeyName, String uniqueKeyValue) {
96-
Query query = new Query(Criteria.where(uniqueKeyName).is(uniqueKeyValue));
97-
return upsert(newResource, query);
111+
return upsert(newResource, Criteria.where(uniqueKeyName).is(uniqueKeyValue));
98112
}
99113

100114
public Mono<Boolean> upsert(Update update, String uniqueKeyName, String uniqueKeyValue, Class<?> collection) {
101-
Query query = new Query(Criteria.where(uniqueKeyName).is(uniqueKeyValue));
102-
return upsert(update, query, collection);
115+
return upsert(update, Criteria.where(uniqueKeyName).is(uniqueKeyValue), collection);
103116
}
104117

105-
public <T> Mono<Boolean> upsert(T newResource, Query query) {
118+
public <T> Mono<Boolean> upsert(T newResource, Criteria criteria) {
106119
Update update = convertToUpdate(newResource);
107-
return upsert(update, query, newResource.getClass());
120+
return upsert(update, criteria, newResource.getClass());
108121
}
109122

110-
public Mono<Boolean> upsert(Update update, Query query, Class<?> collection) {
111-
return reactiveMongoTemplate.upsert(query, update, collection)
123+
public Mono<Boolean> upsert(Update update, Criteria criteria, Class<?> collection) {
124+
return reactiveMongoTemplate.upsert(new Query(criteria), update, collection)
112125
.map(updateResult -> updateResult.getModifiedCount() > 0);
113126
}
114127

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.openblocks.sdk.util;
2+
3+
import com.google.common.hash.Hashing;
4+
5+
public class HashUtils {
6+
7+
@SuppressWarnings("UnstableApiUsage")
8+
public static String hash(byte[] content) {
9+
return Hashing.sha256().hashBytes(content).toString();
10+
}
11+
}

0 commit comments

Comments
 (0)