Subversion Repositories SmartDukaan

Rev

Rev 36893 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36893 Rev 37019
Line 1828... Line 1828...
1828
                                             @RequestParam(name = "area", defaultValue = "") String area,
1828
                                             @RequestParam(name = "area", defaultValue = "") String area,
1829
                                             @RequestParam(name = "baseArea", defaultValue = "") String baseArea,
1829
                                             @RequestParam(name = "baseArea", defaultValue = "") String baseArea,
1830
                                             @RequestParam(name = "name", defaultValue = "") String name,
1830
                                             @RequestParam(name = "name", defaultValue = "") String name,
1831
                                             @RequestParam(name = "designation", defaultValue = "") String designation,
1831
                                             @RequestParam(name = "designation", defaultValue = "") String designation,
1832
                                             @RequestParam(name = "mobile", defaultValue = "") String mobile,
1832
                                             @RequestParam(name = "mobile", defaultValue = "") String mobile,
-
 
1833
                                             @RequestParam(name = "email", defaultValue = "") String email,
-
 
1834
                                             @RequestParam(name = "position", defaultValue = "") String position,
1833
                                             @RequestParam(name = "email", defaultValue = "") String email) throws Exception {
1835
                                             @RequestParam(name = "afterId", defaultValue = "0") int afterId) throws Exception {
1834
        Map<String, Object> response = new HashMap<>();
1836
        Map<String, Object> response = new HashMap<>();
1835
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
1837
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
1836
        if (!isContactUsEditor(loginDetails)) {
1838
        if (!isContactUsEditor(loginDetails)) {
1837
            response.put("success", false);
1839
            response.put("success", false);
1838
            response.put("message", "You are not authorised to edit contacts.");
1840
            response.put("message", "You are not authorised to edit contacts.");
Line 1869... Line 1871...
1869
        contact.setDesignation(designation == null ? "" : designation.trim());
1871
        contact.setDesignation(designation == null ? "" : designation.trim());
1870
        contact.setMobile(mobile == null || mobile.trim().isEmpty() ? "-" : mobile.trim());
1872
        contact.setMobile(mobile == null || mobile.trim().isEmpty() ? "-" : mobile.trim());
1871
        contact.setEmail(email == null ? "" : email.trim());
1873
        contact.setEmail(email == null ? "" : email.trim());
1872
        contactUsRepository.persist(contact);
1874
        contactUsRepository.persist(contact);
1873
 
1875
 
-
 
1876
        // Position control: place the new/edited contact within its section. A NEW contact defaults
-
 
1877
        // to the bottom; an EDIT keeps its place unless the editor explicitly chose a new position.
-
 
1878
        String pos = (position == null || position.trim().isEmpty())
-
 
1879
                ? (id > 0 ? "KEEP" : "BOTTOM")
-
 
1880
                : position.trim().toUpperCase();
-
 
1881
        if (!"KEEP".equals(pos)) {
-
 
1882
            repositionInSection(section, contact.getId(), pos, afterId);
-
 
1883
        }
-
 
1884
 
1874
        response.put("success", true);
1885
        response.put("success", true);
1875
        return response;
1886
        return response;
1876
    }
1887
    }
1877
 
1888
 
1878
    @RequestMapping(value = "/contactUs/delete", method = RequestMethod.POST)
1889
    @RequestMapping(value = "/contactUs/delete", method = RequestMethod.POST)
Line 1896... Line 1907...
1896
        contactUsRepository.persist(contact);
1907
        contactUsRepository.persist(contact);
1897
        response.put("success", true);
1908
        response.put("success", true);
1898
        return response;
1909
        return response;
1899
    }
1910
    }
1900
 
1911
 
-
 
1912
    @RequestMapping(value = "/contactUs/reorder", method = RequestMethod.POST)
-
 
1913
    @ResponseBody
-
 
