📄 headermap2.java.inwork
字号:
/*
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "OpenWFE" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of The OpenWFE Group. For written permission,
* please contact openwfe@openwfe.org.
*
* 4. Products derived from this Software may not be called "OpenWFE"
* nor may "OpenWFE" appear in their names without prior written
* permission of The OpenWFE Group. OpenWFE is a registered
* trademark of The OpenWFE Group.
*
* 5. Due credit should be given to the OpenWFE Project
* (http://www.openwfe.org/).
*
* THIS SOFTWARE IS PROVIDED BY THE OPENWFE GROUP AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE OPENWFE GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2002 (C) The OpenWFE Group. All Rights Reserved.
*
* $Id: HeaderMap.java,v 1.2 2003/12/21 13:52:12 jmettraux Exp $
*/
//
// HeaderMap.java
//
// jmettraux@openwfe.org
// steven@openwfe.org
//
// generated with
// jtmpl 1.0.04 31.10.2002 John Mettraux (jmettraux@openwfe.org)
//
package openwfe.org.webclient;
import openwfe.org.worklist.Header;
import openwfe.org.expressions.FlowExpressionId;
import java.util.HashMap;
import java.util.List;
/**
* A snapshot of a worklist, store by store.
*
* <p><font size=2>CVS Info :
* <br>$Author: jmettraux $
* <br>$Date: 2003/12/21 13:52:12 $
* <br>$Id: HeaderMap.java,v 1.2 2003/12/21 13:52:12 jmettraux Exp $ </font>
*
* @author jmettraux@openwfe.org
* @author steven@openwfe.org
*/
public class HeaderMap
{
static org.apache.log4j.Logger log =
org.apache.log4j.Logger.getLogger(HeaderMap.class.getName());
//
// CONSTANTS (definitions)
public final static String delimiter = "....";
public final static String delimiterRegex = "\\.\\.\\.\\.";
//
// FIELDS
protected java.util.Map storeMap = new java.util.HashMap();
protected java.util.Set writeSet = new java.util.HashSet();
/**
* modified by steven,to support paging and searching,
* and this will affect the following method:
* 1.putHeaders
* and all the affected code will start with "//steven start"
* and end with //steven end
*/
//steven start
//total headers contained by the header map
private long lTotal = 0;
//items per page
protected int nItemPerPage=20;
//pages that total headers are scattered into
protected int pages = 0;
//a helper structure,used in getHeaders(int page),
// to locate the start point of the page faster
protected java.util.ArrayList pageToStore = new java.util.ArrayList();
public final static String size = "size";
public final static String store = "store";
public int setItemPerPage(int itemPerPage) {
//if itemPerPage <= 0 or > 10000,it will be ignored
if (itemPerPage > 0 && itemPerPage <= 10000) {
nItemPerPage = itemPerPage;
return 0;
}else {
if (itemPerPage > 0 )
nItemPerPage = 10000;
return -1;
}
}
public int getItemPerPage()
{
return this.nItemPerPage;
}
public long getTotal()
{
return this.lTotal;
}
//make sure to call this method BEFORE refreshing stores!!!
public void init ()
{
this.lTotal = 0;
this.pages = 0;
this.pageToStore = new java.util.ArrayList();
}
//make sure to call this method AFTER refreshing stores!!!
public boolean setPages()
{
this.pages = (int)(getTotal() / getItemPerPage());
if (getTotal() % getItemPerPage() != 0) pages++;
if (pages == 0) pages = 1;
return true;
}
public java.util.List getHeaders (int page)
{
java.util.ArrayList headers = new java.util.ArrayList();
if (page < 1 || page > pages) return headers;
int start = (page-1) * getItemPerPage();
int end = start+getItemPerPage()-1;
int pos = 0;
for (int i=0; i<this.pageToStore.size(); i++)
{
HashMap hm = (HashMap)(this.pageToStore.get(i));
int thisSize = Integer.parseInt(hm.get(this.size).toString());
if (pos+thisSize > start && pos<=end)
{
String storeName = hm.get(this.store).toString();
List thisHeaders = this.getHeaders(storeName);
int j = start-pos;
if (j<0)
j=0;
else
pos = start;
for (;j<thisHeaders.size() && pos<=end;j++,pos++)
headers.add(thisHeaders.get(j));
if (pos>end) break;
}
else
{
pos += thisSize;
}
}
return headers;
}
//steven end
//
// CONSTRUCTORS
public HeaderMap ()
{
}
//
// METHODS
public java.util.Set getStores ()
{
return this.storeMap.keySet();
}
public void putHeaders
(String storeName, boolean canWrite, java.util.List headers)
{
//
// tag each header
for (int i=0; i<headers.size(); i++)
{
String workItemId = storeName + delimiter + i;
Header h = (Header)headers.get(i);
h.setStringId(workItemId);
//steven start
/**
* 1.filtering can be done in getHeader(int) method,
* but we'd better remember the start point(in storeMap) of every page,
* and tag the right header(or make a copy from storeMap).
* I don't think this is a good choice.
* 2.or,filters can be added here to make sure that
* all the headers in the headermap meet some criteria.but some garbage
* wasted the network bandwidth.
* 3.BUT,it is recomended that only the wanted headers are
* returned when refreshing the stores,that's to say,when query the queue.
* and this has advantage when we do is query the queue and get
* all the needed headers, and then return them to the client side.
* what do u think of it,John,
* and which answer do u prefer ?
*/
//steven end
}
this.storeMap.put(storeName, headers);
//steven start
HashMap hm = new HashMap();
hm.put(this.store, storeName);
hm.put(this.size,String.valueOf(headers.size()));
lTotal+=headers.size();
//if the pageToStore is properly initialized,the code below can be removed
//its purpose is to make sure no reduplication occur
for (int i=0;i<this.pageToStore.size();i++) {
HashMap h = (HashMap)this.pageToStore.get(i);
if (h.get(this.store).toString().equals(storeName)) {
lTotal -= Integer.parseInt(h.get(this.size).toString());
this.pageToStore.remove(i);
break;
}
}
this.pageToStore.add(hm);
//steven end
if (canWrite)
this.writeSet.add(storeName);
else
this.writeSet.remove(storeName);
}
public java.util.List getHeaders (String storeName)
{
return (java.util.List)this.storeMap.get(storeName);
}
public FlowExpressionId parseExpressionId (String expressionId)
throws WebclientException
{
if (expressionId == null) return null;
try
{
String[] ss = expressionId.split(delimiterRegex);
String storeName = ss[0];
String sIndex = ss[1];
int index = Integer.parseInt(sIndex);
java.util.List headers = getHeaders(storeName);
FlowExpressionId result =
((Header)headers.get(index)).getExpressionId();
return result;
}
catch (Exception e)
{
throw new WebclientException
("Failed to retrieve a workItemId", e);
}
}
public boolean canWrite (String storeName)
{
return this.writeSet.contains(storeName);
}
//
// STATIC METHODS
public static String getStoreName (String workItemId)
{
if (workItemId == null) return null;
int i = workItemId.indexOf("....");
if (i < 0) return null;
return workItemId.substring(0, i);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -