refactoring for path mapping

This commit is contained in:
Keiichi Watanabe 2015-10-19 14:33:42 +09:00
parent 2cbec540b9
commit 6f28da6f92
7 changed files with 274 additions and 196 deletions

View file

@ -30,6 +30,7 @@ import org.codelibs.fess.es.cbean.PathMappingCB;
import org.codelibs.fess.es.exbhv.PathMappingBhv;
import org.codelibs.fess.es.exentity.PathMapping;
import org.dbflute.cbean.result.PagingResultBean;
import org.dbflute.optional.OptionalEntity;
public class PathMappingService implements Serializable {
@ -54,18 +55,8 @@ public class PathMappingService implements Serializable {
return pathMappingList;
}
public PathMapping getPathMapping(final Map<String, String> keys) {
final PathMapping pathMapping = pathMappingBhv.selectEntity(cb -> {
cb.query().docMeta().setId_Equal(keys.get("id"));
cb.request().setVersion(true);
setupEntityCondition(cb, keys);
}).orElse(null);//TODO
if (pathMapping == null) {
// TODO exception?
return null;
}
return pathMapping;
public OptionalEntity<PathMapping> getPathMapping(final String id) {
return pathMappingBhv.selectByPK(id);
}
public void store(final PathMapping pathMapping) {

View file

@ -16,9 +16,6 @@
package org.codelibs.fess.app.web.admin.pathmapping;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.codelibs.fess.Constants;
@ -29,6 +26,7 @@ import org.codelibs.fess.app.web.CrudMode;
import org.codelibs.fess.app.web.base.FessAdminAction;
import org.codelibs.fess.es.exentity.PathMapping;
import org.codelibs.fess.helper.SystemHelper;
import org.dbflute.optional.OptionalEntity;
import org.lastaflute.web.Execute;
import org.lastaflute.web.callback.ActionRuntime;
import org.lastaflute.web.response.HtmlResponse;
@ -38,6 +36,7 @@ import org.lastaflute.web.validation.VaErrorHook;
/**
* @author shinsuke
* @author Shunji Makino
* @author Keiichi Watanabe
*/
public class AdminPathmappingAction extends FessAdminAction {
@ -64,14 +63,14 @@ public class AdminPathmappingAction extends FessAdminAction {
// Search Execute
// ==============
@Execute
public HtmlResponse index(final PathMappingSearchForm form) {
public HtmlResponse index(final SearchForm form) {
return asHtml(path_AdminPathmapping_IndexJsp).renderWith(data -> {
searchPaging(data, form);
});
}
@Execute
public HtmlResponse list(final Integer pageNumber, final PathMappingSearchForm form) {
public HtmlResponse list(final Integer pageNumber, final SearchForm form) {
pathMappingPager.setCurrentPageNumber(pageNumber);
return asHtml(path_AdminPathmapping_IndexJsp).renderWith(data -> {
searchPaging(data, form);
@ -79,15 +78,15 @@ public class AdminPathmappingAction extends FessAdminAction {
}
@Execute
public HtmlResponse search(final PathMappingSearchForm form) {
copyBeanToBean(form.searchParams, pathMappingPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
public HtmlResponse search(final SearchForm form) {
copyBeanToBean(form, pathMappingPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
return asHtml(path_AdminPathmapping_IndexJsp).renderWith(data -> {
searchPaging(data, form);
});
}
@Execute
public HtmlResponse reset(final PathMappingSearchForm form) {
public HtmlResponse reset(final SearchForm form) {
pathMappingPager.clear();
return asHtml(path_AdminPathmapping_IndexJsp).renderWith(data -> {
searchPaging(data, form);
@ -95,17 +94,17 @@ public class AdminPathmappingAction extends FessAdminAction {
}
@Execute
public HtmlResponse back(final PathMappingSearchForm form) {
public HtmlResponse back(final SearchForm form) {
return asHtml(path_AdminPathmapping_IndexJsp).renderWith(data -> {
searchPaging(data, form);
});
}
protected void searchPaging(final RenderData data, final PathMappingSearchForm form) {
protected void searchPaging(final RenderData data, final SearchForm form) {
data.register("pathMappingItems", pathMappingService.getPathMappingList(pathMappingPager)); // page navi
// restore from pager
copyBeanToBean(pathMappingPager, form.searchParams, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
copyBeanToBean(pathMappingPager, form, op -> op.include("id"));
}
// ===================================================================================
@ -116,51 +115,92 @@ public class AdminPathmappingAction extends FessAdminAction {
// ----------
@Token(save = true, validate = false)
@Execute
public HtmlResponse createpage(final PathMappingEditForm form) {
form.initialize();
form.crudMode = CrudMode.CREATE;
public HtmlResponse createpage() {
return asHtml(path_AdminPathmapping_EditJsp).useForm(CreateForm.class, op -> {
op.setup(form -> {
form.initialize();
form.crudMode = CrudMode.CREATE;
});
});
}
@Token(save = true, validate = false)
@Execute
public HtmlResponse editpage(final int crudMode, final String id) {
verifyCrudMode(crudMode, CrudMode.EDIT);
return asHtml(path_AdminPathmapping_EditJsp).useForm(EditForm.class, op -> {
op.setup(form -> {
pathMappingService.getPathMapping(id).ifPresent(entity -> {
copyBeanToBean(entity, form, copyOp -> {
copyOp.excludeNull();
});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
form.crudMode = crudMode;
});
});
}
@Token(save = true, validate = false)
@Execute
public HtmlResponse createagain(final CreateForm form) {
verifyCrudMode(form.crudMode, CrudMode.CREATE);
validate(form, messages -> {}, toEditHtml());
return asHtml(path_AdminPathmapping_EditJsp);
}
@Token(save = true, validate = false)
@Execute
public HtmlResponse editpage(final int crudMode, final String id, final PathMappingEditForm form) {
form.crudMode = crudMode;
form.id = id;
verifyCrudMode(form, CrudMode.EDIT);
loadPathMapping(form);
public HtmlResponse editagain(final EditForm form) {
verifyCrudMode(form.crudMode, CrudMode.EDIT);
validate(form, messages -> {}, toEditHtml());
return asHtml(path_AdminPathmapping_EditJsp);
}
@Token(save = true, validate = false)
@Execute
public HtmlResponse editagain(final PathMappingEditForm form) {
return asHtml(path_AdminPathmapping_EditJsp);
}
@Token(save = true, validate = false)
@Execute
public HtmlResponse editfromconfirm(final PathMappingEditForm form) {
public HtmlResponse editfromconfirm(final EditForm form) {
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.EDIT;
loadPathMapping(form);
final String id = form.id;
pathMappingService.getPathMapping(id).ifPresent(entity -> {
copyBeanToBean(entity, form, op -> {});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
return asHtml(path_AdminPathmapping_EditJsp);
}
@Token(save = true, validate = false)
@Execute
public HtmlResponse deletepage(final int crudMode, final String id, final PathMappingEditForm form) {
form.crudMode = crudMode;
form.id = id;
verifyCrudMode(form, CrudMode.DELETE);
loadPathMapping(form);
return asHtml(path_AdminPathmapping_ConfirmJsp);
public HtmlResponse deletepage(final int crudMode, final String id) {
verifyCrudMode(crudMode, CrudMode.DELETE);
return asHtml(path_AdminPathmapping_ConfirmJsp).useForm(EditForm.class, op -> {
op.setup(form -> {
pathMappingService.getPathMapping(id).ifPresent(entity -> {
copyBeanToBean(entity, form, copyOp -> {
copyOp.excludeNull();
});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
form.crudMode = crudMode;
});
});
}
@Token(save = true, validate = false)
@Execute
public HtmlResponse deletefromconfirm(final PathMappingEditForm form) {
public HtmlResponse deletefromconfirm(final EditForm form) {
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.DELETE;
loadPathMapping(form);
final String id = form.id;
pathMappingService.getPathMapping(id).ifPresent(entity -> {
copyBeanToBean(entity, form, op -> {});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
return asHtml(path_AdminPathmapping_ConfirmJsp);
}
@ -168,25 +208,35 @@ public class AdminPathmappingAction extends FessAdminAction {
// Confirm
// -------
@Execute
public HtmlResponse confirmpage(final int crudMode, final String id, final PathMappingEditForm form) {
form.crudMode = crudMode;
form.id = id;
verifyCrudMode(form, CrudMode.CONFIRM);
loadPathMapping(form);
public HtmlResponse confirmpage(final int crudMode, final String id) {
verifyCrudMode(crudMode, CrudMode.CONFIRM);
return asHtml(path_AdminPathmapping_ConfirmJsp).useForm(EditForm.class, op -> {
op.setup(form -> {
pathMappingService.getPathMapping(id).ifPresent(entity -> {
copyBeanToBean(entity, form, copyOp -> {
copyOp.excludeNull();
});
form.crudMode = crudMode;
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
});
});
}
@Token(save = false, validate = true, keep = true)
@Execute
public HtmlResponse confirmfromcreate(final CreateForm form) {
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.CREATE;
return asHtml(path_AdminPathmapping_ConfirmJsp);
}
@Token(save = false, validate = true, keep = true)
@Execute
public HtmlResponse confirmfromcreate(final PathMappingEditForm form) {
validate(form, messages -> {}, toEditHtml());
return asHtml(path_AdminPathmapping_ConfirmJsp);
}
@Token(save = false, validate = true, keep = true)
@Execute
public HtmlResponse confirmfromupdate(final PathMappingEditForm form) {
public HtmlResponse confirmfromupdate(final EditForm form) {
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.EDIT;
return asHtml(path_AdminPathmapping_ConfirmJsp);
}
@ -195,75 +245,87 @@ public class AdminPathmappingAction extends FessAdminAction {
// -------------
@Token(save = false, validate = true)
@Execute
public HtmlResponse create(final PathMappingEditForm form) {
public HtmlResponse create(final CreateForm form) {
verifyCrudMode(form.crudMode, CrudMode.CREATE);
validate(form, messages -> {}, toEditHtml());
pathMappingService.store(createPathMapping(form));
saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
createPathMapping(form).ifPresent(entity -> {
copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
pathMappingService.store(entity);
saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL), toEditHtml());
});
return redirect(getClass());
}
@Token(save = false, validate = true)
@Execute
public HtmlResponse update(final PathMappingEditForm form) {
public HtmlResponse update(final EditForm form) {
verifyCrudMode(form.crudMode, CrudMode.EDIT);
validate(form, messages -> {}, toEditHtml());
pathMappingService.store(createPathMapping(form));
saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
createPathMapping(form).ifPresent(entity -> {
copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
pathMappingService.store(entity);
saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), toEditHtml());
});
return redirect(getClass());
}
@Execute
public HtmlResponse delete(final PathMappingEditForm form) {
verifyCrudMode(form, CrudMode.DELETE);
pathMappingService.delete(getPathMapping(form));
saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
public HtmlResponse delete(final EditForm form) {
verifyCrudMode(form.crudMode, CrudMode.DELETE);
validate(form, messages -> {}, toEditHtml());
final String id = form.id;
pathMappingService.getPathMapping(id).ifPresent(entity -> {
pathMappingService.delete(entity);
saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
return redirect(getClass());
}
// ===================================================================================
// Assist Logic
// ============
protected void loadPathMapping(final PathMappingEditForm form) {
copyBeanToBean(getPathMapping(form), form, op -> op.exclude("crudMode"));
}
protected PathMapping getPathMapping(final PathMappingEditForm form) {
final PathMapping pathMapping = pathMappingService.getPathMapping(createKeyMap(form));
if (pathMapping == null) {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), toEditHtml());
}
return pathMapping;
}
protected PathMapping createPathMapping(final PathMappingEditForm form) {
PathMapping pathMapping;
protected OptionalEntity<PathMapping> createPathMapping(final CreateForm form) {
final String username = systemHelper.getUsername();
final long currentTime = systemHelper.getCurrentTimeAsLong();
if (form.crudMode == CrudMode.EDIT) {
pathMapping = getPathMapping(form);
} else {
pathMapping = new PathMapping();
pathMapping.setCreatedBy(username);
pathMapping.setCreatedTime(currentTime);
switch (form.crudMode) {
case CrudMode.CREATE:
if (form instanceof CreateForm) {
final PathMapping entity = new PathMapping();
entity.setCreatedBy(username);
entity.setCreatedTime(currentTime);
entity.setUpdatedBy(username);
entity.setUpdatedTime(currentTime);
return OptionalEntity.of(entity);
}
break;
case CrudMode.EDIT:
if (form instanceof EditForm) {
return pathMappingService.getPathMapping(((EditForm) form).id).map(entity -> {
entity.setUpdatedBy(username);
entity.setUpdatedTime(currentTime);
return entity;
});
}
break;
default:
break;
}
pathMapping.setUpdatedBy(username);
pathMapping.setUpdatedTime(currentTime);
copyBeanToBean(form, pathMapping, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
return pathMapping;
}
protected Map<String, String> createKeyMap(final PathMappingEditForm form) {
final Map<String, String> keys = new HashMap<String, String>();
keys.put("id", form.id);
return keys;
return OptionalEntity.empty();
}
// ===================================================================================
// Small Helper
// ============
protected void verifyCrudMode(final PathMappingEditForm form, final int expectedMode) {
if (form.crudMode != expectedMode) {
protected void verifyCrudMode(final int crudMode, final int expectedMode) {
if (crudMode != expectedMode) {
throwValidationError(messages -> {
messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(form.crudMode));
messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode));
}, toEditHtml());
}
}

View file

@ -0,0 +1,69 @@
/*
* Copyright 2009-2015 the 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.pathmapping;
import java.io.Serializable;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import org.codelibs.fess.app.web.CrudMode;
import org.codelibs.fess.util.ComponentUtil;
import org.lastaflute.web.validation.Required;
/**
* @author codelibs
* @author Shunji Makino
* @author Keiichi Watanabe
*/
public class CreateForm implements Serializable {
private static final long serialVersionUID = 1L;
public Integer crudMode;
@Required
@Size(max = 1000)
public String regex;
@Required
@Size(max = 1000)
public String replacement;
@Required
public String processType;
@Required
@Min(value = 0)
@Max(value = 2147483647)
public Integer sortOrder;
@Required
@Size(max = 1000)
public String createdBy;
@Required
public Long createdTime;
public void initialize() {
crudMode = CrudMode.CREATE;
sortOrder = 0;
createdBy = ComponentUtil.getSystemHelper().getUsername();
createdTime = ComponentUtil.getSystemHelper().getCurrentTimeAsLong();
}
}

View file

@ -0,0 +1,42 @@
/*
* Copyright 2009-2015 the 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.pathmapping;
import javax.validation.constraints.Size;
import org.lastaflute.web.validation.Required;
/**
* @author Keiichi Watanabe
*/
public class EditForm extends CreateForm {
private static final long serialVersionUID = 1L;
@Required
@Size(max = 1000)
public String id;
@Size(max = 1000)
public String updatedBy;
public Long updatedTime;
@Required
public Integer versionNo;
}

View file

@ -1,84 +0,0 @@
/*
* Copyright 2009-2015 the 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.pathmapping;
import java.io.Serializable;
import org.codelibs.fess.util.ComponentUtil;
/**
* @author codelibs
* @author Shunji Makino
*/
public class PathMappingEditForm implements Serializable {
private static final long serialVersionUID = 1L;
//@IntegerType
public int crudMode;
//@Required(target = "confirmfromupdate,update,delete")
//@Maxbytelength(maxbytelength = 1000)
public String id;
//@Required(target = "confirmfromcreate,create,confirmfromupdate,update,delete")
//@Maxbytelength(maxbytelength = 1000)
public String regex;
//@Required(target = "confirmfromcreate,create,confirmfromupdate,update,delete")
//@Maxbytelength(maxbytelength = 1000)
public String replacement;
//@Required(target = "confirmfromcreate,create,confirmfromupdate,update,delete")
public String processType;
//@Required(target = "confirmfromupdate,update,delete")
//@IntRange(min = 0, max = 2147483647)
public String sortOrder;
//@Required(target = "confirmfromupdate,update,delete")
//@Maxbytelength(maxbytelength = 255)
public String createdBy;
//@Required(target = "confirmfromupdate,update,delete")
//@LongType
public String createdTime;
//@Maxbytelength(maxbytelength = 255)
public String updatedBy;
//@LongType
public String updatedTime;
//@Required(target = "confirmfromupdate,update,delete")
//@LongType
public String versionNo;
public void initialize() {
id = null;
regex = null;
replacement = null;
processType = null;
sortOrder = null;
createdBy = "system";
createdTime = Long.toString(ComponentUtil.getSystemHelper().getCurrentTimeAsLong());
updatedBy = null;
updatedTime = null;
versionNo = null;
sortOrder = "0";
}
}

View file

@ -17,16 +17,14 @@
package org.codelibs.fess.app.web.admin.pathmapping;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* @author codelibs
* @author Shunji Makino
*/
public class PathMappingSearchForm implements Serializable {
public class SearchForm implements Serializable {
private static final long serialVersionUID = 1L;
public Map<String, String> searchParams = new HashMap<String, String>();
public String id;
}

View file

@ -12,7 +12,7 @@
<jsp:param name="menuCategoryType" value="crawl" />
<jsp:param name="menuType" value="pathMapping" />
</jsp:include>
<div class="content-wrapper">
<%-- Content Header --%>
<section class="content-header">
@ -20,7 +20,7 @@
<la:message key="labels.path_mapping_title_details" />
</h1>
<ol class="breadcrumb">
<li><la:link href="index">
<la:message key="labels.path_mapping_link_list" />
</la:link></li>
@ -102,13 +102,13 @@
<td>
<c:if test="${processType=='C'}">
<la:message key="labels.path_mapping_pt_crawling" />
</c:if>
</c:if>
<c:if test="${processType=='D'}">
<la:message key="labels.path_mapping_pt_displaying" />
</c:if>
<c:if test="${processType=='B'}">
<la:message key="labels.path_mapping_pt_both" />
</c:if>
</c:if>
<la:hidden property="processType" />
</td>
</tr>
@ -122,7 +122,7 @@
<%-- Box Footer --%>
<div class="box-footer">
<c:if test="${crudMode == 1}">
<input type="submit" class="btn" name="editagain" value="<la:message key="labels.path_mapping_button_back"/>" />
<input type="submit" class="btn" name="createagain" value="<la:message key="labels.path_mapping_button_back"/>" />
<input type="submit" class="btn btn-primary" name="create"
value="<la:message key="labels.path_mapping_button_create"/>"
/>