Search in sources :

Example 51 with AxelorException

use of com.axelor.exception.AxelorException in project axelor-open-suite by axelor.

the class ICalendarService method removeEventFromIcal.

public void removeEventFromIcal(ICalendarEvent event) throws MalformedURLException, ICalendarException {
    if (event.getCalendar() != null && !Strings.isNullOrEmpty(event.getUid())) {
        ICalendar calendar = event.getCalendar();
        PathResolver RESOLVER = getPathResolver(calendar.getTypeSelect());
        Protocol protocol = getProtocol(calendar.getIsSslConnection());
        URL url = new URL(protocol.getScheme(), calendar.getUrl(), calendar.getPort(), "");
        ICalendarStore store = new ICalendarStore(url, RESOLVER);
        try {
            if (store.connect(calendar.getLogin(), getCalendarDecryptPassword(calendar.getPassword()))) {
                List<CalDavCalendarCollection> colList = store.getCollections();
                if (!colList.isEmpty()) {
                    CalDavCalendarCollection collection = colList.get(0);
                    final Map<String, VEvent> remoteEvents = new HashMap<>();
                    for (VEvent item : ICalendarStore.getEvents(collection)) {
                        remoteEvents.put(item.getUid().getValue(), item);
                    }
                    VEvent target = remoteEvents.get(event.getUid());
                    if (target != null)
                        removeCalendar(collection, target.getUid().getValue());
                }
            } else {
                throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CALENDAR_NOT_VALID));
            }
        } catch (Exception e) {
            throw new ICalendarException(e);
        } finally {
            store.disconnect();
        }
    }
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) AxelorException(com.axelor.exception.AxelorException) HashMap(java.util.HashMap) PathResolver(net.fortuna.ical4j.connector.dav.PathResolver) CalDavCalendarCollection(net.fortuna.ical4j.connector.dav.CalDavCalendarCollection) URL(java.net.URL) FailedOperationException(net.fortuna.ical4j.connector.FailedOperationException) URISyntaxException(java.net.URISyntaxException) ObjectStoreException(net.fortuna.ical4j.connector.ObjectStoreException) ParseException(java.text.ParseException) ParserException(net.fortuna.ical4j.data.ParserException) ValidationException(net.fortuna.ical4j.validate.ValidationException) SocketException(java.net.SocketException) AxelorException(com.axelor.exception.AxelorException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) DavException(org.apache.jackrabbit.webdav.DavException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConstraintViolationException(net.fortuna.ical4j.model.ConstraintViolationException) ICalendar(com.axelor.apps.base.db.ICalendar) Protocol(org.apache.commons.httpclient.protocol.Protocol)

Example 52 with AxelorException

use of com.axelor.exception.AxelorException in project axelor-open-suite by axelor.

the class ICalendarService method doSync.

@Transactional(rollbackOn = { Exception.class })
protected ICalendar doSync(ICalendar calendar, CalDavCalendarCollection collection, LocalDateTime startDate, LocalDateTime endDate) throws IOException, URISyntaxException, ParseException, ObjectStoreException, ConstraintViolationException, DavException, ParserConfigurationException, ParserException, AxelorException {
    final boolean keepRemote = calendar.getKeepRemote() == Boolean.TRUE;
    final Map<String, VEvent> modifiedRemoteEvents = new HashMap<>();
    final List<ICalendarEvent> modifiedLocalEvents = getICalendarEvents(calendar);
    final Set<String> allRemoteUids = new HashSet<>();
    final Set<VEvent> updatedEvents = new HashSet<>();
    List<VEvent> events = null;
    Instant lastSynchro = null;
    if (calendar.getLastSynchronizationDateT() != null) {
        lastSynchro = calendar.getLastSynchronizationDateT().toInstant(OffsetDateTime.now().getOffset());
    } else {
        lastSynchro = LocalDateTime.MIN.toInstant(OffsetDateTime.now().getOffset());
    }
    if (startDate == null || endDate == null) {
        events = ICalendarStore.getModifiedEvents(collection, null, allRemoteUids);
    } else {
        events = ICalendarStore.getModifiedEventsInRange(collection, lastSynchro, allRemoteUids, startDate, endDate);
    }
    if (CollectionUtils.isEmpty(events) && CollectionUtils.isEmpty(modifiedLocalEvents)) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CALENDAR_NO_EVENTS_FOR_SYNC_ERROR));
    }
    if (events != null) {
        for (VEvent item : events) {
            modifiedRemoteEvents.put(item.getUid().getValue(), item);
        }
    }
    for (ICalendarEvent item : modifiedLocalEvents) {
        VEvent source = createVEvent(item);
        VEvent target = modifiedRemoteEvents.get(source.getUid().getValue());
        // If uid is empty, the event is new
        if (StringUtils.isBlank(item.getUid())) {
            item.setUid(source.getUid().getValue());
            Calendar cal = newCalendar();
            cal.getComponents().add(source);
            collection.addCalendar(cal);
            allRemoteUids.add(item.getUid());
        } else // else it has been modified
        {
            // if target is null, then it hasn't been modified or it has been deleted
            if (target == null) {
                target = source;
            } else {
                updateEvent(source, target, keepRemote);
            // modifiedRemoteEvents.remove(target.getUid().getValue());
            }
            updatedEvents.add(target);
        }
    }
    // corresponding ICalendarEvent
    for (Map.Entry<String, VEvent> entry : modifiedRemoteEvents.entrySet()) {
        findOrCreateEvent(entry.getValue(), calendar);
    }
    // update remote events
    for (VEvent item : updatedEvents) {
        Calendar cal = newCalendar();
        cal.getComponents().add(item);
        // collection.updateCalendar(cal);
        try {
            collection.addCalendar(cal);
        } catch (Exception e) {
            TraceBackService.trace(e);
        }
    }
    // remove deleted remote events
    removeDeletedEventsInRange(allRemoteUids, calendar, startDate, endDate);
    return calendar;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) ICalendarEvent(com.axelor.apps.base.db.ICalendarEvent) AxelorException(com.axelor.exception.AxelorException) HashMap(java.util.HashMap) Instant(java.time.Instant) Calendar(net.fortuna.ical4j.model.Calendar) ICalendar(com.axelor.apps.base.db.ICalendar) FailedOperationException(net.fortuna.ical4j.connector.FailedOperationException) URISyntaxException(java.net.URISyntaxException) ObjectStoreException(net.fortuna.ical4j.connector.ObjectStoreException) ParseException(java.text.ParseException) ParserException(net.fortuna.ical4j.data.ParserException) ValidationException(net.fortuna.ical4j.validate.ValidationException) SocketException(java.net.SocketException) AxelorException(com.axelor.exception.AxelorException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) DavException(org.apache.jackrabbit.webdav.DavException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConstraintViolationException(net.fortuna.ical4j.model.ConstraintViolationException) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Transactional(com.google.inject.persist.Transactional)

Example 53 with AxelorException

use of com.axelor.exception.AxelorException in project axelor-open-suite by axelor.

the class DuplicateObjectsService method concatFields.

/*
   * get all records for duplicate records
   */
private String concatFields(Class<?> modelClass, Set<String> fieldSet) throws AxelorException {
    StringBuilder fields = new StringBuilder("LOWER(concat(");
    Mapper mapper = Mapper.of(modelClass);
    int count = 0;
    for (String field : fieldSet) {
        Property property = mapper.getProperty(field);
        if (property == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.GENERAL_8), field, modelClass.getSimpleName());
        }
        if (property.isCollection()) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.GENERAL_9), field);
        }
        if (count != 0) {
            fields.append(",");
        }
        count++;
        fields.append("cast(self" + "." + field);
        if (property.getTarget() != null) {
            fields.append(".id");
        }
        fields.append(" as string)");
    }
    fields.append("))");
    return fields.toString();
}
Also used : Mapper(com.axelor.db.mapper.Mapper) AxelorException(com.axelor.exception.AxelorException) Property(com.axelor.db.mapper.Property)

