001    package org.kuali.student.lum.common.client.widgets;
002    
003    import java.util.ArrayList;
004    import java.util.Arrays;
005    import java.util.Iterator;
006    import java.util.List;
007    
008    import org.kuali.student.r1.common.assembly.data.Data;
009    import org.kuali.student.r2.core.search.dto.SearchParamInfo;
010    import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
011    import org.kuali.student.r2.lum.clu.dto.MembershipQueryInfo;
012    
013    public class CluSetRangeModelUtil {
014        
015        public static CluSetRangeModelUtil INSTANCE = new CluSetRangeModelUtil();
016        
017        private CluSetRangeModelUtil() {
018        }
019        
020        public Data toData(SearchRequestInfo searchRequest, String searchRequestId) {
021            if (searchRequest == null) {
022                return null;
023            }
024            List<SearchParamInfo> searchParams = searchRequest.getParams();
025            CluSetRangeHelper cluSetRangeHelper = CluSetRangeHelper.wrap(new Data());
026            cluSetRangeHelper.setId(searchRequestId);
027            cluSetRangeHelper.setSearchTypeKey(searchRequest.getSearchKey());
028            if (searchParams != null) {
029                for (SearchParamInfo searchParam : searchParams) {
030                    QueryParamHelper queryParamHelper = QueryParamHelper.wrap(new Data());
031                    queryParamHelper.setValue(null);
032                    queryParamHelper.setListValue(null);
033                    queryParamHelper.setKey(searchParam.getKey());
034    
035    
036                    if (searchParam.getValues() != null) {
037    
038                        if (searchParam.getValues().size() == 1) {
039                            queryParamHelper.setValue(searchParam.getValues().get(0));
040                        } else {
041                            for (String param : searchParam.getValues()) {
042                                queryParamHelper.getListValue().add(param);
043                            }
044                        }
045                        if (cluSetRangeHelper.getQueryParams() == null) {
046                            cluSetRangeHelper.setQueryParams(new Data());
047                        }
048                        cluSetRangeHelper.getQueryParams().add(queryParamHelper.getData());
049                    }
050                }
051            }
052            
053            return cluSetRangeHelper.getData();
054        }
055        
056        public Data toData(MembershipQueryInfo membershipQueryInfo) {
057            if (membershipQueryInfo == null) {
058                return null;
059            }
060            List<org.kuali.student.r2.core.search.dto.SearchParamInfo> searchParams = membershipQueryInfo.getQueryParamValues();
061            CluSetRangeHelper cluSetRangeHelper = CluSetRangeHelper.wrap(new Data());
062            cluSetRangeHelper.setId(membershipQueryInfo.getId());
063            cluSetRangeHelper.setSearchTypeKey(membershipQueryInfo.getSearchTypeKey());
064            if (searchParams != null) {
065                for (SearchParamInfo searchParam : searchParams) {
066                    QueryParamHelper queryParamHelper = QueryParamHelper.wrap(new Data());
067                    if(searchParam.getValues() != null) {
068                        if( searchParam.getValues().size() == 1){
069                            queryParamHelper.setValue(searchParam.getValues().get(0));
070                        }
071                        else {
072                            for(String param : searchParam.getValues()){
073                                queryParamHelper.getListValue().add(param);
074                            }
075                        }
076                    }
077                    queryParamHelper.setKey(searchParam.getKey());
078                    if (cluSetRangeHelper.getQueryParams() == null) {
079                        cluSetRangeHelper.setQueryParams(new Data());
080                    }
081                    cluSetRangeHelper.getQueryParams().add(queryParamHelper.getData());
082                }
083            }
084            
085            return cluSetRangeHelper.getData();
086        }
087        
088    //    public SearchRequestInfo toSearchRequest(Data data) {
089    //        SearchRequestInfo searchRequest = null;
090    //        if (data != null) {
091    //            CluSetRangeHelper cluSetRangeHelper = CluSetRangeHelper.wrap(data);
092    //            Data queryParamsData = cluSetRangeHelper.getQueryParams();
093    //            searchRequest = new SearchRequestInfo();
094    //            searchRequest.setSearchKey(cluSetRangeHelper.getSearchTypeKey());
095    //            for (Data.Property p : queryParamsData) {
096    //                QueryParamHelper queryParamHelper = QueryParamHelper.wrap((Data)p.getValue());
097    //                SearchParamInfo searchParam = new SearchParamInfo();
098    //                searchParam.setKey(queryParamHelper.getKey());
099    //                searchParam.setValue(queryParamHelper.getValue());
100    //                if (searchRequest.getParams() == null) {
101    //                    searchRequest.setParams(new ArrayList<SearchParamInfo>());
102    //                }
103    //                searchRequest.getParams().add(searchParam);
104    //            }
105    //        }
106    //        return searchRequest;
107    //    }
108        
109        public MembershipQueryInfo toMembershipQueryInfo(Data data) {
110            MembershipQueryInfo membershipQueryInfo = null;
111            if (data != null) {
112                CluSetRangeHelper cluSetRangeHelper = CluSetRangeHelper.wrap(data);
113                Data queryParamsData = cluSetRangeHelper.getQueryParams();
114                membershipQueryInfo = new MembershipQueryInfo();
115                membershipQueryInfo.setId(cluSetRangeHelper.getId());
116                membershipQueryInfo.setSearchTypeKey(cluSetRangeHelper.getSearchTypeKey());
117                // make sure the membershipQueryInfo has some contents in it.
118                // Return null if otherwise.
119                if (membershipQueryInfo.getSearchTypeKey() == null ||
120                        membershipQueryInfo.getSearchTypeKey().trim().isEmpty()) {
121                    return null;
122                }
123                for (Data.Property p : queryParamsData) {
124                    QueryParamHelper queryParamHelper = QueryParamHelper.wrap((Data)p.getValue());
125                    org.kuali.student.r2.core.search.dto.SearchParamInfo searchParam = new org.kuali.student.r2.core.search.dto.SearchParamInfo();
126                    searchParam.setKey(queryParamHelper.getKey());
127                    if (queryParamHelper.getValue() != null) {
128                        searchParam.setValues(Arrays.asList(queryParamHelper.getValue()));
129                    }
130                    else
131                    {
132                        for (Iterator<Data.Property> propIter=(queryParamHelper.getListValue()).iterator();propIter.hasNext();) {
133                            Data.Property prop = propIter.next();
134                            searchParam.getValues().add((String) prop.getValue());
135                        }
136                    }
137                    if (membershipQueryInfo.getQueryParamValues() == null) {
138                        membershipQueryInfo.setQueryParamValues(new ArrayList<org.kuali.student.r2.core.search.dto.SearchParamInfo>());
139                    }
140                    membershipQueryInfo.getQueryParamValues().add(searchParam);
141                }
142            }
143            return membershipQueryInfo;
144        }
145    
146    }