/* * 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.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; import org.lastaflute.web.Execute; import org.lastaflute.web.response.HtmlResponse; import org.lastaflute.web.response.render.RenderData; import org.lastaflute.web.ruts.process.ActionRuntime; /** * @author shinsuke */ public class AdminBoostdocAction extends FessAdminAction { // =================================================================================== // Attribute // ========= @Resource private BoostDocumentRuleService boostDocumentRuleService; @Resource private BoostDocPager boostDocPager; // =================================================================================== // Hook // ====== @Override protected void setupHtmlData(final ActionRuntime runtime) { super.setupHtmlData(runtime); runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameBoostdoc())); } // =================================================================================== // Search Execute // ============== @Execute public HtmlResponse index() { return asListHtml(); } @Execute public HtmlResponse list(final OptionalThing<Integer> pageNumber, final SearchForm form) { pageNumber.ifPresent(num -> { boostDocPager.setCurrentPageNumber(pageNumber.get()); }).orElse(() -> { boostDocPager.setCurrentPageNumber(0); }); return asHtml(path_AdminBoostdoc_AdminBoostdocJsp).renderWith(data -> { searchPaging(data, form); }); } @Execute public HtmlResponse search(final SearchForm form) { copyBeanToBean(form, boostDocPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE)); return asHtml(path_AdminBoostdoc_AdminBoostdocJsp).renderWith(data -> { searchPaging(data, form); }); } @Execute public HtmlResponse reset(final SearchForm form) { boostDocPager.clear(); return asHtml(path_AdminBoostdoc_AdminBoostdocJsp).renderWith(data -> { searchPaging(data, form); }); } protected void searchPaging(final RenderData data, final SearchForm form) { RenderDataUtil.register(data, "boostDocumentRuleItems", boostDocumentRuleService.getBoostDocumentRuleList(boostDocPager)); // page navi // restore from pager copyBeanToBean(boostDocPager, form, op -> op.include("id")); } // =================================================================================== // Edit Execute // ============ // ----------------------------------------------------- // Entry Page // ---------- @Execute public HtmlResponse createnew() { saveToken(); return asEditHtml().useForm(CreateForm.class, op -> { op.setup(form -> { form.initialize(); form.crudMode = CrudMode.CREATE; }); }); } @Execute public HtmlResponse edit(final EditForm form) { validate(form, messages -> {}, () -> asListHtml()); final String id = form.id; boostDocumentRuleService.getBoostDocumentRule(id).ifPresent(entity -> { copyBeanToBean(entity, form, op -> {}); }).orElse(() -> { throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asListHtml()); }); saveToken(); if (form.crudMode.intValue() == CrudMode.EDIT) { // back form.crudMode = CrudMode.DETAILS; return asDetailsHtml(); } else { form.crudMode = CrudMode.EDIT; return asEditHtml(); } } // ----------------------------------------------------- // Details // ------- @Execute public HtmlResponse details(final int crudMode, final String id) { verifyCrudMode(crudMode, CrudMode.DETAILS); saveToken(); return asDetailsHtml().useForm(EditForm.class, op -> { op.setup(form -> { boostDocumentRuleService.getBoostDocumentRule(id).ifPresent(entity -> { copyBeanToBean(entity, form, copyOp -> { copyOp.excludeNull(); }); form.crudMode = crudMode; }).orElse(() -> { throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asListHtml()); }); }); }); } // ----------------------------------------------------- // Actually Crud // ------------- @Execute public HtmlResponse create(final CreateForm form) { verifyCrudMode(form.crudMode, CrudMode.CREATE); validate(form, messages -> {}, () -> asEditHtml()); verifyToken(() -> asEditHtml()); getBoostDocumentRule(form).ifPresent( entity -> { try { boostDocumentRuleService.store(entity); saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL)); } catch (final Exception e) { throwValidationError(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)), () -> asEditHtml()); } }).orElse(() -> { throwValidationError(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL), () -> asEditHtml()); }); return redirect(getClass()); } @Execute public HtmlResponse update(final EditForm form) { verifyCrudMode(form.crudMode, CrudMode.EDIT); validate(form, messages -> {}, () -> asEditHtml()); verifyToken(() -> asEditHtml()); getBoostDocumentRule(form).ifPresent( entity -> { try { boostDocumentRuleService.store(entity); saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL)); } catch (final Exception e) { throwValidationError(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)), () -> asEditHtml()); } }).orElse(() -> { throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), () -> asEditHtml()); }); return redirect(getClass()); } @Execute public HtmlResponse delete(final EditForm form) { verifyCrudMode(form.crudMode, CrudMode.DETAILS); validate(form, messages -> {}, () -> asDetailsHtml()); verifyToken(() -> asDetailsHtml()); final String id = form.id; boostDocumentRuleService .getBoostDocumentRule(id) .ifPresent( entity -> { try { boostDocumentRuleService.delete(entity); saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL)); } catch (final Exception e) { throwValidationError( messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)), () -> asEditHtml()); } }).orElse(() -> { throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asDetailsHtml()); }); return redirect(getClass()); } // =================================================================================== // Assist Logic // ============ private 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 -> { entity.setCreatedBy(username); entity.setCreatedTime(currentTime); return entity; }); case CrudMode.EDIT: if (form instanceof EditForm) { return ComponentUtil.getComponent(BoostDocumentRuleService.class).getBoostDocumentRule(((EditForm) form).id); } break; default: break; } return OptionalEntity.empty(); } 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); BeanUtil.copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE)); return entity; }); } // =================================================================================== // Small Helper // ============ protected void verifyCrudMode(final int crudMode, final int expectedMode) { if (crudMode != expectedMode) { throwValidationError(messages -> { messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode)); }, () -> asListHtml()); } } // =================================================================================== // JSP // ========= private HtmlResponse asListHtml() { return asHtml(path_AdminBoostdoc_AdminBoostdocJsp).renderWith(data -> { RenderDataUtil.register(data, "boostDocumentRuleItems", boostDocumentRuleService.getBoostDocumentRuleList(boostDocPager)); }).useForm(SearchForm.class, setup -> { setup.setup(form -> { copyBeanToBean(boostDocPager, form, op -> op.include("id")); }); }); } private HtmlResponse asEditHtml() { return asHtml(path_AdminBoostdoc_AdminBoostdocEditJsp); } private HtmlResponse asDetailsHtml() { return asHtml(path_AdminBoostdoc_AdminBoostdocDetailsJsp); } }