1914
    public Map<String, Object> reorderContactUs(HttpServletRequest request,
-
 
1915
                                                @RequestParam(name = "id") int id,
-
 
1916
                                                @RequestParam(name = "direction") String direction) throws Exception {
-
 
1917
        Map<String, Object> response = new HashMap<>();
-
 
1918
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
-
 
1919
        if (!isContactUsEditor(loginDetails)) {
-
 
1920
            response.put("success", false);
-
 
1921
            response.put("message", "You are not authorised to edit contacts.");
-
 
1922
            return response;
-
 
1923
        }
-
 
1924
        ContactUs contact = contactUsRepository.selectById(id);
-
 
1925
        if (contact == null || !contact.isActive()) {
-
 
1926
            response.put("success", false);
-
 
1927
            response.put("message", "Contact not found.");
-
 
1928
            return response;
-
 
1929
        }
-
 
1930
        boolean up = "UP".equalsIgnoreCase(direction);
-
 
1931
        List<ContactUs> list = contactUsRepository.findActiveBySection(contact.getSection());
-
 
1932
        int idx = indexOfContact(list, id);
-
 
1933
        int target = up ? idx - 1 : idx + 1;
-
 
1934
        if (idx < 0 || target < 0 || target >= list.size()) {
-
 
1935
            // Already at the top/bottom edge - nothing to move.
-
 
1936
            response.put("success", true);
-
 
1937
            return response;
-
 
1938
        }
-
 
1939
        ContactUs tmp = list.get(idx);
-
 
1940
        list.set(idx, list.get(target));
-
 
1941
        list.set(target, tmp);
-
 
1942
        renumberSequential(list);
-
 
1943
        response.put("success", true);
-
 
1944
        return response;
-
 
1945
    }
-
 
1946
 
-
 
1947
    /**
-
 
1948
     * Places {@code contactId} within its section per {@code position} (TOP | BOTTOM | AFTER) and
-
 
1949
     * renumbers the whole section's sort_order to a clean 10, 20, 30 … sequence, so ordering is
-
 
1950
     * always gap-free and deterministic. {@code afterId} is only consulted for AFTER.
-
 
1951
     */
-
 
1952
    private void repositionInSection(String section, int contactId, String position, int afterId) {
-
 
1953
        List<ContactUs> list = contactUsRepository.findActiveBySection(section);
-
 
1954
        int movingIdx = indexOfContact(list, contactId);
-
 
1955
        if (movingIdx < 0) {
-
 
1956
            return; // not found (should not happen immediately after persist)
-
 
1957
        }
-
 
1958
        ContactUs moving = list.remove(movingIdx);
-
 
1959
        int index;
-
 
1960
        if ("TOP".equals(position)) {
-
 
1961
            index = 0;
-
 
1962
        } else if ("AFTER".equals(position)) {
-
 
1963
            int anchorIdx = indexOfContact(list, afterId);
-
 
1964
            index = (anchorIdx >= 0) ? anchorIdx + 1 : list.size();
-
 
1965
        } else { // BOTTOM
-
 
1966
            index = list.size();
-
 
1967
        }
-
 
1968
        list.add(index, moving);
-
 
1969
        renumberSequential(list);
-
 
1970
    }
-
 
1971
 
-
 
1972
    /** Assigns sort_order 10, 20, 30 … in list order, persisting only rows whose order changed. */
-
 
1973
    private void renumberSequential(List<ContactUs> orderedContacts) {
-
 
1974
        for (int i = 0; i < orderedContacts.size(); i++) {
-
 
1975
            ContactUs c = orderedContacts.get(i);
-
 
1976
            int newOrder = (i + 1) * 10;
-
 
1977
            if (c.getSortOrder() != newOrder) {
-
 
1978
                c.setSortOrder(newOrder);
-
 
1979
                contactUsRepository.persist(c);
-
 
1980
            }
-
 
1981
        }
-
 
1982
    }
-
 
1983
 
-
 
1984
    private int indexOfContact(List<ContactUs> list, int id) {
-
 
1985
        for (int i = 0; i < list.size(); i++) {
-
 
1986
            if (list.get(i).getId() == id) {
-
 
1987
                return i;
-
 
1988
            }
-
 
1989
        }
-
 
1990
        return -1;
-
 
1991
    }
-
 
1992
 
1901
    @RequestMapping(value = "/notifications", method = RequestMethod.GET)
1993
    @RequestMapping(value = "/notifications", method = RequestMethod.GET)
1902
    public String getNotificationsWithType(HttpServletRequest request, @RequestParam(required = false) MessageType
1994
    public String getNotificationsWithType(HttpServletRequest request, @RequestParam(required = false) MessageType
1903
            messageType, @RequestParam(name = "offset", defaultValue = "0") int offset,
1995
            messageType, @RequestParam(name = "offset", defaultValue = "0") int offset,
1904
                                           @RequestParam(name = "limit", defaultValue = "20") int limit, Model model) throws Exception {
1996
                                           @RequestParam(name = "limit", defaultValue = "20") int limit, Model model) throws Exception {
1905
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
1997
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);