Example 54 with AxelorException

use of com.axelor.exception.AxelorException in project axelor-open-suite by axelor.

the class ProductBaseRepository method save.

@Override
public Product save(Product product) {
    try {
        if (appBaseService.getAppBase().getGenerateProductSequence() && Strings.isNullOrEmpty(product.getCode())) {
            product.setCode(Beans.get(ProductService.class).getSequence(product));
        }
    } catch (Exception e) {
        TraceBackService.traceExceptionFromSaveMethod(e);
        throw new PersistenceException(e.getMessage(), e);
    }
    product.setFullName(String.format(FULL_NAME_FORMAT, product.getCode(), product.getName()));
    if (product.getId() != null) {
        Product oldProduct = Beans.get(ProductRepository.class).find(product.getId());
        translationService.updateFormatedValueTranslations(oldProduct.getFullName(), FULL_NAME_FORMAT, product.getCode(), product.getName());
    } else {
        translationService.createFormatedValueTranslations(FULL_NAME_FORMAT, product.getCode(), product.getName());
    }
    product = super.save(product);
    if (product.getBarCode() == null && appBaseService.getAppBase().getActivateBarCodeGeneration()) {
        try {
            boolean addPadding = false;
            InputStream inStream;
            if (!appBaseService.getAppBase().getEditProductBarcodeType()) {
                inStream = barcodeGeneratorService.createBarCode(product.getSerialNumber(), appBaseService.getAppBase().getBarcodeTypeConfig(), addPadding);
            } else {
                inStream = barcodeGeneratorService.createBarCode(product.getSerialNumber(), product.getBarcodeTypeConfig(), addPadding);
            }
            if (inStream != null) {
                MetaFile barcodeFile = metaFiles.upload(inStream, String.format("ProductBarCode%d.png", product.getId()));
                product.setBarCode(barcodeFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AxelorException e) {
            TraceBackService.traceExceptionFromSaveMethod(e);
            throw new ValidationException(e);
        }
    }
    return super.save(product);
}
Also used : AxelorException(com.axelor.exception.AxelorException) ValidationException(javax.validation.ValidationException) InputStream(java.io.InputStream) PersistenceException(javax.persistence.PersistenceException) Product(com.axelor.apps.base.db.Product) MetaFile(com.axelor.meta.db.MetaFile) IOException(java.io.IOException) IOException(java.io.IOException) AxelorException(com.axelor.exception.AxelorException) PersistenceException(javax.persistence.PersistenceException) ValidationException(javax.validation.ValidationException)

Example 55 with AxelorException

use of com.axelor.exception.AxelorException in project axelor-open-suite by axelor.

the class PartnerServiceImpl method checkDefaultAddress.

/**
 * Ensures that there is exactly one default invoicing address and no more than one default
 * delivery address. If the partner address list is valid, returns the default invoicing address.
 *
 * @param partner
 * @throws AxelorException
 */
protected Address checkDefaultAddress(Partner partner) throws AxelorException {
    List<PartnerAddress> partnerAddressList = partner.getPartnerAddressList();
    Address defaultInvoicingAddress = null;
    Address defaultDeliveryAddress = null;
    if (partnerAddressList != null) {
        for (PartnerAddress partnerAddress : partnerAddressList) {
            if (partnerAddress.getIsDefaultAddr() && partnerAddress.getIsInvoicingAddr()) {
                if (defaultInvoicingAddress != null) {
                    throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.ADDRESS_8));
                }
                defaultInvoicingAddress = partnerAddress.getAddress();
            }
            if (partnerAddress.getIsDefaultAddr() && partnerAddress.getIsDeliveryAddr()) {
                if (defaultDeliveryAddress != null) {
                    throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.ADDRESS_9));
                }
                defaultDeliveryAddress = partnerAddress.getAddress();
            }
        }
    }
    return defaultInvoicingAddress;
}
Also used : AxelorException(com.axelor.exception.AxelorException) EmailAddress(com.axelor.apps.message.db.EmailAddress) PartnerAddress(com.axelor.apps.base.db.PartnerAddress) Address(com.axelor.apps.base.db.Address) PartnerAddress(com.axelor.apps.base.db.PartnerAddress)

Aggregations

AxelorException (com.axelor.exception.AxelorException)546 Transactional (com.google.inject.persist.Transactional)126 BigDecimal (java.math.BigDecimal)118 ArrayList (java.util.ArrayList)84 IOException (java.io.IOException)73 Product (com.axelor.apps.base.db.Product)64 Company (com.axelor.apps.base.db.Company)63 LocalDate (java.time.LocalDate)58 Partner (com.axelor.apps.base.db.Partner)48 List (java.util.List)45 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)40 HashMap (java.util.HashMap)38 Invoice (com.axelor.apps.account.db.Invoice)33 StockMove (com.axelor.apps.stock.db.StockMove)32 Map (java.util.Map)31 Beans (com.axelor.inject.Beans)30 I18n (com.axelor.i18n.I18n)28 Inject (com.google.inject.Inject)28 MoveLine (com.axelor.apps.account.db.MoveLine)27 Context (com.axelor.rpc.Context)27