001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.wicket;
018:
019: import info.jtrac.Jtrac;
020: import info.jtrac.domain.AbstractItem;
021: import java.util.List;
022: import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
023: import org.apache.wicket.markup.html.WebMarkupContainer;
024: import org.apache.wicket.markup.html.basic.Label;
025: import org.apache.wicket.markup.html.form.Button;
026: import org.apache.wicket.markup.html.form.Form;
027: import org.apache.wicket.model.AbstractReadOnlyModel;
028: import org.apache.wicket.model.IModel;
029: import org.apache.wicket.util.time.Duration;
030:
031: /**
032: * rebuild indexes admin option
033: */
034: public class IndexRebuildPage extends BasePage {
035:
036: public IndexRebuildPage(boolean success) {
037: if (success) {
038: add(new Label("heading",
039: localize("index_rebuild_success.message")));
040: add(new WebMarkupContainer("form").setVisible(false));
041: } else {
042: add(new Label("heading", localize("index_rebuild.heading")));
043: add(new RebuildIndexesForm("form"));
044: }
045: }
046:
047: /**
048: * wicket form
049: */
050: private class RebuildIndexesForm extends Form {
051:
052: private int current;
053: private int total;
054: private boolean finished;
055:
056: public RebuildIndexesForm(String id) {
057:
058: super (id);
059:
060: final Label progress = new Label("progress");
061: progress.setOutputMarkupId(true);
062:
063: add(new Button("start") {
064: @Override
065: public void onSubmit() {
066: // hide the button
067: this .setVisible(false);
068: // long running process, use thread
069: new Thread() {
070: // don't serialize this!
071: private transient Jtrac jtrac = getJtrac();
072:
073: public void run() {
074: jtrac.clearIndexes();
075: List<AbstractItem> items = jtrac
076: .findAllItems();
077: total = items.size();
078: for (current = 0; current < total; current++) {
079: jtrac.index(items.get(current));
080: }
081: finished = true;
082: }
083: }.start();
084:
085: // poll and update the progress every 5 seconds
086: progress.add(new AjaxSelfUpdatingTimerBehavior(
087: Duration.seconds(5)));
088: IModel model = new AbstractReadOnlyModel() {
089: public Object getObject() {
090: if (finished) {
091: // reshow the page, with success message
092: setResponsePage(new IndexRebuildPage(
093: true));
094: }
095: int percent = total == 0 ? 0 : 100
096: * current / total;
097: return percent + "% [" + current + " / "
098: + total + "]";
099: };
100: };
101: progress.setModel(model);
102: }
103: });
104:
105: add(progress);
106: }
107:
108: }
109:
110: }
|