fix #948 Admin API: /api/admin/boostdoc

This commit is contained in:
Shinsuke Sugaya 2017-03-25 06:45:36 +09:00
parent 080eb397ac
commit e93fcd4d24
12 changed files with 325 additions and 31 deletions

View file

@ -17,12 +17,15 @@ package org.codelibs.fess.app.web.admin.boostdoc;
import javax.annotation.Resource;
import org.codelibs.core.beans.util.BeanUtil;
import org.codelibs.fess.Constants;
import org.codelibs.fess.app.pager.BoostDocPager;
import org.codelibs.fess.app.service.BoostDocumentRuleService;
import org.codelibs.fess.app.web.CrudMode;
import org.codelibs.fess.app.web.base.FessAdminAction;
import org.codelibs.fess.es.config.exentity.BoostDocumentRule;
import org.codelibs.fess.helper.SystemHelper;
import org.codelibs.fess.util.ComponentUtil;
import org.codelibs.fess.util.RenderDataUtil;
import org.dbflute.optional.OptionalEntity;
import org.dbflute.optional.OptionalThing;
@ -225,7 +228,7 @@ public class AdminBoostdocAction extends FessAdminAction {
// Assist Logic
// ============
private OptionalEntity<BoostDocumentRule> getEntity(final CreateForm form, final String username, final long currentTime) {
public static OptionalEntity<BoostDocumentRule> getEntity(final CreateForm form, final String username, final long currentTime) {
switch (form.crudMode) {
case CrudMode.CREATE:
return OptionalEntity.of(new BoostDocumentRule()).map(entity -> {
@ -235,7 +238,7 @@ public class AdminBoostdocAction extends FessAdminAction {
});
case CrudMode.EDIT:
if (form instanceof EditForm) {
return boostDocumentRuleService.getBoostDocumentRule(((EditForm) form).id);
return ComponentUtil.getComponent(BoostDocumentRuleService.class).getBoostDocumentRule(((EditForm) form).id);
}
break;
default:
@ -244,13 +247,14 @@ public class AdminBoostdocAction extends FessAdminAction {
return OptionalEntity.empty();
}
protected OptionalEntity<BoostDocumentRule> getBoostDocumentRule(final CreateForm form) {
public static OptionalEntity<BoostDocumentRule> getBoostDocumentRule(final CreateForm form) {
final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
final String username = systemHelper.getUsername();
final long currentTime = systemHelper.getCurrentTimeAsLong();
return getEntity(form, username, currentTime).map(entity -> {
entity.setUpdatedBy(username);
entity.setUpdatedTime(currentTime);
copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
BeanUtil.copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
return entity;
});
}

View file

@ -0,0 +1,33 @@
/*
* Copyright 2012-2017 CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.api.admin;
import org.codelibs.fess.Constants;
import org.codelibs.fess.util.ComponentUtil;
import org.lastaflute.web.validation.Required;
public class BaseSearchBody {
@Required
public Integer size = ComponentUtil.getFessConfig().getPagingPageSizeAsInteger();
@Required
public Integer page = Constants.DEFAULT_ADMIN_PAGE_NUMBER;
public BaseSearchBody() {
super();
}
}

View file

@ -15,16 +15,8 @@
*/
package org.codelibs.fess.app.web.api.admin.accesstoken;
import org.codelibs.fess.Constants;
import org.codelibs.fess.util.ComponentUtil;
import org.lastaflute.web.validation.Required;
import org.codelibs.fess.app.web.api.admin.BaseSearchBody;
public class SearchBody {
@Required
public Integer size = ComponentUtil.getFessConfig().getPagingPageSizeAsInteger();
@Required
public Integer page = Constants.DEFAULT_ADMIN_PAGE_NUMBER;
public class SearchBody extends BaseSearchBody {
}

View file

@ -15,6 +15,20 @@
*/
package org.codelibs.fess.app.web.api.admin.badword;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.codelibs.fess.Constants;
import org.codelibs.fess.app.pager.BadWordPager;
import org.codelibs.fess.app.service.BadWordService;
@ -23,6 +37,7 @@ import org.codelibs.fess.app.web.admin.badword.CreateForm;
import org.codelibs.fess.app.web.admin.badword.EditForm;
import org.codelibs.fess.app.web.admin.badword.UploadForm;
import org.codelibs.fess.app.web.api.ApiResult;
import org.codelibs.fess.app.web.api.admin.BaseSearchBody;
import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
import org.codelibs.fess.es.config.exentity.BadWord;
import org.codelibs.fess.exception.FessSystemException;
@ -32,13 +47,6 @@ import org.lastaflute.web.Execute;
import org.lastaflute.web.response.JsonResponse;
import org.lastaflute.web.response.StreamResponse;
import javax.annotation.Resource;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
public class ApiAdminBadwordAction extends FessApiAdminAction {
@Resource
@ -50,7 +58,7 @@ public class ApiAdminBadwordAction extends FessApiAdminAction {
// GET /api/admin/badword
// POST /api/admin/badword
@Execute
public JsonResponse<ApiResult> settings(SearchBody body) {
public JsonResponse<ApiResult> settings(BaseSearchBody body) {
validateApi(body, messages -> {});
final BadWordPager pager = new BadWordPager();
pager.setPageSize(body.size);

View file

@ -1,3 +1,18 @@
/*
* Copyright 2012-2017 CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.api.admin.badword;
import org.codelibs.fess.app.web.admin.badword.CreateForm;

View file

@ -1,3 +1,18 @@
/*
* Copyright 2012-2017 CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.api.admin.badword;
import org.codelibs.fess.app.web.admin.badword.DownloadForm;

View file

@ -1,3 +1,18 @@
/*
* Copyright 2012-2017 CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.api.admin.badword;
import org.codelibs.fess.app.web.admin.badword.EditForm;

View file

@ -1,14 +1,22 @@
/*
* Copyright 2012-2017 CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.api.admin.badword;
import org.codelibs.fess.Constants;
import org.codelibs.fess.util.ComponentUtil;
import org.lastaflute.web.validation.Required;
import org.codelibs.fess.app.web.api.admin.BaseSearchBody;
public class SearchBody {
@Required
public Integer size = ComponentUtil.getFessConfig().getPagingPageSizeAsInteger();
@Required
public Integer page = Constants.DEFAULT_ADMIN_PAGE_NUMBER;
public class SearchBody extends BaseSearchBody {
}

View file

@ -0,0 +1,138 @@
/*
* Copyright 2012-2017 CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.api.admin.boostdoc;
import static org.codelibs.fess.app.web.admin.boostdoc.AdminBoostdocAction.getBoostDocumentRule;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.codelibs.fess.app.pager.BoostDocPager;
import org.codelibs.fess.app.service.BoostDocumentRuleService;
import org.codelibs.fess.app.web.CrudMode;
import org.codelibs.fess.app.web.api.ApiResult;
import org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse;
import org.codelibs.fess.app.web.api.ApiResult.ApiConfigsResponse;
import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse;
import org.codelibs.fess.app.web.api.ApiResult.Status;
import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
import org.codelibs.fess.es.config.exentity.BoostDocumentRule;
import org.lastaflute.web.Execute;
import org.lastaflute.web.response.JsonResponse;
/**
* @author shinsuke
*/
public class ApiAdminBoostdocAction extends FessApiAdminAction {
// ===================================================================================
// Attribute
// =========
@Resource
private BoostDocumentRuleService boostDocumentRuleService;
// ===================================================================================
// Search Execute
// ==============
// GET /api/admin/boostdoc
// POST /api/admin/boostdoc
@Execute
public JsonResponse<ApiResult> settings(SearchBody body) {
validateApi(body, messages -> {});
final BoostDocPager pager = new BoostDocPager();
pager.setPageSize(body.size);
pager.setCurrentPageNumber(body.page);
final List<BoostDocumentRule> list = boostDocumentRuleService.getBoostDocumentRuleList(pager);
return asJson(new ApiConfigsResponse<EditBody>()
.settings(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList()))
.total(pager.getAllRecordCount()).status(Status.OK).result());
}
// GET /api/admin/boostdoc/setting/{id}
@Execute
public JsonResponse<ApiResult> get$setting(String id) {
return asJson(new ApiConfigResponse()
.setting(boostDocumentRuleService.getBoostDocumentRule(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
return null;
})).status(Status.OK).result());
}
// PUT /api/admin/boostdoc/setting
@Execute
public JsonResponse<ApiResult> put$setting(CreateBody body) {
validateApi(body, messages -> {});
body.crudMode = CrudMode.CREATE;
final BoostDocumentRule boostDoc = getBoostDocumentRule(body).map(entity -> {
try {
boostDocumentRuleService.store(entity);
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return entity;
}).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
return null;
});
return asJson(new ApiUpdateResponse().id(boostDoc.getId()).created(true).status(Status.OK).result());
}
// POST /api/admin/boostdoc/setting
@Execute
public JsonResponse<ApiResult> post$setting(final EditBody body) {
validateApi(body, messages -> {});
body.crudMode = CrudMode.EDIT;
final BoostDocumentRule boostDoc = getBoostDocumentRule(body).map(entity -> {
try {
boostDocumentRuleService.store(entity);
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return entity;
}).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
return null;
});
return asJson(new ApiUpdateResponse().id(boostDoc.getId()).created(false).status(Status.OK).result());
}
// DELETE /api/admin/boostdoc/setting/{id}
@Execute
public JsonResponse<ApiResult> delete$setting(String id) {
boostDocumentRuleService.getBoostDocumentRule(id).ifPresent(entity -> {
try {
boostDocumentRuleService.delete(entity);
saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
}
}).orElse(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
});
return asJson(new ApiResponse().status(Status.OK).result());
}
protected EditBody createEditBody(final BoostDocumentRule entity) {
final EditBody form = new EditBody();
copyBeanToBean(entity, form, copyOp -> copyOp.excludeNull());
form.crudMode = null;
return form;
}
}

View file

@ -0,0 +1,22 @@
/*
* Copyright 2012-2017 CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.api.admin.boostdoc;
import org.codelibs.fess.app.web.admin.boostdoc.CreateForm;
public class CreateBody extends CreateForm {
}

View file

@ -0,0 +1,22 @@
/*
* Copyright 2012-2017 CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.api.admin.boostdoc;
import org.codelibs.fess.app.web.admin.boostdoc.EditForm;
public class EditBody extends EditForm {
}

View file

@ -0,0 +1,22 @@
/*
* Copyright 2012-2017 CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.api.admin.boostdoc;
import org.codelibs.fess.app.web.api.admin.BaseSearchBody;
public class SearchBody extends BaseSearchBody {
}