/* * 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.keymatch; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.codelibs.fess.Constants; import org.codelibs.fess.app.pager.KeyMatchPager; import org.codelibs.fess.app.service.KeyMatchService; import org.codelibs.fess.app.web.CrudMode; import org.codelibs.fess.app.web.base.FessAdminAction; import org.codelibs.fess.es.config.exentity.KeyMatch; import org.codelibs.fess.helper.KeyMatchHelper; 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 * @author jflute */ public class AdminKeymatchAction extends FessAdminAction { // =================================================================================== // Attribute // ========= @Resource private KeyMatchHelper keyMatchHelper; @Resource private KeyMatchService keyMatchService; @Resource private KeyMatchPager keyMatchPager; // =================================================================================== // Hook // ====== @Override protected void setupHtmlData(final ActionRuntime runtime) { super.setupHtmlData(runtime); runtime.registerData("helpLink", systemHelper.getHelpLink(fessConfig.getOnlineHelpNameKeymatch())); } // =================================================================================== // Search Execute // ============== @Execute public HtmlResponse index(final SearchForm form) { return asListHtml(); } @Execute public HtmlResponse list(final OptionalThing<Integer> pageNumber, final SearchForm form) { pageNumber.ifPresent(num -> { keyMatchPager.setCurrentPageNumber(pageNumber.get()); }).orElse(() -> { keyMatchPager.setCurrentPageNumber(0); }); return asHtml(path_AdminKeymatch_AdminKeymatchJsp).renderWith(data -> { searchPaging(data, form); }); } @Execute public HtmlResponse search(final SearchForm form) { copyBeanToBean(form, keyMatchPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE)); return asHtml(path_AdminKeymatch_AdminKeymatchJsp).renderWith(data -> { searchPaging(data, form); }); } @Execute public HtmlResponse reset(final SearchForm form) { keyMatchPager.clear(); return asHtml(path_AdminKeymatch_AdminKeymatchJsp).renderWith(data -> { searchPaging(data, form); }); } protected void searchPaging(final RenderData data, final SearchForm form) { RenderDataUtil.register(data, "keyMatchItems", keyMatchService.getKeyMatchList(keyMatchPager)); // page navi // restore from pager copyBeanToBean(keyMatchPager, form, op -> op.include("id")); } // =================================================================================== // Edit Execute // ============ // ----------------------------------------------------- // Entry Page // ---------- @Execute public HtmlResponse createnew() { saveToken(); return asHtml(path_AdminKeymatch_AdminKeymatchEditJsp).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; keyMatchService.getKeyMatch(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(); final List<Map<String, Object>> docList = new ArrayList<>(); return asHtml(path_AdminKeymatch_AdminKeymatchDetailsJsp).useForm(EditForm.class, op -> { op.setup(form -> { keyMatchService.getKeyMatch(id).ifPresent(entity -> { copyBeanToBean(entity, form, copyOp -> { copyOp.excludeNull(); }); form.crudMode = crudMode; docList.addAll(keyMatchHelper.getBoostedDocumentList(entity.getTerm(), entity.getMaxSize())); }).orElse(() -> { throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), () -> asListHtml()); }); }); }).renderWith(data -> { data.register("docs", docList); }); } // ----------------------------------------------------- // Actually Crud // ------------- @Execute public HtmlResponse create(final CreateForm form) { verifyCrudMode(form.crudMode, CrudMode.CREATE); validate(form, messages -> {}, () -> asEditHtml()); verifyToken(() -> asEditHtml()); getKeyMatch(form).ifPresent( entity -> { try { keyMatchService.store(entity); saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL)); ComponentUtil.getKeyMatchHelper().update(); } 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()); getKeyMatch(form).ifPresent( entity -> { try { keyMatchService.store(entity); saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL)); ComponentUtil.getKeyMatchHelper().update(); } 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; keyMatchService .getKeyMatch(id) .ifPresent( entity -> { try { keyMatchService.delete(entity); saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL)); ComponentUtil.getKeyMatchHelper().update(); } 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 // ============ public static OptionalEntity<KeyMatch> getEntity(final CreateForm form, final String username, final long currentTime) { switch (form.crudMode) { case CrudMode.CREATE: return OptionalEntity.of(new KeyMatch()).map(entity -> { entity.setCreatedBy(username); entity.setCreatedTime(currentTime); return entity; }); case CrudMode.EDIT: if (form instanceof EditForm) { return ComponentUtil.getComponent(KeyMatchService.class).getKeyMatch(((EditForm) form).id); } break; default: break; } return OptionalEntity.empty(); } public static OptionalEntity<KeyMatch> getKeyMatch(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)); 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_AdminKeymatch_AdminKeymatchJsp).renderWith(data -> { RenderDataUtil.register(data, "keyMatchItems", keyMatchService.getKeyMatchList(keyMatchPager)); // page navi }).useForm(SearchForm.class, setup -> { setup.setup(form -> { copyBeanToBean(keyMatchPager, form, op -> op.include("id")); }); }); } private HtmlResponse asEditHtml() { return asHtml(path_AdminKeymatch_AdminKeymatchEditJsp); } private HtmlResponse asDetailsHtml() { return asHtml(path_AdminKeymatch_AdminKeymatchDetailsJsp); } }