Search in sources :

Example 41 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project Settler by EmhyrVarEmreis.

the class ImportService method importTransactions.

public void importTransactions(MultipartFile file) throws IOException {
    List<Category> categoryList = categoryRepository.findAll();
    Map<String, Category> categoryMap = categoryList == null ? new HashMap<>() : categoryList.stream().collect(Collectors.toMap(Category::getCode, category -> category));
    List<Transaction> transactionList = new ArrayList<>();
    ByteArrayInputStream stream = new ByteArrayInputStream(file.getBytes());
    String content = IOUtils.toString(stream, "UTF-8");
    String[] lines = content.split("\\r?\\n");
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("dd.MM.yyyy");
    User o = userRepository.findOneByLogin(splitLine(lines[0])[26]);
    User c = userRepository.findOneByLogin(splitLine(lines[0])[27]);
    int ln = 0;
    for (String line : lines) {
        ln++;
        if (ln < 3) {
            continue;
        }
        try {
            String[] cells = splitLine(line);
            String name = cells[0];
            String mark = cells[4].trim();
            Double value;
            Double sharedAValue;
            Double sharedBValue;
            Integer sharedA;
            Integer sharedB;
            LocalDateTime date;
            Boolean normal;
            Transaction transaction = new Transaction();
            List<Redistribution> owners = new ArrayList<>();
            List<Redistribution> contractors = new ArrayList<>();
            value = Double.valueOf(cells[2].replaceAll("[^0-9^,-]+", "").replace(",", "."));
            date = dateTimeFormatter.parseLocalDateTime(cells[1]);
            sharedA = cells.length < 7 || cells[5].trim().isEmpty() ? -1 : Integer.valueOf(cells[5].trim());
            sharedB = cells.length < 7 || cells[6].trim().isEmpty() ? -1 : Integer.valueOf(cells[6].trim());
            normal = value > 0;
            value = Math.abs(value);
            if (mark.equalsIgnoreCase("x")) {
                if (sharedA < 0 || sharedB < 0) {
                    sharedA = 1;
                    sharedB = 2;
                }
            } else if (mark.equalsIgnoreCase("b") || mark.equalsIgnoreCase("z")) {
                sharedA = 1;
                sharedB = 1;
            } else {
                continue;
            }
            sharedAValue = value;
            value = (value / (sharedA)) * sharedB;
            value = (double) Math.round(value * 100) / 100;
            sharedBValue = value - sharedAValue;
            sharedBValue = (double) Math.round(sharedBValue * 100) / 100;
            if (normal) {
                owners.add(new Redistribution(RedistributionType.O, transaction, o, value));
                if (sharedBValue > 0) {
                    contractors.add(new Redistribution(RedistributionType.C, transaction, o, sharedBValue));
                }
                contractors.add(new Redistribution(RedistributionType.C, transaction, c, sharedAValue));
            } else {
                owners.add(new Redistribution(RedistributionType.O, transaction, c, value));
                contractors.add(new Redistribution(RedistributionType.C, transaction, o, sharedAValue));
                if (sharedBValue > 0) {
                    contractors.add(new Redistribution(RedistributionType.C, transaction, c, sharedBValue));
                }
            }
            transaction.setValue(value);
            owners.forEach(redistribution -> redistribution.setPercentage(redistribution.getPercentage() / 1.0 / transaction.getValue()));
            contractors.forEach(redistribution -> redistribution.setPercentage(redistribution.getPercentage() / 1.0 / transaction.getValue()));
            transaction.setOwners(owners);
            transaction.setContractors(contractors);
            date = date.withHourOfDay(12).withMinuteOfHour(0).withSecondOfMinute(0);
            transaction.setCreator(Security.currentUser());
            transaction.setType(TransactionType.NOR);
            transaction.setDescription(name);
            transaction.setEvaluated(date);
            List<Category> cl = checkCategories(transaction.getDescription()).stream().map(categoryMap::get).filter(Objects::nonNull).collect(Collectors.toList());
            transaction.setCategories(cl.isEmpty() ? null : cl);
            transaction.setReference(sequenceManager.getNextReferenceForTransaction(transaction));
            transactionList.add(transaction);
        } catch (Exception e) {
            log.warn("Unable to convert line No{}: {}", ln, line, e);
        }
    }
    transactionList.sort((o1, o2) -> o1.getEvaluated().compareTo(o2.getCreated()));
    transactionList.forEach(t -> {
        t.setCreated(new LocalDateTime());
        log.info(t.getEvaluated() + " " + t.getReference() + " " + t.getValue() + " [" + t.getDescription() + "]" + " [" + t.getOwners().stream().map(r -> r.getId().getUser().getLogin() + "/" + r.getPercentage()).collect(Collectors.joining(", ")) + "]" + " [" + t.getContractors().stream().map(r -> r.getId().getUser().getLogin() + "/" + r.getPercentage()).collect(Collectors.joining(", ")) + "]" + " [" + (t.getCategories() == null ? "" : t.getCategories().stream().map(Category::getCode).collect(Collectors.joining(", "))) + "]");
        transactionRepository.save(t);
    });
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) TransactionType(pl.morecraft.dev.settler.domain.dictionaries.TransactionType) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Category(pl.morecraft.dev.settler.domain.Category) ByteArrayInputStream(java.io.ByteArrayInputStream) Transaction(pl.morecraft.dev.settler.domain.Transaction) Gson(com.google.gson.Gson) Service(org.springframework.stereotype.Service) UserRepository(pl.morecraft.dev.settler.dao.repository.UserRepository) Qualifier(org.springframework.beans.factory.annotation.Qualifier) CategoryRepository(pl.morecraft.dev.settler.dao.repository.CategoryRepository) RedistributionType(pl.morecraft.dev.settler.domain.dictionaries.RedistributionType) DateTimeFormat(org.joda.time.format.DateTimeFormat) User(pl.morecraft.dev.settler.domain.User) Logger(org.slf4j.Logger) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Security(pl.morecraft.dev.settler.security.util.Security) IOException(java.io.IOException) LocalDateTime(org.joda.time.LocalDateTime) Collectors(java.util.stream.Collectors) TransactionRepository(pl.morecraft.dev.settler.dao.repository.TransactionRepository) IOUtils(org.apache.commons.io.IOUtils) MultipartFile(org.springframework.web.multipart.MultipartFile) Redistribution(pl.morecraft.dev.settler.domain.Redistribution) Transactional(org.springframework.transaction.annotation.Transactional) Category(pl.morecraft.dev.settler.domain.Category) User(pl.morecraft.dev.settler.domain.User) IOException(java.io.IOException) Redistribution(pl.morecraft.dev.settler.domain.Redistribution) Transaction(pl.morecraft.dev.settler.domain.Transaction) ByteArrayInputStream(java.io.ByteArrayInputStream) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 42 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project joda-time by JodaOrg.

