Skip to content

Commit

Permalink
add upgrade runner for widget
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardShan committed Jan 3, 2019
1 parent 6d61be4 commit 2f2187f
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ tempFiles/
userfiles/
.vscode
*yarn.lock
bin/upgrade.go
bin/upgrade
6 changes: 6 additions & 0 deletions server/src/main/java/edp/davinci/dao/WidgetMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,10 @@ public interface WidgetMapper {

@Select({"select * from widget where view_id = #{viewId}"})
List<Widget> getWidgetsByWiew(@Param("viewId") Long viewId);

@Select({"SELECT * from widget WHERE IFNULL(config,'') != ''"})
List<Widget> queryUpgrade();


int updateConfigBatch(@Param("list") List<Widget> list);
}
49 changes: 49 additions & 0 deletions server/src/main/java/edp/davinci/runner/UpgradeRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* <<
* Davinci
* ==
* Copyright (C) 2016 - 2018 EDP
* ==
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* >>
*
*/

package edp.davinci.runner;

import edp.davinci.service.WidgetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
* widget 升级数据迁移
*/
@Order(4)
@Component
public class UpgradeRunner implements ApplicationRunner {

@Autowired
private WidgetService widgetService;

@Override
public void run(ApplicationArguments args) throws Exception {
new Thread(() -> {
try {
widgetService.upgradeWidgetConfig();
} catch (Exception e) {

}
}).start();
}
}
3 changes: 2 additions & 1 deletion server/src/main/java/edp/davinci/service/WidgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import edp.davinci.core.common.ResultMap;
import edp.davinci.core.service.CheckEntityService;
import edp.davinci.dto.viewDto.ViewExecuteParam;
import edp.davinci.dto.viewDto.ViewExecuteSql;
import edp.davinci.dto.widgetDto.WidgetCreate;
import edp.davinci.dto.widgetDto.WidgetUpdate;
import edp.davinci.model.User;
Expand All @@ -45,4 +44,6 @@ public interface WidgetService extends CheckEntityService {
ViewExecuteParam buildViewExecuteParam(Widget widget);

ResultMap generationCsv(Long id, ViewExecuteParam executeParam, User user, HttpServletRequest request);

void upgradeWidgetConfig();
}
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,57 @@ public ResultMap generationCsv(Long id, ViewExecuteParam executeParam, User user
}
return resultMap.successAndRefreshToken(request).payload(getHost() + filePath);
}


@Transactional
public void upgradeWidgetConfig() {
List<Widget> widgets = widgetMapper.queryUpgrade();
List<Widget> updateList = null;
if (null != widgets && widgets.size() > 0) {
updateList = new ArrayList<>();

for (Widget widget : widgets) {
if (StringUtils.isEmpty(widget.getConfig())) {
continue;
}

JSONObject jsonObject = JSONObject.parseObject(widget.getConfig());
if (null != jsonObject) {
if (jsonObject.containsKey("cols")) {
JSONArray cols = jsonObject.getJSONArray("cols");
if (null != cols && cols.size() > 0) {
Map<Long, List<JSONObject>> map = null;
for (Object obj : cols) {
if (obj instanceof String) {
if (null == map) {
map = new HashMap<>();
}

List<JSONObject> list = null;
if (map.containsKey(widget.getId())) {
list = map.get(widget.getId());
} else {
list = new ArrayList<>();
map.put(widget.getId(), list);
}
JSONObject col = new JSONObject();
col.put("name", String.valueOf(obj));
list.add(col);
}
}
if (null != map && map.size() > 0) {
jsonObject.put("cols", map.get(widget.getId()));
widget.setConfig(jsonObject.toJSONString());
updateList.add(widget);
}
}
}
}
}
}

if (null != updateList && updateList.size() > 0) {
widgetMapper.updateConfigBatch(updateList);
}
}
}
11 changes: 11 additions & 0 deletions server/src/main/resources/mybatis/mapper/WidgetMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@
</foreach>
</insert>

<update id="updateConfigBatch">
update widget set `config` =
<foreach collection="list" item="record" index="index" separator=" " open="case id" close="end">
when #{record.id} then #{record.config}
</foreach>
where id in
<foreach collection="list" index="index" item="record" separator="," open="(" close=")">
#{record.id,jdbcType=BIGINT}
</foreach>
</update>


<select id="getByIds" resultType="edp.davinci.model.Widget" parameterType="java.util.Set">
select * from widget where id in
Expand Down

0 comments on commit 2f2187f

Please sign in to comment.