modify kuromoji admin page

This commit is contained in:
Shinsuke Sugaya 2015-10-10 08:35:20 +09:00
parent 9860aa827f
commit dd782c9130
7 changed files with 112 additions and 75 deletions

View file

@ -16,6 +16,7 @@
package org.codelibs.fess.app.service;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
@ -23,7 +24,6 @@ import javax.annotation.Resource;
import org.codelibs.core.beans.util.BeanUtil;
import org.codelibs.fess.Constants;
import org.codelibs.fess.app.pager.KuromojiPager;
import org.codelibs.fess.dict.DictionaryExpiredException;
import org.codelibs.fess.dict.DictionaryFile.PagingList;
import org.codelibs.fess.dict.DictionaryManager;
import org.codelibs.fess.dict.kuromoji.KuromojiFile;
@ -35,43 +35,41 @@ public class KuromojiService {
protected DictionaryManager dictionaryManager;
public List<KuromojiItem> getKuromojiList(final String dictId, final KuromojiPager kuromojiPager) {
final KuromojiFile kuromojiFile = getKuromojiFile(dictId);
return getKuromojiFile(dictId).map(file -> {
final int pageSize = kuromojiPager.getPageSize();
final PagingList<KuromojiItem> userDictList = file.selectList((kuromojiPager.getCurrentPageNumber() - 1) * pageSize, pageSize);
final int pageSize = kuromojiPager.getPageSize();
final PagingList<KuromojiItem> userDictList =
kuromojiFile.selectList((kuromojiPager.getCurrentPageNumber() - 1) * pageSize, pageSize);
// update pager
BeanUtil.copyBeanToBean(userDictList, kuromojiPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
userDictList.setPageRangeSize(5);
kuromojiPager.setPageNumberList(userDictList.createPageNumberList());
return userDictList;
// update pager
BeanUtil.copyBeanToBean(userDictList, kuromojiPager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
userDictList.setPageRangeSize(5);
kuromojiPager.setPageNumberList(userDictList.createPageNumberList());
return (List<KuromojiItem>) userDictList;
}).orElseGet(() -> Collections.emptyList());
}
public KuromojiFile getKuromojiFile(final String dictId) {
return dictionaryManager.getDictionaryFile(dictId).filter(file -> file instanceof KuromojiFile).map(file -> (KuromojiFile) file)
.orElseThrow(() -> new DictionaryExpiredException());
public OptionalEntity<KuromojiFile> getKuromojiFile(final String dictId) {
return dictionaryManager.getDictionaryFile(dictId).filter(file -> file instanceof KuromojiFile)
.map(file -> OptionalEntity.of((KuromojiFile) file)).orElse(OptionalEntity.empty());
}
public OptionalEntity<KuromojiItem> getKuromoji(final String dictId, final long id) {
final KuromojiFile kuromojiFile = getKuromojiFile(dictId);
return kuromojiFile.get(id);
return getKuromojiFile(dictId).map(file -> file.get(id).get());
}
public void store(final String dictId, final KuromojiItem kuromojiItem) {
final KuromojiFile kuromojiFile = getKuromojiFile(dictId);
if (kuromojiItem.getId() == 0) {
kuromojiFile.insert(kuromojiItem);
} else {
kuromojiFile.update(kuromojiItem);
}
getKuromojiFile(dictId).ifPresent(file -> {
if (kuromojiItem.getId() == 0) {
file.insert(kuromojiItem);
} else {
file.update(kuromojiItem);
}
});
}
public void delete(final String dictId, final KuromojiItem kuromojiItem) {
final KuromojiFile kuromojiFile = getKuromojiFile(dictId);
kuromojiFile.delete(kuromojiItem);
getKuromojiFile(dictId).ifPresent(file -> {
file.delete(kuromojiItem);
});
}
}

View file

@ -16,6 +16,10 @@
package org.codelibs.fess.app.web.admin.dict.kuromoji;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.annotation.Resource;
import org.codelibs.core.beans.util.BeanUtil;
@ -33,11 +37,13 @@ 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.ActionResponse;
import org.lastaflute.web.response.HtmlResponse;
import org.lastaflute.web.response.render.RenderData;
import org.lastaflute.web.validation.VaErrorHook;
/**
* @author shinsuke
* @author Keiichi Watanabe
*/
public class AdminDictKuromojiAction extends FessAdminAction {
@ -256,14 +262,27 @@ public class AdminDictKuromojiAction extends FessAdminAction {
op.setup(form -> {
form.dictId = dictId;
});
}).renderWith(data -> {
kuromojiService.getKuromojiFile(dictId).ifPresent(file -> {
data.register("path", file.getPath());
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsFailedToDownloadKuromojiFile(GLOBAL), toIndexHtml());
});
});
}
@Token(save = false, validate = true)
@Execute
public HtmlResponse download(final DownloadForm form) {
// TODO Download
return asHtml(path_AdminDictKuromoji_DownloadJsp);
public ActionResponse download(final DownloadForm form) {
validate(form, messages -> {}, () -> downloadpage(form.dictId));
return kuromojiService.getKuromojiFile(form.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentType("text/plain; charset=UTF-8").stream(out -> {
out.write(file.getInputStream());
});
}).orElseGet(() -> {
throwValidationError(messages -> messages.addErrorsFailedToDownloadKuromojiFile(GLOBAL), () -> downloadpage(form.dictId));
return null;
});
}
// -----------------------------------------------------
@ -276,14 +295,34 @@ public class AdminDictKuromojiAction extends FessAdminAction {
op.setup(form -> {
form.dictId = dictId;
});
}).renderWith(data -> {
kuromojiService.getKuromojiFile(dictId).ifPresent(file -> {
data.register("path", file.getPath());
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsFailedToDownloadKuromojiFile(GLOBAL), toIndexHtml());
});
});
}
@Token(save = false, validate = true)
@Execute
public HtmlResponse upload(final UploadForm form) {
// TODO
return redirectWith(getClass(), moreUrl("list/1").params("dictId", form.dictId));
validate(form, messages -> {}, () -> uploadpage(form.dictId));
return kuromojiService.getKuromojiFile(form.dictId).map(file -> {
try (InputStream inputStream = form.kuromojiFile.getInputStream()) {
file.update(inputStream);
} catch (IOException e) {
throwValidationError(messages -> messages.addErrorsFailedToUploadKuromojiFile(GLOBAL), () -> {
return redirectWith(getClass(), moreUrl("uploadpage/" + form.dictId));
});
}
saveInfo(messages -> messages.addSuccessUploadKuromojiFile(GLOBAL));
return redirectWith(getClass(), moreUrl("uploadpage/" + form.dictId));
}).orElseGet(() -> {
throwValidationError(messages -> messages.addErrorsFailedToUploadKuromojiFile(GLOBAL), () -> uploadpage(form.dictId));
return null;
});
}
// -----------------------------------------------------