the class TestDateTime_Constructors method testParse_formatter.

public void testParse_formatter() throws Throwable {
    DateTimeFormatter f = DateTimeFormat.forPattern("yyyy--dd MM HH").withChronology(ISOChronology.getInstance(PARIS));
    assertEquals(new DateTime(2010, 6, 30, 13, 0, ISOChronology.getInstance(PARIS)), DateTime.parse("2010--30 06 13", f));
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 43 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project joda-time by JodaOrg.

the class TestYearMonth_Constructors method testParse_formatter.

public void testParse_formatter() throws Throwable {
    DateTimeFormatter f = DateTimeFormat.forPattern("yyyy--MM").withChronology(ISOChronology.getInstance(PARIS));
    assertEquals(new YearMonth(2010, 6), YearMonth.parse("2010--06", f));
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 44 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project ETSMobile-Android2 by ApplETS.

the class EvenementCommunauteAdapter method getGroupView.

@Override
public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView != null) {
        holder = (ViewHolder) convertView.getTag();
    } else {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.row_header_applets_events, parent, false);
        holder = new ViewHolder();
        holder.imageViewEvenement = (ImageView) convertView.findViewById(R.id.iv_evenement);
        holder.textViewNomEvenement = (TextView) convertView.findViewById(R.id.tv_nom_evenement);
        holder.textViewMois = (TextView) convertView.findViewById(R.id.tv_mois);
        holder.textViewJour = (TextView) convertView.findViewById(R.id.tv_jour);
        convertView.setTag(holder);
    }
    final EvenementCommunaute item = (EvenementCommunaute) getGroup(listPosition);
    String urlImageEvenement = item.getImage();
    final String urlImageOrganisateur = item.getSourceEvenement().getUrlImage();
    if (!urlImageEvenement.isEmpty()) {
        Picasso.with(context).load(urlImageEvenement).placeholder(R.drawable.event_header).into(holder.imageViewEvenement);
    }
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
    DateTime date = dateTimeFormatter.parseDateTime(item.getDebut());
    String monthString = date.toString("MMMM");
    monthString = monthString.substring(0, 1).toUpperCase() + monthString.substring(1, 3);
    holder.textViewMois.setText(monthString);
    holder.textViewJour.setText(date.toString("dd", Locale.CANADA_FRENCH));
    holder.textViewNomEvenement.setText(item.getNom());
    return convertView;
}
Also used : EvenementCommunaute(ca.etsmtl.applets.etsmobile.model.applets_events.EvenementCommunaute) LayoutInflater(android.view.LayoutInflater) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime)

Example 45 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project ETSMobile-Android2 by ApplETS.

the class Helper method ConvertFromWebService.

public static DateTime ConvertFromWebService(String strDate) {
    DateTimeFormatter parser1 = new DateTimeFormatterBuilder().append(ISODateTimeFormat.date()).appendLiteral('T').append(ISODateTimeFormat.hourMinuteSecond()).appendOptional(fractionElement()).appendOptional(offsetElement()).toFormatter().withZone(DateTimeZone.UTC);
    parser1.withChronology(ISOChronology.getInstanceUTC());
    return parser1.parseDateTime(strDate);
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTimeFormatterBuilder(org.joda.time.format.DateTimeFormatterBuilder)

Aggregations

DateTimeFormatter (org.joda.time.format.DateTimeFormatter)209 DateTime (org.joda.time.DateTime)95 Date (java.util.Date)40 Test (org.junit.Test)25 DateTimeZone (org.joda.time.DateTimeZone)19 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)16 SolrInputDocument (org.apache.solr.common.SolrInputDocument)12 IndexSchema (org.apache.solr.schema.IndexSchema)12 DateTimeFormatterBuilder (org.joda.time.format.DateTimeFormatterBuilder)12 CswSourceConfiguration (org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration)10 IOException (java.io.IOException)9 Calendar (java.util.Calendar)8 Map (java.util.Map)8 DatasetConfigDTO (com.linkedin.thirdeye.datalayer.dto.DatasetConfigDTO)7 FormatDateTimeFormatter (org.elasticsearch.common.joda.FormatDateTimeFormatter)7 Test (org.testng.annotations.Test)7 TimeSpec (com.linkedin.thirdeye.api.TimeSpec)6 FilterType (net.opengis.filter.v_1_1_0.FilterType)6 Deployment (org.activiti.engine.test.Deployment)6