JspHelper.java
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.server.mvc;
import java.text.DateFormatSymbols;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.security.model.AclSid;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.server.model.impl.Country;
import org.genesys.server.model.impl.Crop;
import org.genesys.server.model.impl.User;
import org.genesys.server.service.CropService;
import org.genesys.server.service.GeoService;
import org.genesys.server.service.HtmlConverter;
import org.genesys.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@Component
public class JspHelper {
private static final String regExpr = "[^\\w]+";
private static final String delimiter = "-";
@Autowired
private UserService userService;
@Autowired
private CustomAclService aclService;
@Autowired
private GeoService geoService;
@Autowired
private CropService cropService;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private HtmlConverter htmlConverter;
public String userFullName(Long userId) {
if (userId == null) {
return null;
}
final User user = userService.getUser(userId);
if (user != null) {
return user.getFullName();
} else {
return null;
}
}
public User userByUuid(String uuid) {
if (uuid == null) {
return null;
}
return userService.getUser(UUID.fromString(uuid));
}
public AclSid aclSidById(Long id) {
if (id == null) {
return null;
}
return aclService.getSid(id);
}
public Country getCountry(String iso3) {
return geoService.getCountry(iso3);
}
public Crop getCrop(String shortName) {
return cropService.getCrop(shortName);
}
public List<Crop> getCrops(List<String> cropNames) {
return cropService.getCrops(cropNames);
}
public String toJson(Object object) throws JsonProcessingException {
return objectMapper.writer().writeValueAsString(object);
}
public String htmlToText(String html) {
return htmlConverter.toText(html);
}
public String htmlToText(String html, int maxLength) {
return StringUtils.abbreviate(htmlConverter.toText(html), maxLength);
}
public String abbreviate(String text, int maxLength) {
return StringUtils.abbreviate(text, maxLength);
}
public String[] monthNames(Locale locale) {
DateFormatSymbols dfs = new DateFormatSymbols(locale);
return dfs.getMonths();
}
public String[] monthShortNames(Locale locale) {
DateFormatSymbols dfs = new DateFormatSymbols(locale);
return dfs.getShortMonths();
}
public int randomInt(int min, int max) {
return RandomUtils.nextInt(min, max + 1);
}
public String suggestUrlForText(String text) {
return htmlToText(text, 60).replaceAll(regExpr, delimiter).toLowerCase().replaceAll("\\-+$", "");
}
}