View file

@ -22,16 +22,17 @@ import org.lastaflute.web.ruts.multipart.MultipartFormFile;
import org.lastaflute.web.validation.Required;
/**
* @author codelibs
* @author shinsuke
* @author Keiichi Watanabe
*/
public class UploadForm implements Serializable {
private static final long serialVersionUID = 1L;
public MultipartFormFile kuromojiFile;
@Required
public String dictId;
@Required
public MultipartFormFile kuromojiFile;
}

View file

@ -16,12 +16,14 @@
package org.codelibs.fess.dict.kuromoji;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
@ -60,7 +62,7 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
@Override
public OptionalEntity<KuromojiItem> get(final long id) {
if (kuromojiItemList == null) {
reload(null);
reload(null, null);
}
for (final KuromojiItem kuromojiItem : kuromojiItemList) {
@ -74,7 +76,7 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
@Override
public synchronized PagingList<KuromojiItem> selectList(final int offset, final int size) {
if (kuromojiItemList == null) {
reload(null);
reload(null, null);
}
if (offset >= kuromojiItemList.size() || offset < 0) {
@ -92,14 +94,14 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
@Override
public synchronized void insert(final KuromojiItem item) {
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
reload(updater);
reload(updater, null);
}
}
@Override
public synchronized void update(final KuromojiItem item) {
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
reload(updater);
reload(updater, null);
}
}
@ -108,15 +110,14 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
final KuromojiItem kuromojiItem = item;
kuromojiItem.setNewToken(StringUtil.EMPTY);
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
reload(updater);
reload(updater, null);
}
}
protected void reload(final KuromojiUpdater updater) {
protected void reload(final KuromojiUpdater updater, InputStream in) {
final List<KuromojiItem> itemList = new ArrayList<KuromojiItem>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(dictionaryManager.getContentInputStream(this), Constants.UTF_8));
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(in != null ? in : dictionaryManager.getContentInputStream(this), Constants.UTF_8))) {
long id = 0;
String line = null;
while ((line = reader.readLine()) != null) {
@ -171,8 +172,6 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
kuromojiItemList = itemList;
} catch (final IOException e) {
throw new DictionaryException("Failed to parse " + path, e);
} finally {
IOUtils.closeQuietly(reader);
}
}
@ -180,15 +179,15 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
return new File(path).getName();
}
// TODO
// public InputStream getInputStream() throws IOException {
// return new BufferedInputStream(new FileInputStream(file));
// }
//
// public void update(final InputStream in) throws IOException {
// CopyUtil.copy(in, file);
// reload(null);
// }
public InputStream getInputStream() throws IOException {
return new BufferedInputStream(dictionaryManager.getContentInputStream(this));
}
public void update(final InputStream in) throws IOException {
try (KuromojiUpdater updater = new KuromojiUpdater(null)) {
reload(updater, in);
}
}
@Override
public String toString() {
@ -220,7 +219,7 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
public KuromojiItem write(final KuromojiItem oldItem) {
try {
if (item.getId() == oldItem.getId() && item.isUpdated()) {
if (item != null && item.getId() == oldItem.getId() && item.isUpdated()) {
if (item.equals(oldItem)) {
try {
if (!item.isDeleted()) {
@ -259,7 +258,7 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
public KuromojiItem commit() {
isCommit = true;
if (item.isUpdated()) {
if (item != null && item.isUpdated()) {
try {
writer.write(item.toLineString());
writer.write(Constants.LINE_SEPARATOR);

View file

@ -21,7 +21,7 @@
<la:message key="labels.dict_kuromoji_title" />
</h1>
<ol class="breadcrumb">
<li class="active"><la:link href="index">
<li class="active"><la:link href="list/0?dictId=${dictId}">
<la:message key="labels.dict_kuromoji_link_download" />
</la:link></li>
</ol>
@ -39,27 +39,27 @@
</h3>
<div class="box-tools pull-right">
<span class="label label-default">
<la:link href="../index">
<la:link href="/admin/dict/">
<la:message key="labels.dict_list_link" />
</la:link>
</span>
<span class="label label-default">
<a href="#">
<la:link href="list/0/?dictId=${f:u(dictId)}">
<la:message key="labels.dict_kuromoji_list_link" />
</a>
</la:link>
</span>
<span class="label label-default">
<la:link href="createpage?dictId=${f:u(dictId)}">
<la:link href="createpage/${f:u(dictId)}">
<la:message key="labels.dict_kuromoji_link_create" />
</la:link>
</span>
<span class="label label-default">
<la:link href="downloadpage?dictId=${f:u(dictId)}">
<a href="#">
<la:message key="labels.dict_kuromoji_link_download" />
</la:link>
</a>
</span>
<span class="label label-default">
<la:link href="uploadpage?dictId=${f:u(dictId)}">
<la:link href="uploadpage/${f:u(dictId)}">
<la:message key="labels.dict_kuromoji_link_upload" />
</la:link>
</span>
@ -78,13 +78,13 @@
</div>
<%-- Edit Form: BEGIN --%>
<la:form>
<la:form target="_blank">
<div>
<la:hidden property="dictId" />
<table class="table table-bordered table-striped">
<tbody>
<tr>
<th style="">${f:h(filename)}</th>
<th style="">${f:h(path)}</th>
<td style="form-control">
<input type="submit"
class="btn small" name="download"

View file

@ -39,7 +39,7 @@
</h3>
<div class="box-tools pull-right">
<span class="label label-default">
<la:link href="..">
<la:link href="/admin/dict/">
<la:message key="labels.dict_list_link" />
</la:link>
</span>

View file

@ -21,7 +21,7 @@
<la:message key="labels.dict_kuromoji_title" />
</h1>
<ol class="breadcrumb">
<li class="active"><la:link href="index">
<li class="active"><la:link href="list/0?dictId=${dictId}">
<la:message key="labels.dict_kuromoji_link_upload" />
</la:link></li>
</ol>
@ -39,29 +39,29 @@
</h3>
<div class="box-tools pull-right">
<span class="label label-default">
<la:link href="../index">
<la:link href="/admin/dict/">
<la:message key="labels.dict_list_link" />
</la:link>
</span>
<span class="label label-default">
<a href="#">
<la:link href="list/0/?dictId=${f:u(dictId)}">
<la:message key="labels.dict_kuromoji_list_link" />
</a>
</la:link>
</span>
<span class="label label-default">
<la:link href="createpage?dictId=${f:u(dictId)}">
<la:link href="createpage/${f:u(dictId)}">
<la:message key="labels.dict_kuromoji_link_create" />
</la:link>
</span>
<span class="label label-default">
<la:link href="downloadpage?dictId=${f:u(dictId)}">
<la:link href="downloadpage/${f:u(dictId)}">
<la:message key="labels.dict_kuromoji_link_download" />
</la:link>
</span>
<span class="label label-default">
<la:link href="uploadpage?dictId=${f:u(dictId)}">
<a href="#">
<la:message key="labels.dict_kuromoji_link_upload" />
</la:link>
</a>
</span>
</div>
</div>