Search in sources :

Example 1 with Filter

use of com.axelor.rpc.filter.Filter in project axelor-open-suite by axelor.

the class MailServiceBaseImpl method findEmails.

@Override
public List<InternetAddress> findEmails(String matching, List<String> selected, int maxResult) {
    JpaSecurity jpaSecurity = Beans.get(JpaSecurity.class);
    // Users
    Filter userPermissionFilter = jpaSecurity.getFilter(JpaSecurity.CAN_READ, User.class);
    List<String> selectedWithoutNull = new ArrayList<String>(selected);
    for (int i = 0; i < selected.size(); i++) {
        if (Strings.isNullOrEmpty(selected.get(i)))
            selectedWithoutNull.remove(i);
    }
    final List<String> where = new ArrayList<>();
    final Map<String, Object> params = new HashMap<>();
    if (userPermissionFilter != null) {
        where.add(userPermissionFilter.getQuery());
    }
    where.add("((self.partner is not null AND self.partner.emailAddress is not null) OR (self.email is not null))");
    if (!isBlank(matching)) {
        where.add("(LOWER(self.partner.emailAddress.address) like LOWER(:email) OR LOWER(self.partner.fullName) like LOWER(:email) OR LOWER(self.email) like LOWER(:email) OR LOWER(self.name) like LOWER(:email))");
        params.put("email", "%" + matching + "%");
    }
    if (selectedWithoutNull != null && !selectedWithoutNull.isEmpty()) {
        where.add("self.partner.emailAddress.address not in (:selected)");
        params.put("selected", selectedWithoutNull);
    }
    final String filter = Joiner.on(" AND ").join(where);
    final Query<User> query = Query.of(User.class);
    if (!isBlank(filter)) {
        if (userPermissionFilter != null) {
            query.filter(filter, userPermissionFilter.getParams());
        } else {
            query.filter(filter);
        }
        query.bind(params);
    }
    final List<InternetAddress> addresses = new ArrayList<>();
    if (jpaSecurity.isPermitted(JpaSecurity.CAN_READ, User.class)) {
        for (User user : query.fetch(maxResult)) {
            try {
                if (user.getPartner() != null && user.getPartner().getEmailAddress() != null && !Strings.isNullOrEmpty(user.getPartner().getEmailAddress().getAddress())) {
                    final InternetAddress item = new InternetAddress(user.getPartner().getEmailAddress().getAddress(), user.getFullName());
                    addresses.add(item);
                    selectedWithoutNull.add(user.getPartner().getEmailAddress().getAddress());
                } else if (!Strings.isNullOrEmpty(user.getEmail())) {
                    final InternetAddress item = new InternetAddress(user.getEmail(), user.getFullName());
                    addresses.add(item);
                    selectedWithoutNull.add(user.getEmail());
                }
            } catch (UnsupportedEncodingException e) {
                TraceBackService.trace(e);
            }
        }
    }
    // Partners
    Filter partnerPermissionFilter = jpaSecurity.getFilter(JpaSecurity.CAN_READ, Partner.class);
    final List<String> where2 = new ArrayList<>();
    final Map<String, Object> params2 = new HashMap<>();
    if (partnerPermissionFilter != null) {
        where2.add(partnerPermissionFilter.getQuery());
    }
    where2.add("self.emailAddress is not null");
    if (!isBlank(matching)) {
        where2.add("(LOWER(self.emailAddress.address) like LOWER(:email) OR LOWER(self.fullName) like LOWER(:email))");
        params2.put("email", "%" + matching + "%");
    }
    if (selectedWithoutNull != null && !selectedWithoutNull.isEmpty()) {
        where2.add("self.emailAddress.address not in (:selected)");
        params2.put("selected", selectedWithoutNull);
    }
    final String filter2 = Joiner.on(" AND ").join(where2);
    final Query<Partner> query2 = Query.of(Partner.class);
    if (!isBlank(filter2)) {
        if (partnerPermissionFilter != null) {
            query2.filter(filter2, partnerPermissionFilter.getParams());
        } else {
            query2.filter(filter2);
        }
        query2.bind(params2);
    }
    if (jpaSecurity.isPermitted(JpaSecurity.CAN_READ, Partner.class)) {
        for (Partner partner : query2.fetch(maxResult)) {
            try {
                if (partner.getEmailAddress() != null && !Strings.isNullOrEmpty(partner.getEmailAddress().getAddress())) {
                    final InternetAddress item = new InternetAddress(partner.getEmailAddress().getAddress(), partner.getFullName());
                    addresses.add(item);
                }
            } catch (UnsupportedEncodingException e) {
                TraceBackService.trace(e);
            }
        }
    }
    return addresses;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) JpaSecurity(com.axelor.db.JpaSecurity) User(com.axelor.auth.db.User) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Filter(com.axelor.rpc.filter.Filter) Partner(com.axelor.apps.base.db.Partner)

