001: package org.openedit.store.products;
002:
003: import org.openedit.store.ProductPathFinder;
004:
005: /**
006: * An implementation of {@link ProductPathFinder} that places products in
007: * subdirectories based on segments of their ID. For example, a product with ID
008: * <tt>abcdefghij</tt> might become <tt>abc/def/abcdefghij</tt>, if
009: * the segment length is 3 and the maximum number of segments is 2.
010: *
011: * @author Eric Galluzzo
012: */
013: public class SegmentedProductPathFinder implements ProductPathFinder {
014: protected boolean fieldGroupInTopCategory; //_
015: protected int fSegmentLength = 0;
016: protected String fieldPrefix;
017: protected boolean fieldReverse;
018:
019: public boolean isReverse() {
020: return fieldReverse;
021: }
022:
023: public void setReverse(boolean inReverse) {
024: fieldReverse = inReverse;
025: }
026:
027: /**
028: * Creates a segmented product path finder that will never use
029: * subdirectories.
030: */
031: public SegmentedProductPathFinder() {
032: }
033:
034: /**
035: * Creates a segmented product path finder that will create segments of the
036: * given length up to the given maximum.
037: *
038: * @param inSegmentLength
039: * The length of each path segment (e.g. 3 for
040: * <tt>abc/def/...</tt>)
041: * @param inMaxSegments
042: * The maximum number of segments
043: */
044: public SegmentedProductPathFinder(int inSegmentLength) {
045: fSegmentLength = inSegmentLength;
046: }
047:
048: public int getSegmentLength() {
049: return fSegmentLength;
050: }
051:
052: public void setSegmentLength(int inSegmentLength) {
053: fSegmentLength = inSegmentLength;
054: }
055:
056: public String idToPath(String inProductId) {
057: if (inProductId == null) {
058: return null;
059: }
060:
061: if (getSegmentLength() == 0 && getPrefix() == null
062: && !isGroupInTopCategory()) {
063: return inProductId;
064: }
065:
066: StringBuffer sb = new StringBuffer();
067: if (getPrefix() != null) {
068: sb.append(getPrefix());
069: }
070: if (isGroupInTopCategory()) {
071: int unds = inProductId.lastIndexOf('_');
072: if (unds > -1) {
073: if (sb.length() > 0) {
074: sb.append("/");
075: }
076: String group = inProductId.substring(unds + 1);
077:
078: sb.append(group); //directory
079: sb.append("/");
080:
081: }
082: }
083: if (getSegmentLength() > 0) {
084: if (isReverse()) {
085: if (isGroupInTopCategory()) {
086: int unds = inProductId.lastIndexOf('_');
087: if (unds == -1) {
088: unds = inProductId.length();
089: }
090: String chunk = inProductId.substring(unds
091: - getSegmentLength(), unds);
092: sb.append(chunk);
093: } else {
094: String chunk = inProductId.substring(inProductId
095: .length()
096: - getSegmentLength());
097: sb.append(chunk);
098: }
099: } else {
100: String chunk = inProductId.substring(0,
101: getSegmentLength());
102: sb.append(chunk);
103: }
104: sb.append("/");
105: }
106:
107: // for ( int segmentCount = 0; segmentCount < getMaxSegments(); segmentCount++ )
108: // {
109: // int index = segmentCount * getSegmentLength();
110: // if ( index + getSegmentLength() >= inProductId.length() )
111: // {
112: // break;
113: // }
114: // sb.append( inProductId
115: // .substring( index, index + getSegmentLength() ) );
116: // sb.append( "/" );
117: // }
118:
119: sb.append(inProductId);
120:
121: return sb.toString();
122: }
123:
124: public String getPrefix() {
125: return fieldPrefix;
126: }
127:
128: public void setPrefix(String inPrefix) {
129: fieldPrefix = inPrefix;
130: }
131:
132: public boolean isGroupInTopCategory() {
133: return fieldGroupInTopCategory;
134: }
135:
136: public void setGroupInTopCategory(boolean inGroupInTopCategory) {
137: fieldGroupInTopCategory = inGroupInTopCategory;
138: }
139: }
|