001: package com.technoetic.xplanner.domain;
002:
003: import com.technoetic.xplanner.util.StringUtilities;
004:
005: /**
006: * Created by IntelliJ IDEA.
007: * User: tkmower
008: * Date: Nov 29, 2004
009: * Time: 4:47:59 PM
010: */
011: public class SearchResult extends Object {
012: private String matchPrefix = "";
013: private String matchSuffix = "";
014: private String matchingText = "";
015: private String resultType;
016: private Nameable matchingObject;
017: private boolean matchInDescription = false;
018: private String attachedToId;
019: private String attachedToDomainType;
020:
021: public SearchResult(Nameable matchingObject, String resultType) {
022: this .matchingObject = matchingObject;
023: this .resultType = resultType;
024: }
025:
026: public boolean equals(Object o) {
027: if (this == o)
028: return true;
029: if (!(o instanceof SearchResult))
030: return false;
031:
032: final SearchResult searchResult = (SearchResult) o;
033:
034: if (getDomainObjectId() != searchResult.getDomainObjectId())
035: return false;
036:
037: return true;
038: }
039:
040: public String getMatchPrefix() {
041: return matchPrefix;
042: }
043:
044: public void setMatchPrefix(String matchPrefix) {
045: this .matchPrefix = matchPrefix;
046: }
047:
048: public String getMatchSuffix() {
049: return matchSuffix;
050: }
051:
052: public void setMatchSuffix(String matchSuffix) {
053: this .matchSuffix = matchSuffix;
054: }
055:
056: public Object getMatchingObject() {
057: return matchingObject;
058: }
059:
060: public String getMatchingText() {
061: return matchingText;
062: }
063:
064: public void setMatchingText(String matchingText) {
065: this .matchingText = matchingText;
066: }
067:
068: public String getResultType() {
069: return resultType;
070: }
071:
072: public String getTitle() {
073: return matchingObject.getName();
074: }
075:
076: public int hashCode() {
077: return getDomainObjectId();
078: }
079:
080: public int getDomainObjectId() {
081: return matchingObject.getId();
082: }
083:
084: public boolean isMatchInDescription() {
085: return matchInDescription;
086: }
087:
088: public void setMatchInDescription(boolean matchInDescription) {
089: this .matchInDescription = matchInDescription;
090: }
091:
092: public void populate(String searchCriteria,
093: int desiredDescriptionLines, int maxSuffixLength) {
094: if (getLocationOfCriteria(searchCriteria) < 0) {
095: populateResultWithNoDescriptionMatch(desiredDescriptionLines);
096: } else {
097: populateResultWithPrefixSuffixAndResult(searchCriteria,
098: maxSuffixLength);
099: }
100: }
101:
102: private int getLocationOfCriteria(String searchCriteria) {
103: return matchingObject.getDescription().toLowerCase().indexOf(
104: searchCriteria.toLowerCase());
105: }
106:
107: private void populateResultWithNoDescriptionMatch(
108: int desiredDescriptionLines) {
109: setMatchingText(StringUtilities.getFirstNLines(matchingObject
110: .getDescription(), desiredDescriptionLines));
111: }
112:
113: private void populateResultWithPrefixSuffixAndResult(
114: String searchCriteria, int maxSuffixLength) {
115: populate(searchCriteria, maxSuffixLength);
116: }
117:
118: public void populate(String searchCriteria, int maxSuffixLength) {
119: setMatchInDescription(true);
120: String description = matchingObject.getDescription();
121: setMatchPrefix(StringUtilities.computePrefix(description,
122: searchCriteria, maxSuffixLength));
123:
124: int beginIndex = getLocationOfCriteria(searchCriteria);
125: setMatchingText(description.substring(beginIndex, beginIndex
126: + searchCriteria.length()));
127:
128: setMatchSuffix(StringUtilities.computeSuffix(description,
129: searchCriteria, maxSuffixLength));
130: }
131:
132: public void setAttachedToId(String attachedToId) {
133: this .attachedToId = attachedToId;
134: }
135:
136: public String getAttachedToId() {
137: return attachedToId;
138: }
139:
140: public void setAttachedToDomainType(String attachedToDomainType) {
141: this .attachedToDomainType = attachedToDomainType;
142: }
143:
144: public String getAttachedToDomainType() {
145: return attachedToDomainType;
146: }
147: }
|