Example 2 with Filter

use of com.axelor.rpc.filter.Filter in project axelor-open-suite by axelor.

the class AdvancedExportController method createCriteria.

@SuppressWarnings("unchecked")
private List<Long> createCriteria(ActionRequest request, AdvancedExport advancedExport) {
    if (request.getContext().get("_criteria") != null) {
        if (request.getContext().get("_criteria").toString().startsWith("[")) {
            String ids = request.getContext().get("_criteria").toString();
            return Splitter.on(", ").splitToList(ids.substring(1, ids.length() - 1)).stream().map(id -> Long.valueOf(id.toString())).collect(Collectors.toList());
        } else {
            ObjectMapper mapper = new ObjectMapper();
            ActionRequest parentRequest = mapper.convertValue(request.getContext().get("_criteria"), ActionRequest.class);
            Class<? extends Model> klass = (Class<? extends Model>) parentRequest.getBeanClass();
            Filter filter = Beans.get(AdvancedExportService.class).getJpaSecurityFilter(advancedExport.getMetaModel());
            Stream<? extends Model> listObj = parentRequest.getCriteria().createQuery(klass, filter).fetchSteam(advancedExport.getMaxExportLimit());
            return listObj.map(it -> it.getId()).collect(Collectors.toList());
        }
    }
    return null;
}
Also used : MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) Filter(com.axelor.rpc.filter.Filter) ActionView(com.axelor.meta.schema.actions.ActionView) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Mapper(com.axelor.db.mapper.Mapper) AdvancedExportLine(com.axelor.apps.base.db.AdvancedExportLine) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) AdvancedExport(com.axelor.apps.base.db.AdvancedExport) AxelorException(com.axelor.exception.AxelorException) ActionResponse(com.axelor.rpc.ActionResponse) Map(java.util.Map) I18n(com.axelor.i18n.I18n) Inflector(com.axelor.common.Inflector) AdvancedExportRepository(com.axelor.apps.base.db.repo.AdvancedExportRepository) ActionRequest(com.axelor.rpc.ActionRequest) Splitter(com.google.common.base.Splitter) MetaFiles(com.axelor.meta.MetaFiles) Logger(org.slf4j.Logger) Model(com.axelor.db.Model) MetaField(com.axelor.meta.db.MetaField) MethodHandles(java.lang.invoke.MethodHandles) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TraceBackService(com.axelor.exception.service.TraceBackService) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) AdvancedExportService(com.axelor.apps.base.service.advancedExport.AdvancedExportService) MetaFile(com.axelor.meta.db.MetaFile) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) Stream(java.util.stream.Stream) Beans(com.axelor.inject.Beans) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository) IExceptionMessage(com.axelor.apps.base.exceptions.IExceptionMessage) Context(com.axelor.rpc.Context) Joiner(com.google.common.base.Joiner) Singleton(com.google.inject.Singleton) ActionRequest(com.axelor.rpc.ActionRequest) Filter(com.axelor.rpc.filter.Filter) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) AdvancedExportService(com.axelor.apps.base.service.advancedExport.AdvancedExportService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with Filter

use of com.axelor.rpc.filter.Filter in project axelor-open-suite by axelor.

the class ClientViewServiceImpl method getQuotationsOfUser.

@Override
public List<Filter> getQuotationsOfUser(User user) {
    List<Filter> filters = new ArrayList<>();
    Filter filterFromPermission = security.getFilter(JpaSecurity.CAN_READ, SaleOrder.class);
    Filter filter = new JPQLFilter("self.clientPartner.id = " + user.getPartner().getId() + " AND self.statusSelect IN (" + SaleOrderRepository.STATUS_DRAFT_QUOTATION + "," + SaleOrderRepository.STATUS_FINALIZED_QUOTATION + ")");
    if (user.getActiveCompany() != null) {
        filter = Filter.and(filter, new JPQLFilter(" self.company.id = " + user.getActiveCompany().getId()));
    }
    filters.add(filter);
    addPermissionFilter(filters, filterFromPermission);
    return filters;
}
Also used : Filter(com.axelor.rpc.filter.Filter) JPQLFilter(com.axelor.rpc.filter.JPQLFilter) ArrayList(java.util.ArrayList) JPQLFilter(com.axelor.rpc.filter.JPQLFilter)

Example 4 with Filter

use of com.axelor.rpc.filter.Filter in project axelor-open-suite by axelor.

the class ClientViewServiceImpl method getTasksInProgressOfUser.

@Override
public List<Filter> getTasksInProgressOfUser(User user) {
    List<Filter> filters = new ArrayList<>();
    Filter filterFromPermission = security.getFilter(JpaSecurity.CAN_READ, ProjectTask.class);
    Filter filter = new JPQLFilter("self.status = '" + ProjectTaskRepository.STATUS_IN_PROGRESS + "' AND self.typeSelect = '" + ProjectTaskRepository.TYPE_TASK + "' AND self.project.clientPartner.id = " + user.getPartner().getId());
    if (user.getActiveCompany() != null) {
        filter = Filter.and(filter, new JPQLFilter(" self.project.company.id = " + user.getActiveCompany().getId()));
    }
    filters.add(filter);
    addPermissionFilter(filters, filterFromPermission);
    return filters;
}
Also used : Filter(com.axelor.rpc.filter.Filter) JPQLFilter(com.axelor.rpc.filter.JPQLFilter) ArrayList(java.util.ArrayList) JPQLFilter(com.axelor.rpc.filter.JPQLFilter)

Example 5 with Filter

use of com.axelor.rpc.filter.Filter in project axelor-open-suite by axelor.

the class ClientViewServiceImpl method getLateTicketsOfUser.

@Override
public List<Filter> getLateTicketsOfUser(User user) {
    List<Filter> filters = new ArrayList<>();
    Filter filterFromPermission = security.getFilter(JpaSecurity.CAN_READ, Ticket.class);
    Filter filter = new JPQLFilter("self.customer.id = " + user.getPartner().getId() + " AND self.assignedToUser.id = " + user.getId() + " AND ((self.endDateT != null AND self.endDateT > self.deadlineDateT) " + " OR (self.endDateT = null and self.deadlineDateT < current_date() ) )");
    filters.add(filter);
    addPermissionFilter(filters, filterFromPermission);
    return filters;
}
Also used : Filter(com.axelor.rpc.filter.Filter) JPQLFilter(com.axelor.rpc.filter.JPQLFilter) ArrayList(java.util.ArrayList) JPQLFilter(com.axelor.rpc.filter.JPQLFilter)

Aggregations

Filter (com.axelor.rpc.filter.Filter)47 JPQLFilter (com.axelor.rpc.filter.JPQLFilter)23 ArrayList (java.util.ArrayList)22 User (com.axelor.auth.db.User)21 ClientViewService (com.axelor.apps.portal.service.ClientViewService)19 StockMove (com.axelor.apps.stock.db.StockMove)7 Invoice (com.axelor.apps.account.db.Invoice)5 Ticket (com.axelor.apps.helpdesk.db.Ticket)5 SaleOrder (com.axelor.apps.sale.db.SaleOrder)5 ProjectTask (com.axelor.apps.project.db.ProjectTask)4 StockMoveRepository (com.axelor.apps.stock.db.repo.StockMoveRepository)3 HashMap (java.util.HashMap)3 AdvancedExport (com.axelor.apps.base.db.AdvancedExport)2 AdvancedExportService (com.axelor.apps.base.service.advancedExport.AdvancedExportService)2 Project (com.axelor.apps.project.db.Project)2 SaleOrderRepository (com.axelor.apps.sale.db.repo.SaleOrderRepository)2 JpaSecurity (com.axelor.db.JpaSecurity)2 Model (com.axelor.db.Model)2 I18n (com.axelor.i18n.I18n)2 Beans (com.axelor.inject.Beans)2