Subversion Repositories SmartDukaan

Rev

Rev 34835 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spice.profitmandi.common.enumuration.DateTimePattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.mail.internet.InternetAddress;
import java.text.Normalizer;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
        
        private static ObjectMapper objectMapper = new ObjectMapper();
        
        private static final Logger LOGGER = LogManager.getLogger(StringUtils.class);
        private static final String DATE_PATTERN = "dd/MM/yyyy";
        private static final String GADGET_COP_DATE_PATTERN = "MM/dd/yyyy";
        private static final String DATE_TIME_PATTERN = "dd/MM/yyyy HH:mm:ss";
        private static final String DATE_PATTERN_HYPHENATED = "dd-MM-yyyy";
        private static final String DATE_TIME_ABBR = "dd/MM/yyyy hh:mma";
        private static final DateTimeFormatter SHORT_MONTH_DATE_FORMATTER = DateTimeFormatter.ofPattern("d-MMM-yyyy");
        private static final DateTimeFormatter SHORT_MONTH_DATE_YEAR_FORMATTER = DateTimeFormatter.ofPattern("dd-MMM-yy");
        private StringUtils(){

        }

        public static final LocalDate fromShortMonthDateYear(String dateString) throws DateTimeParseException {
                dateString = dateString.toLowerCase();
                dateString = StringUtils.capitalizeFirstChar(dateString);
                return LocalDate.parse(dateString, SHORT_MONTH_DATE_YEAR_FORMATTER);
        }

        public static final LocalDate fromShortMonthDate(String dateString) throws DateTimeParseException {
                dateString = StringUtils.capitalizeFirstChar(dateString);
                return LocalDate.parse(dateString, SHORT_MONTH_DATE_FORMATTER);
        }

        public static final LocalDate toDate(String dateString)throws DateTimeParseException{
                LOGGER.info("Converting dateString [{}] with pattern[{}]", dateString, DATE_PATTERN);
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
                return LocalDate.parse(dateString, dateTimeFormatter);
        }

        public static final LocalDate toDate(String dateString, String pattern)throws DateTimeParseException{
                LOGGER.info("Converting dateString [{}] with pattern[{}]", dateString, pattern);
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
                return LocalDate.parse(dateString, dateTimeFormatter);
        }

        public static final String toGadgetCopDateString(LocalDate ldt) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(GADGET_COP_DATE_PATTERN);
                return dateTimeFormatter.format(ldt);
        }

        public static final LocalDate fromHypendatedDate(String dateString)throws DateTimeParseException{       
                LOGGER.info("Converting dateString [{}] with pattern[{}]", dateString, DATE_PATTERN_HYPHENATED);
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN_HYPHENATED);
                return LocalDate.parse(dateString, dateTimeFormatter);
        }

        public static final LocalTime toTime(String timeString) throws DateTimeParseException{
                return LocalTime.parse(timeString);
        }

        public static final LocalTime secondsToTime(double totalSeconds) throws DateTimeParseException{
                int totalSecs = (int) Math.round(totalSeconds);
                int hours = totalSecs / 3600;
                int minutes = (totalSecs % 3600) / 60;
                int seconds = totalSecs % 60;
                String timeString = String.format("%02d:%02d:%02d", hours, minutes, seconds);
                return LocalTime.parse(timeString);
        }

        public static final LocalTime timeDifference(LocalTime startTime, LocalTime endTime) throws DateTimeParseException{
                Duration duration = Duration.between(startTime, endTime);
                if (duration.isNegative()) {
                        duration = duration.plusHours(24); // handle case when now is past midnight
                }

                long hours = duration.toHours();
                long minutes = duration.toMinutes() % 60;
                long seconds = duration.getSeconds() % 60;

                return LocalTime.of((int) hours % 24, (int) minutes, (int) seconds);
        }
        
        public static final LocalDateTime toDateTime(long epocTime){
                return Instant.ofEpochMilli(epocTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
        }
        
        public static final String toString(LocalDate localDate){
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
                String formattedDateTime = localDate.format(formatter);
                return formattedDateTime;
        }

        public static final String toHyphenatedString(LocalDate localDate){
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_PATTERN_HYPHENATED);
                String formattedDateTime = localDate.format(formatter);
                return formattedDateTime;
        }
        
        public static final String toString(LocalDateTime localDateTime){
                if(localDateTime == null){
                        return null;
                }
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-YYYY HH:mm:ss");
                return localDateTime.format(formatter);
        }
        
        public static final LocalDateTime toDateTime(String dateTimeString) throws DateTimeParseException{
                if(dateTimeString == null || dateTimeString.equals("0") || dateTimeString.isEmpty()){
                        return null;
                }
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
                return LocalDateTime.parse(dateTimeString, dateTimeFormatter);
        }

        public static final String toLocalDateTime(String dateTimeString) throws DateTimeParseException{
                if(dateTimeString == null || dateTimeString.equals("0") || dateTimeString.isEmpty()){
                        return null;
                }
                LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
                DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(DATE_TIME_ABBR);
                return dateTime.format(outputFormatter);
        }

        public static final LocalDateTime toDateTime(String dateTimeString, DateTimePattern dateTimePattern) throws DateTimeParseException{
                if(dateTimeString == null || dateTimeString.equals("0") || dateTimeString.isEmpty()){
                        return null;
                }
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimePattern.getValue());
                return LocalDateTime.parse(dateTimeString, formatter);
        }

        public static boolean isValidMobile(String mobile){
                try{
                        Long.valueOf(mobile);
                }
                catch(Exception e){
                        return false;
                }

                if (mobile.startsWith("0")){
                        return false;
                }
                if (mobile.length()!=10){
                        return false;
                }
                return true;
        }

        public static boolean isValidEmailAddress(String email) {
                boolean result = true;
                try {
                        InternetAddress emailAddr = new InternetAddress(email);
                        emailAddr.validate();
                } catch (Exception ex) {
                        result = false;
                }
                return result;
        }
        
        public static boolean isValidGstNumber(String gstNumber) {
                if(gstNumber == null || gstNumber.isEmpty() || gstNumber.length() == 15) {
                        return true;
                }
                return false;
        }
        
        public static List<String> getDuplicateElements(List<String> elements){
                List<String> duplicates = new ArrayList<>();
                for(int i = 0; i < elements.size(); i++){
                        for(int j = i + 1; j < elements.size(); j++){
                                if(elements.get(i).equals(elements.get(j))){
                                        duplicates.add(elements.get(i));
                                }
                        }
                }
                return duplicates;
        }
        
        public static String toString(Object object) throws Exception{
                try {
                        return objectMapper.writeValueAsString(object);
                } catch (JsonProcessingException e) {
                        LOGGER.error("Error occured while converting object to json", e);
                        throw e;
                }
        }
        public static boolean isValidPinCode(String pinCode){
                if(pinCode == null || pinCode.isEmpty()){
                        return false;
                }
                if(pinCode.length() != 6){
                        return false;
                }
                if(pinCode.startsWith("00") || pinCode.startsWith("01") || pinCode.startsWith("10")){
                        return false;
                }
                return true;
                
        }
        
        public static String generatePolicyNumber(String prefix, int sequence){
                String policyNumber = String.format(prefix + "%06d", sequence);
                LOGGER.info("Generated Policy Number [{}]", policyNumber);
                return policyNumber;
        }
        
        public static String generateFofoStoreSequence(String prefix, int sequence){
                String fofoStoreSequenceNumber = String.format(prefix + "%03d", sequence);
                LOGGER.info("Generated Fofo Store Sequence Number [{}]", fofoStoreSequenceNumber);
                return fofoStoreSequenceNumber;
        }
        
        public static String generateRechageRequestId(String prefix, int sequence){
                String oxigenRechargeRequestId = String.format(prefix + "%06d", sequence);
                LOGGER.info("Generated Recharge Request Id [{}]", oxigenRechargeRequestId);
                return oxigenRechargeRequestId;
        }
        
        public static String generateDeliveryNoteId(String prefix, int sequence){
                String deliveryNoteId = String.format(prefix + "%06d", sequence);
                LOGGER.info("Generated Delivery Note Id [{}]", deliveryNoteId);
                return deliveryNoteId;
        }
        
        public static String toOxigenRechargeRequestDate(LocalDateTime now){
                StringBuilder oxigenRechargeRequestDate = new StringBuilder();
                oxigenRechargeRequestDate.append(now.getYear());
                oxigenRechargeRequestDate.append(now.getMonthValue());
                oxigenRechargeRequestDate.append(now.getDayOfMonth());
                oxigenRechargeRequestDate.append(now.getHour());
                oxigenRechargeRequestDate.append(now.getMinute());
                oxigenRechargeRequestDate.append(now.getSecond());
                return oxigenRechargeRequestDate.toString();
        }
        
        public static LocalDateTime fromOxigenRechargeRequestDate(String oxigenRechargeRequestDate){
                int year = Integer.parseInt(oxigenRechargeRequestDate.substring(0, 3));
                int month = Integer.parseInt(oxigenRechargeRequestDate.substring(4, 5));
                int dayOfMonth = Integer.parseInt(oxigenRechargeRequestDate.substring(6, 7));
                int hour = Integer.parseInt(oxigenRechargeRequestDate.substring(8, 9));
                int minute = Integer.parseInt(oxigenRechargeRequestDate.substring(10, 11));
                int second = Integer.parseInt(oxigenRechargeRequestDate.substring(12, 13));
                return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second);
        }

        public static String capitalizeFirstChar(String input) {
                if (input == null || input.isEmpty()) {
                        return input; // Return the input if it's null or empty
                }

                // Define the regular expression to match the first alphabetic character
                Pattern pattern = Pattern.compile("([a-zA-Z])");
                Matcher matcher = pattern.matcher(input);

                // If the first character matches, capitalize it
                if (matcher.find()) {
                        return matcher.replaceFirst(matcher.group(1).toUpperCase());
                }

                return input; // If no match is found, return the original string
        }

        public static String formatDistance(double distanceInMeters) {
                if (distanceInMeters <= 950) {
                        int roundedMeters = ((int) Math.ceil(distanceInMeters / 100.0)) * 100;
                        double distanceKm = roundedMeters / 1000.0;
                        return String.format("%.2f", distanceKm);
                } else {
                        return String.format("%.2f", distanceInMeters / 1000.0);
                }
        }

        public static String toSlug(String input) {
                if (input == null) return "";
                String normalized = Normalizer.normalize(input, Normalizer.Form.NFD).replaceAll("\\p{M}", "");
                String lowerCase = normalized.toLowerCase(Locale.ENGLISH);
                String slug = lowerCase.replaceAll("[^a-z0-9]+", "-");
                slug = slug.replaceAll("^-+|-+$", "");
                return slug;
        }

}